首页 > 解决方案 > 与用于 smtp 的邮件相比,发送具有不同地址的邮件

问题描述

我正在尝试使用 phpmailer 发送邮件。邮件正在发送,但我无法更改发件人地址。


try {
    //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
    $mail->isSMTP();                                            // Set mailer to use SMTP
    $mail->Host       = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'abc@gmail.com';                     // SMTP username
    $mail->Password   = '****';                               // SMTP password
    $mail->SMTPSecure = 'tls';                                  // Enable TLS encryption, `ssl` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('xyz@yahoo.com');
    $mail->addAddress('lmn@gmail.com' );     // Add a recipient
    // $mail->addAddress('ellen@example.com');               // Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    // Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

发送邮件时,发件人地址恰好是 abc@gmail.com,即 smtp 设置中使用的地址,而不是我在 mail->setFrom() 中输入的地址。我该如何解决这个问题?

标签: phpphpmailer

解决方案


这是 PHPMailer 文档中涵盖的众所周知的问题 - gmail 不允许您从任意地址发送,因为它是伪造的。最重要的是,雅虎的 DMARC 政策设置为“拒绝”,因此即使 gmail 允许您从雅虎地址发送邮件,任何接收服务器都会拒绝该邮件。

所以这里的答案是,不,你不能这样做。如果您想雅虎地址发送,您必须通过雅虎服务器发送。


推荐阅读