简体   繁体   中英

PHP Mailer sending 1 extra empty mail message with root@localhost

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?

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';
    }
}

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