
本文档旨在解决在使用 Heroku 应用程序通过 Gmail 账户和 PHPMailer 发送电子邮件时,邮件容易被标记为垃圾邮件的问题。我们将探讨根本原因,并提供一些可行的建议,以提高邮件的送达率,避免被垃圾邮件过滤器拦截。请注意,由于 Gmail 的安全策略限制,直接从 Heroku 服务器使用 Gmail 账户发送邮件存在固有挑战。
问题分析
在使用 Heroku 应用程序,通过 PHPMailer 和 Gmail 账户发送邮件时,邮件容易进入垃圾箱,主要原因在于:
SPF、DKIM 和 DMARC 验证失败: Gmail 有严格的安全策略,会验证邮件的 SPF (Sender Policy Framework)、DKIM (DomainKeys Identified Mail) 和 DMARC (Domain-based Message Authentication, Reporting & Conformance) 记录。当邮件不是通过 Gmail 的服务器发送,而是通过 Heroku 服务器发送时,这些验证通常会失败,导致邮件被标记为垃圾邮件。发件人地址与服务器不匹配: 邮件是从 your-app.herokuapp.com 发送的,但发件人地址是 @gmail.com。这种不匹配是垃圾邮件过滤器常见的判断依据。Heroku 动态 IP 地址: Heroku 使用动态 IP 地址,这些 IP 地址可能没有良好的声誉,容易被列入黑名单。
解决方案与建议
虽然不能完全保证邮件不被标记为垃圾邮件,但以下是一些可以尝试的方法来提高送达率:
1. 使用 Gmail 的 SMTP 服务器
确保 PHPMailer 配置正确,使用 Gmail 的 SMTP 服务器。以下是一个示例配置:
立即学习“PHP免费学习笔记(深入)”;
SMTPDebug = SMTP::DEBUG_OFF; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = 'your_email@gmail.com'; //SMTP username $mail->Password = 'your_gmail_password'; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail->setFrom('your_email@gmail.com', 'Your Name'); $mail->addAddress('recipient@example.com', 'Recipient Name'); //Add a recipient //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body in bold!'; $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}";}
注意事项:
替换 your_email@gmail.com 和 your_gmail_password 为你的 Gmail 账户和密码。如果启用了 Gmail 的两步验证,需要生成一个应用专用密码,并使用该密码代替 Gmail 账户密码。确保已安装 PHPMailer 库。可以使用 Composer 安装:composer require phpmailer/phpmailer
2. 检查 Gmail 账户设置
允许安全性较低的应用访问: Gmail 默认会阻止安全性较低的应用访问你的账户。你需要在 Gmail 设置中启用“允许安全性较低的应用访问”。注意: 强烈建议不要使用此方法,因为它会降低账户的安全性。如果必须使用,请在使用完毕后立即禁用。启用 IMAP/SMTP: 确保你的 Gmail 账户已启用 IMAP 和 SMTP。
3. 避免垃圾邮件特征
使用真实的 From 地址: 尽量使用与你的 Heroku 应用程序相关的域名,而不是直接使用 @gmail.com。虽然在 Heroku 上没有自定义域名的情况下比较困难,但可以考虑使用 Gmail 的别名功能,并使用该别名作为发件人地址。避免使用垃圾邮件触发词: 检查邮件内容,避免使用常见的垃圾邮件触发词,例如“免费”、“折扣”、“立即购买”等。提供取消订阅链接: 在邮件底部添加取消订阅链接,让收件人可以选择不再接收邮件。保持邮件内容简洁明了: 避免使用过多的图片和链接,保持邮件内容简洁明了。使用文本格式的备用版本: 为 HTML 邮件提供纯文本格式的备用版本,以便在不支持 HTML 的邮件客户端中也能正常显示。
4. 使用第三方邮件服务
如果以上方法仍然无法解决问题,可以考虑使用第三方邮件服务,例如 SendGrid、Mailgun 或 Amazon SES。这些服务专门用于发送事务性邮件,通常具有更高的送达率和更好的信誉。它们通常提供免费套餐,可以满足小型项目的需求。
示例 (使用 SendGrid):
注册 SendGrid 账户: 在 SendGrid 官网注册一个账户。获取 API 密钥: 在 SendGrid 控制台中获取 API 密钥。安装 SendGrid PHP 库: 使用 Composer 安装 SendGrid PHP 库:composer require sendgrid/sendgrid使用 SendGrid API 发送邮件:
<?phprequire 'vendor/autoload.php'; // If you're using Composer (recommended)// Comment out the above line if not using Composer// require("/sendgrid-php.php");// If not using Composer, uncomment the above line$email = new SendGridMailMail();$email->setFrom("your_email@example.com", "Your Name");$email->setSubject("Sending with SendGrid is Fun");$email->addTo("recipient@example.com", "Recipient Name");$email->addContent( "text/plain", "and easy to do anywhere, even with PHP");$email->addContent( "text/html", "and easy to do anywhere, even with PHP");$sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));try { $response = $sendgrid->send($email); print $response->statusCode() . "n"; print_r($response->headers()); print $response->body() . "n";} catch (Exception $e) { echo 'Caught exception: '. $e->getMessage() ."n";}
注意事项:
替换 your_email@example.com 和 recipient@example.com 为你的发件人和收件人地址。将 SENDGRID_API_KEY 环境变量设置为你的 SendGrid API 密钥。 在 Heroku 中,可以使用 heroku config:set SENDGRID_API_KEY=YOUR_API_KEY 命令设置环境变量。
总结
虽然直接从 Heroku 服务器使用 Gmail 账户发送邮件存在挑战,但通过正确配置 PHPMailer、检查 Gmail 账户设置、避免垃圾邮件特征和使用第三方邮件服务,可以显著提高邮件的送达率。建议优先考虑使用第三方邮件服务,因为它们专门用于发送事务性邮件,具有更高的可靠性和更好的信誉。
以上就是使用 Gmail 账户和 PHPMailer 从 Heroku 服务器发送邮件的详细内容,更多请关注php中文网其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/81358.html
微信扫一扫
支付宝扫一扫