Jump to content

Emailing twice error


Go to solution Solved by samanj,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/311916-emailing-twice-error/
Share on other sites

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.