samanj Posted December 22, 2020 Share Posted December 22, 2020 Hi, A html form that I have made connects to a php file that contains code to send an acknowledgement email. For some reason the code at the end of the file sends the same email twice to the recipient. Anyone spot my (likely very obvious) error(s) anywhere? <?php //phpmailer while ($row = mysqli_fetch_array ($result)) { //connect to phpmailer require_once('PHPMailer/class.phpmailer.php'); //set email $mail = new PHPMailer(true); $mail->IsSMTP(); try { $mail->SMTPDebug = 1; $mail->SMTPAuth = TRUE; $mail->SMTPSecure = "tls"; $mail->Port = 342; $mail->Host = "smtp.******.com"; $mail->Username = "noreply@*******.com"; $mail->Password = "$new_str2"; $mail->AddReplyTo('noreply@*******.com', 'Your Name'); // $mail->AddAddress($row["referrer"], 'your Name'); // $mail->SetFrom('noreply@*******.com', 'Your Name'); // $mail->Subject = 'Subject'; $mail->AltBody = 'sorry, this app cannot show mail... please use another app to open this mail ! '; $mail->CharSet = 'UTF-8'; $mail->ContentType = 'text/html'; // set html mode $mail->MsgHTML(' <html> <body> To the referrer, <br><br> You have a new update for your patient.<br><br> Please check on the e-Referral system. Yours sincerely, <br><br> e-Referral system <br><br> <small> This message will not have any patient identifiable information, however, if you are not the intended recipient, please inform IT services.<br><br> We kindly ask that you do not disclose, copy or distribute information in this e-mail; as to do so is not advised and may be unlawful. <br><br> Your assistance is deeply appreciated. </small> </body> </html> '); // html message //$mail->AddAttachment('images/phpmailer.gif'); // $mail->Send(); // send echo "<center>Senior plan submitted successfully to the matching patient details. <br><br> Please be aware that only a single senior plan can be submitted per e-referral. <br> <br> Any further updates will overwrite the previous submission. <br> <br> It is advised to check the patient case to ensure the update has been made."; } catch (phpmailerException $e) { echo $e->errorMessage(); // phpmailer error code. do not change it } catch (Exception $e) { echo $e->getMessage(); // phpmailer error code. do not change it } ?> My appreciation in advance, Samanj Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/ Share on other sites More sharing options...
Barand Posted December 22, 2020 Share Posted December 22, 2020 I can't be definite because there does not not appear to be an end } to your while loop, but could it be because you are sending the mail inside a loop? I'm guessing, therefore, that isn't your actual code, so not much we can do. Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583343 Share on other sites More sharing options...
requinix Posted December 22, 2020 Share Posted December 22, 2020 I'm thinking either (a) the while loop Barand pointed out is returning more rows than expected or (b) the PHP script is executing twice. Both should be pretty easy to test. Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583344 Share on other sites More sharing options...
samanj Posted December 23, 2020 Author Share Posted December 23, 2020 Hi, So the full code is below: <html> <head> <style> * { text-align: center; font-size: medium; font-family: Arial, Helvetica, sans-serif; color: black; margin: 1em; /* set font */ } </style> </head> <body bgcolor="#B2D3C2"> <br> <a href="homepage.html">Return to Home screen</a><br> <p><span id="datetime"></span></p> <script> var dt = new Date(); document.getElementById("datetime").innerHTML = dt.toLocaleString(); </script> </center> </body> </html> <!--end of html code--> <?php // get values form input text and number $password = $_POST['password']; $snnumber = $_POST['snnumber']; $PIN = $_POST['PIN']; $comments = $_POST['comments']; $seniorsent = date("Y-m-d H:i:s"); //----------------start main code---------------------- if(isset($_POST['update'])) { $lines_array = file("file.txt"); $search_string = "username"; foreach($lines_array as $line) { if(strpos($line, $search_string) !== false) { list(, $new_str) = explode(":", $line); $new_str = trim($new_str); } } if(isset($_POST['update'])) { $lines_array = file("file2.txt"); $search_string = "pw"; foreach($lines_array as $line) { if(strpos($line, $search_string) !== false) { list(, $new_str2) = explode(":", $line); $new_str2 = trim($new_str2); } } $hostname = "localhost"; $username = "$new_str"; $password = "$password"; $databaseName = "referral"; $connect = mysqli_connect($hostname, $username, $password, $databaseName); //query2 update info in data-base $query2 = "UPDATE card SET comments = '$comments' , seniorsent = '$seniorsent' WHERE snnumber = '$snnumber' and PIN = '$PIN'"; @mysqli_query($connect, $query2); //query get the email info from data-base $query = "SELECT PIN, referrer FROM card WHERE PIN='$PIN' AND snnumber='$snnumber'"; $result = mysqli_query($connect, $query); //end of query codes //phpmailer while ($row = mysqli_fetch_array ($result)) { //connect to phpmailer require_once('PHPMailer/class.phpmailer.php'); //set email $mail = new PHPMailer(true); $mail->IsSMTP(); try { $mail->SMTPDebug = 1; $mail->SMTPAuth = TRUE; $mail->SMTPSecure = "tls"; $mail->Port = 342; $mail->Host = "smtp.******.com"; $mail->Username = "noreply@*******.com"; $mail->Password = "$new_str2"; $mail->AddReplyTo('noreply@*******.com', 'Your Name'); // $mail->AddAddress($row["referrer"], 'your Name'); // $mail->SetFrom('noreply@*******.com', 'Your Name'); // $mail->Subject = 'Subject'; $mail->AltBody = 'sorry, this app cannot show mail... please use another app to open this mail ! '; $mail->CharSet = 'UTF-8'; $mail->ContentType = 'text/html'; // set html mode $mail->MsgHTML(' <html> <body> To the referrer, <br><br> You have a new update for your patient.<br><br> Please check on the e-Referral system. Yours sincerely, <br><br> e-Referral system <br><br> <small> This message will not have any patient identifiable information, however, if you are not the intended recipient, please inform IT services.<br><br> We kindly ask that you do not disclose, copy or distribute information in this e-mail; as to do so is not advised and may be unlawful. <br><br> Your assistance is deeply appreciated. </small> </body> </html> '); // html message //$mail->AddAttachment('images/phpmailer.gif'); // $mail->Send(); // send echo "<center>Senior plan submitted successfully to the matching patient details. <br><br> Please be aware that only a single senior plan can be submitted per e-referral. <br> <br> Any further updates will overwrite the previous submission. <br> <br> It is advised to check the patient case to ensure the update has been made."; } catch (phpmailerException $e) { echo $e->errorMessage(); // phpmailer error code. do not change it } catch (Exception $e) { echo $e->getMessage(); // phpmailer error code. do not change it } } } } ?> Hopefully that helps. Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583351 Share on other sites More sharing options...
Barand Posted December 23, 2020 Share Posted December 23, 2020 16 hours ago, requinix said: I'm thinking either (a) the while loop Barand pointed out is returning more rows than expected or (b) the PHP script is executing twice. Both should be pretty easy to test. Unfortunately (for you) both those are things you will have to check for yourself. Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583353 Share on other sites More sharing options...
samanj Posted December 23, 2020 Author Share Posted December 23, 2020 I am inclined to think that the script is running twice as I receive two emails. From what it sounds like, no one is able to spot a simple error. Hence it’s back to me again. Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583354 Share on other sites More sharing options...
dodgeitorelse3 Posted December 23, 2020 Share Posted December 23, 2020 Did you bother to test the 2 suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583368 Share on other sites More sharing options...
Solution samanj Posted December 24, 2020 Author Solution Share Posted December 24, 2020 Turned out that there were duplicate (sometimes triplicate) data entries and hence the repeated messages & emails. Mystery solved. 🙂 Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583380 Share on other sites More sharing options...
benanamen Posted December 24, 2020 Share Posted December 24, 2020 1 hour ago, samanj said: there were duplicate (sometimes triplicate) data entries Sounds like someone hasn't set unique indexes on the relevant DB columns. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311916-emailing-twice-error/#findComment-1583382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.