Jump to content

PHP problems


MarkT
 Share

Recommended Posts

Hello,

I have a script in my website, which is used to send emails, however it's not working.

You log in with sessions, and then you go to a form, which then echos: Send successful,

but it doesn't actually send or complete t he SQL query to log it, it sometimes works, sometimes doesn't.

 

Thanks in advance.

Link to comment
Share on other sites

Not sure if I can help, but regardless, what's your code?

if($_GET['action'] == "send") {$send = $_POST['to'];if($_POST['to'] == "ALL") {$send = $_POST['to'];$subject = $_POST['subject'];$from2 = $_POST['email'];$content = $_POST['content'];$team = $_POST['to'];$emails = mysqli_query($con,"SELECT * FROM parents WHERE global = '1'");$date = date("d-m-Y");$time = time();$user = $_SESSION['fname'];$user2 = $_SESSION['lname'];mysqli_query($con,"INSERT into email_logs (user, send_to, content, date_sent, time_sent) VALUES ('{$user} {$user2}','{$send}','{$content}','{$date}','{$time}')");while($results = mysqli_fetch_array($emails)) {$toemail = "{$results['email_address']}";$subject = "{$subject}";$message = "<html><head>  <title>{$subject}</title></head><body>{$content}<br /><font color='red'><strong><h2>Regards</font> <br>Ditton Minors FC</h2></strong></body></html>";$from2 = $_POST['email'];$from = "{$from2}";$headers  = 'MIME-Version: 1.0' . "rn";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";$headers .= "Bcc: ".$toemail."rn";$headers .= "From: admin@dittonminorsfc.co.ukrn";mail($to,$subject,$message,$headers);echo "<h1 style='color: #00FF00;'>Email Sent Successfully</h1><meta http-equiv='refresh' content='2;url=adminemail.php'>";}} else {$send = $_POST['to'];$subject = $_POST['subject'];$from2 = $_POST['email'];$content = $_POST['content'];$team = $_POST['to'];$emails2 = mysqli_query($con,"SELECT * FROM parents WHERE team = '{$team}'");$date = date("d-m-Y");$time = time();$user = $_SESSION['fname'];$user2 = $_SESSION['lname'];mysqli_query($con,"INSERT into email_logs (user, send_to, content, date_sent, time_sent) VALUES ('{$user} {$user2}','{$send}','{$content}','{$date}','{$time}')");while($results2 = mysqli_fetch_array($emails2)) {$toemail = "{$results2['email_address']}";$subject = "{$subject}";$message = "<html><head>  <title>{$subject}</title></head><body>{$content}<br /><font color='red'><strong><h2>Regards</font> <br>Ditton Minors FC</h2></strong></body></html>";$from2 = $_POST['email'];$from = "{$from2}";$headers  = 'MIME-Version: 1.0' . "rn";$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";$headers .= "Bcc: ".$toemail."rn";$headers .= "From: admin@dittonminorsfc.co.ukrn";mail($to,$subject,$message,$headers);echo "<h1 style='color: #00FF00;'>Email Sent Successfully</h1><meta http-equiv='refresh' content='2;url=adminemail.php'>";}}}?>
Link to comment
Share on other sites

Can anyone help? I've logged out and logged back in, and after a few times of logging in/out it works, but it's long winded and would prefer a better resolution.

 

Thanks.

Link to comment
Share on other sites

have you done any logging or tried breaking the problem down line by line? Do you make sure your code gets through a few lines ok first? Comment everything out, then uncomment a few related lines at a time and see how it goes. I would pay specific attention to your queries and your mail call. You aren't checking for any errors at any of those points. Also, I never see you call session_start()

Link to comment
Share on other sites

have you done any logging or tried breaking the problem down line by line? Do you make sure your code gets through a few lines ok first? Comment everything out, then uncomment a few related lines at a time and see how it goes. I would pay specific attention to your queries and your mail call. You aren't checking for any errors at any of those points. Also, I never see you call session_start()

This is only part of the code, the bit that sends the mail. This works most times, but stops sometimes.

Link to comment
Share on other sites

mail() returns false if there was a mistake from PHP's part, so for one you can have your program send the mail first and write in the database later if there were no problems. Though as the manual said, mail() can't tell for sure if the SMTP server sent it, it just knows that the mail was accepted by the server for delivery.

$test = mail($to,$subject,$message,$headers);if(!$test) {    echo '( ! ) MAIL ERROR';    // Do something here} 
Link to comment
Share on other sites

 

mail() returns false if there was a mistake from PHP's part, so for one you can have your program send the mail first and write in the database later if there were no problems. Though as the manual said, mail() can't tell for sure if the SMTP server sent it, it just knows that the mail was accepted by the server for delivery.

$test = mail($to,$subject,$message,$headers);if(!$test) {    echo '( ! ) MAIL ERROR';    // Do something here} 

yes but it simultaneously executes an SQL query, which wasn't inserted properly, although it's inserted 10 already.

Link to comment
Share on other sites

Like mail(), mysqli_query() returns false if it didn't work. It's up to you to use these error handling features to prepare your program for unexpected results. It helps with debugging as well as controlling the program flow.

$query = mysqli_query($con,"INSERT into email_logs (user, send_to, content, date_sent, time_sent) VALUES ('{$user} {$user2}','{$send}','{$content}','{$date}','{$time}')");if(!$query) {    // Show the reason the query failed    echo mysqli_error();    exit;}
Link to comment
Share on other sites

Problem fixed,

Basically, I was trying to send a HTML email with a link, but the SQL query uses ', and the PHP uses " which meant it wasn't logging it properly, but still sent the emails, I ended up sending 10 emails without realising it, then I figured out the problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...