php - 与用于 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() 中输入的地址。我该如何解决这个问题?
解决方案
这是 PHPMailer 文档中涵盖的众所周知的问题 - gmail 不允许您从任意地址发送,因为它是伪造的。最重要的是,雅虎的 DMARC 政策设置为“拒绝”,因此即使 gmail 允许您从雅虎地址发送邮件,任何接收服务器都会拒绝该邮件。
所以这里的答案是,不,你不能这样做。如果您想从雅虎地址发送,您必须通过雅虎服务器发送。
推荐阅读
- terraform - 我可以在 Terraform Cloud 上使用 Terragrunt
- datepicker - Materializecss date picker is not working
- git - Looking for folders containing bare git repositories
- mysql - Typo3 LTS9 学说:如何使用 mysql @-Variables?
- elasticsearch - bool 查询不支持 [search_after] -- 弹性搜索错误
- authentication - npm login --always-auth doesn't add the appropriate entry to the config
- python - How do I read a CSV file using Pandas then split the columns into two Numpy arrays using to_numpy for Scikit-Learn?
- azerothcore - 在游戏连接中禁用并允许创建角色 azerothcore
- android - how to delete (photo ,video , file) in android 9
- acumatica - 如何在网格视图委托中覆盖账单和调整屏幕 AddPOReceipt 按钮