johngreen
johngreen

Reputation: 2744

'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. enter image description here

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="[email protected]";
    $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'=>'[email protected]','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
?>

Upvotes: 2

Views: 1948

Answers (4)

Viacheslav Bakshaev
Viacheslav Bakshaev

Reputation: 272

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

$mailer->isSMTP();

to

$mailer->Mailer   = 'smtp'; 

So, try to use $mailer->Mailer.

Upvotes: 1

doub1ejack
doub1ejack

Reputation: 11171

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";

Upvotes: 0

Ivan Gasparetto
Ivan Gasparetto

Reputation: 9

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.

Upvotes: 0

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

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.

Upvotes: 0

Related Questions