PHP Mailer使用root @ localhost发送1个额外的空邮件消息

时间:2014-11-01 03:22:18

标签: php smtp sendmail phpmailer mediatemple

我目前在MediaTemple Grid服务器上遇到此问题。

我正在使用PHP Mailer,我有以下代码:

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(); 

当结束时没有错误,但由于某种原因,我收到三封电子邮件。

  1. $ mail - 来自$ _POST [“email”]
  2. $ mail - 来自root @ localhost - 它具有完全相同的邮件正文但没有$ _POST数据
  3. $ mail_customer - 在没有其他邮件的情况下到达$ _POST [“email”]。
  4. 可能导致此问题的原因以及如何解决?它与PHP Mailer或服务器配置有关吗?

1 个答案:

答案 0 :(得分:0)

尝试在方法中包装您的第一封电子邮件,并使用if else结构运行其他

请查看下面的示例代码,了解我的建议。

尝试以这种方式编写代码。

注意:我还没有测试过。

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