tags:

views:

271

answers:

5

I've used two PHP email scripts and routing it through my SMTP server, when I do this though it sends two of the same email.

When I use mail() this doesn't happen, but I'd much rather use SMTP.

Any ideas why this may be occuring?

+1  A: 

If you're setting the 'To' and/or 'Recipient' header multiple times, the SMTP server could interpret that as separate e-mail address, thus you'll receive the multiple e-mails.

I'd recommend using the PEAR Mail class. Very simple to use, and handles much of the work for you. It supports multiple backends including SMTP. Likewise, if you want to expand your class to send HTML emails, the Mail_Mime class handles this very nicely, providing methods to set the plain-text body and the HTML body (in case the recipient doesn't support HTML).

drowe
A: 
function send_email($from, $fromname, $to, $subject, $body, $alt = '')
{
    require_once('class.phpmailer.php');
    //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

    $mail->IsSMTP(); // telling the class to use SMTP

    try
    {
     $mail->Host       = 'localhost'; // SMTP server
     $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
     //$mail->AddReplyTo($from, $fromname);
     $mail->AddAddress($to);
     $mail->SetFrom($from, $fromname);
     $mail->Subject = $subject;
     //$mail->AltBody = $alt; // optional - MsgHTML will create an alternate automatically
     $mail->MsgHTML($body);
     $mail->Send();
     echo 'Message Sent OK';
    }
    catch (phpmailerException $e)
    {
     echo $e->errorMessage(); //Pretty error messages from PHPMailer
    }
    catch (Exception $e)
    {
     echo $e->getMessage(); //Boring error messages from anything else!
    }
}

That's the current function so far

James
You might want to post this as an edit to the original post, rather than as an 'answer' to your question.
cmptrgeekken
A: 

So if you're only using PHPMailer without editing it's code, it's not your script's fault. Maybe check your SMTP server's configuration?

Daniel S
A: 

Based on your code, if it's the class which is at fault, you'd expect to get 'Message Sent OK' twice (I can't see why that would happen though). If you don't, then I'd be looking at your SMTP server (maybe via a call to support).

I'm assuming you've disabled Reply-to to rule it out as a cause in this case? Note: I'm not suggesting that would affect anything (other than you likely being classified as spam).

Incidentally, I moved from PHPMailer to Swift Mailer some time ago & have never looked back. If you don't get any joy from support then I'd try at least testing with Swift Mailer.

da5id
A: 

I agree with what da5id said, why dont you take the second error message out. Further have you checked the receiver whether they REALLY get 2 messages?