我使用php mailer发送电子邮件。但如果发件人是雅虎的电子邮件地址,那么它就不会发送电子邮件。为什么?
我正在使用以下代码:
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!
}
目前,您使用mail()
(PHPMailer的默认行为)通过您自己的本地邮件服务器发送邮件,而不是Yahoo。
如果你从雅虎地址发送,但不是通过雅虎服务器发送,你的消息将无法通过SPF检查,因为雅虎有非常严格的政策。
因此,您需要调用isSMTP()
并将Host
设置为yahoo SMTP服务器(并设置适当的auth,将代码基于PHPMailer提供的gmail示例,因为它非常相似),或者将您的from地址更改为反映您真正发送的服务器,或者其SPF记录允许您的服务器成为邮件源的地址。