2

I am currently having this issue on a MediaTemple Grid server.

I am using PHP Mailer and I have the following code:

require_once("inc/class.phpmailer.php");

// Mail from contact form for me

$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> CharSet = "UTF-8";
$mail -> SetFrom($_POST["email"],$_POST["name"]);
$mail -> AddReplyTo($_POST["email"],$_POST["name"]);
$mail -> AddBCC("myaddress@mydomain.yep");

$mail -> Subject = "Mail Subject";
$body = "... some html ...";
$mail -> MsgHTML($body);

// Automatic mail for the person that contacted me

$mail_customer = new PHPMailer();
$mail_customer -> IsSMTP();
$mail_customer -> CharSet = "UTF-8";
$mail_customer -> SetFrom("contact@mydomain.yep","Contact");
$mail_customer -> AddReplyTo("contact@mydomain.yep","Contact");
$mail_customer -> AddAddress($_POST["email"],$_POST["name"]);

$mail_customer -> Subject = "Thank you for contacting us!";
$body_customer = "... some html ...";
$mail_customer -> MsgHTML($body_customer);

$mail -> Send();
$mail_customer -> Send(); 

When this is over there are no errors, but for some reason I get three e-mails.

  1. $mail - from $_POST["email"]
  2. $mail - from root@localhost - this has the exact same message body but without the $_POST data
  3. $mail_customer - that arrives to $_POST["email"] without other mails.

What could be causing this and how could I fix it? Does it have to do with PHP Mailer or with server configuration?

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41
  • Don't use the submitter's address as the from address - it wil fail with all yahoo and aol domains, and increasingly for others. Send from your web server's address. Also make sure you are using latest PHPMailer. In both cases you're calling `isSMTP`, but not setting a host, nor providing ay username or password properties. – Synchro Nov 02 '14 at 09:56
  • I have been using it like this since forever and it works without issue. The problem here is not the mail sending it's the fact that it sends 3 mails instead of 2, the third being a void e-mail sent from root@localhost. It's even stranger that it only sends for $mail and not for $mail_customer. – Mihail Minkov Nov 03 '14 at 15:32
  • Cut down your problem - do one thing at a time and isolate exactly where it's generating the extra message. Use a fake mail server (see PHPMailer docs for pointers) so you can see exactly what's being sent out. – Synchro Nov 03 '14 at 15:36
  • The one thing that's extremely weird is that in the beginning I had the mailer without isSMTP() and it was working fine... The next day all the contact forms stopped working and they started working again when I added the isSMTP() but the third "phantom" mail started being sent, could it be from the php mailer configuration or a server problem? – Mihail Minkov Nov 03 '14 at 19:22
  • Adding random bits of code is not a sane way of tracking down the problem. You want it to work because you're doing it right, not by blind luck. If you're going to use SMTP, configure it properly. Plenty of examples in the docs. – Synchro Nov 03 '14 at 21:36

1 Answers1

0

Try wrapping your first email in a method and use a if else construct to run the other

Look at my sample code below to get the idea of what i am proposing.

Try to write your code this way.

Notice: i have not tested this yet.

    require_once('../class.phpmailer.php');


   //MAIL SENDER 1
    $mail             = new PHPMailer(); 

    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);

    $mail->AddReplyTo("name@yourdomain.com","First Last");

    $mail->SetFrom('name@yourdomain.com', 'First Last');

    $mail->AddReplyTo("name@yourdomain.com","First Last");

    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");

    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail->MsgHTML($body);

    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      //RUN THE OTHER METHOD EMAIL
      mail_customer();
    }

//MAIL SENDER 2 WITHIN A METHOD

 function mail_customer(){   
    $mail             = new PHPMailer(); 

    $body             = file_get_contents('contents.html');
    $body             = eregi_replace("[\]",'',$body);

    $mail->AddReplyTo("name@yourdomain.com","First Last");

    $mail->SetFrom('name@yourdomain.com', 'First Last');

    $mail->AddReplyTo("name@yourdomain.com","First Last");

    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");

    $mail->Subject    = "PHPMailer Test Subject via mail(), basic";

    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

    $mail->MsgHTML($body);

    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      //RUN THE OTHER METHOD EMAIL
echo 'Mail Sent';
    }
}
Oluwaseye
  • 685
  • 8
  • 20
  • I did that, but the issue is not there, you see it doesn't give me an error message it does send the mails. The thing is that it sends one more which is unnecessary. I chatted with MT support and they weren't able to help me but apparently it might have to do something with sendmail. – Mihail Minkov Nov 01 '14 at 21:54