为什么Php Mailer不发送电子邮件如果发件人是Yahoo Mail

时间:2015-10-29 05:04:07

标签: php phpmailer

我正在使用php邮件发送电子邮件。但如果发件人是雅虎电子邮件地址,那么它就不会发送电子邮件。为什么?

我使用以下代码:

  require 'PHPMailerAutoload.php'; 
  $mail = new PHPMailer(true);
  try {
      //Set who the message is to be sent from
      $mail->setFrom("$email", "$name");  // yahoo mail address        
      //Set an alternative reply-to address
      //$mail->addReplyTo('replyto@example.com',  "$name");
      //Set who the message is to be sent to
      $mail->addAddress('receiver email adddress', 'Name');
      //Set the subject line
      $mail->Subject = "$subject";
      //Read an HTML message body from an external file, convert referenced images to embedded,
      //and convert the HTML into a basic plain-text alternative body
      $mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
      //Replace the plain text body with one created manually
      $mail->AltBody = 'This is a plain-text message body';
      //Attach an image file
      //$mail->addAttachment('images/phpmailer_mini.png');
      //send the message
      //Note that we don't need check the response from this because it will throw an exception if it has trouble
      $mail->send();
      echo "Message sent! = $email";
  } catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
  } catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
  }

1 个答案:

答案 0 :(得分:0)

目前您正在使用mail()(PHPMailer的默认行为)通过您自己的本地邮件服务器(不是Yahoo)发送邮件。

如果您从雅虎地址发送但未通过雅虎服务器发送,则由于雅虎的政策非常严格,您的邮件将无法通过SPF检查。

因此,您需要调用isSMTP()并将Host设置为yahoo SMTP服务器(并设置适当的身份验证,将您的代码基于PHPMailer提供的gmail示例,因为它非常类似的),或将您的地址更改为反映您实际发送的服务器或其SPF记录允许您的服务器成为邮件来源的地址。