简体   繁体   中英

'To' address being added twice while using PHPmailer

I am using PHPmailer to send mails. However, the mail is being sent to each user twice. Below is the screen-shot of a test mail. 在此处输入图片说明

The following is my code :

<?php

##REQUIRED FUNCTION
function send_mail_to($receiver, $msg)
{
    $subject="[ TNP Update ] - Do you like the new look ?";
    #Grab the PHPmailer class
    require_once('./lib/phpmailer/phpmailer.inc.php');

    #Create object
    $mailer = new PHPmailer(); //Instantiate class
    $mailer->From="tnp@aakashbhowmick.in";
    $mailer->FromName="TNP Mailer";
    $mailer->IsHTML(true);
    $mailer->Subject = $subject;
    $mailer->Body = $msg;
    $mailer->AddAddress($receiver);
    #Send the email
    set_time_limit(300);
    $mailer->Send();

} //End of send_mail_to()

    ###### THE WORKING CODE ######

#Only authorised access is allowed.
if($_POST['signature']=="some-secret-signature-here"){
    $msg1=urldecode($_POST['text']);

    # Formatting the message a little
    $msg1=str_replace("#c0c0c0","#EAE99A",$msg1);
    $msg1=str_replace("<td","<td style='font-family:Trebuchet MS,Verdana,arial'; ",$msg1);

    #Start sending mails. Some lines commented for testing purpose
    //include("connection.php");
    //$result=mysql_query("SELECT * FROM subscribers");
    $subscriber=array('id'=>'1','email'=>'aakashrocks@gmail.com','active'=>'1');
    //while($subscriber=mysql_fetch_array($result)){
        if($subscriber['active']==1){  

            $body="Some text";
                send_mail_to($subscriber['email'], $body); 

        }    #End-of-if
    //}   #End-of-while

} ##End of if
?>

I had the same problem, for me solution was to change

$mailer->isSMTP();

to

$mailer->Mailer   = 'smtp'; 

So, try to use $mailer->Mailer .

I was experiencing the same problem. In my case changing mail systems did the trick.

By default, phpMailer sends out email using Mail. Once I told it to use Sendmail instead, I stopped getting the duplicate address.

$email = new phpmailer;
$email->mailer = "sendmail";

May be there is a page refresh and hence multiple execution - Make sure the mail method is called only once by the browser for each reciever. To make sure, you could set a session variable upon sending and go into the send part only if session is not set.

  if(!isset($_SESSION[$reciever]))
    {
       $_SESSION[$reciever] = 1;
       \\mail code here
    }
  else{
        echo "doing it more than once";
     }

Also you can set $mailer->$SingleTo to true, so you would know if its multiple execution or single. You could also send the timestamp with the email for more debugging.

I had the same problem but using the SMTP connection. I still don't know why but it happens when your recipient doesn't have a name. So, instead of

$mailer->AddAddress($receiver);

Do

$mailer->AddAddress($receiver, 'Receiver name');

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM