使用 Gmail 账户和 PHPMailer 从 Heroku 服务器发送邮件

碧海醫心
发布: 2025-09-07 18:27:19
原创
473人浏览过

使用 gmail 账户和 phpmailer 从 heroku 服务器发送邮件

本文档旨在解决在使用 Heroku 应用程序通过 Gmail 账户和 PHPMailer 发送电子邮件时,邮件容易被标记为垃圾邮件的问题。我们将探讨根本原因,并提供一些可行的建议,以提高邮件的送达率,避免被垃圾邮件过滤器拦截。请注意,由于 Gmail 的安全策略限制,直接从 Heroku 服务器使用 Gmail 账户发送邮件存在固有挑战。

问题分析

在使用 Heroku 应用程序,通过 PHPMailer 和 Gmail 账户发送邮件时,邮件容易进入垃圾箱,主要原因在于:

  1. SPF、DKIM 和 DMARC 验证失败: Gmail 有严格的安全策略,会验证邮件的 SPF (Sender Policy Framework)、DKIM (DomainKeys Identified Mail) 和 DMARC (Domain-based Message Authentication, Reporting & Conformance) 记录。当邮件不是通过 Gmail 的服务器发送,而是通过 Heroku 服务器发送时,这些验证通常会失败,导致邮件被标记为垃圾邮件。
  2. 发件人地址与服务器不匹配: 邮件是从 your-app.herokuapp.com 发送的,但发件人地址是 @gmail.com。这种不匹配是垃圾邮件过滤器常见的判断依据。
  3. Heroku 动态 IP 地址: Heroku 使用动态 IP 地址,这些 IP 地址可能没有良好的声誉,容易被列入黑名单。

解决方案与建议

虽然不能完全保证邮件不被标记为垃圾邮件,但以下是一些可以尝试的方法来提高送达率:

1. 使用 Gmail 的 SMTP 服务器

确保 PHPMailer 配置正确,使用 Gmail 的 SMTP 服务器。以下是一个示例配置:

立即学习PHP免费学习笔记(深入)”;

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->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 <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}";
}
登录后复制

注意事项:

AppMall应用商店
AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店 56
查看详情 AppMall应用商店
  • 替换 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):

  1. 注册 SendGrid 账户: 在 SendGrid 官网注册一个账户。
  2. 获取 API 密钥: 在 SendGrid 控制台中获取 API 密钥。
  3. 安装 SendGrid PHP 库: 使用 Composer 安装 SendGrid PHP 库:composer require sendgrid/sendgrid
  4. 使用 SendGrid API 发送邮件:
<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line

$email = new \SendGrid\Mail\Mail();
$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", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$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中文网其它相关文章!

gmail邮箱
gmail邮箱

gmail邮箱是一款直观、高效、实用的电子邮件应用。免费提供15GB存储空间,可以永久保留重要的邮件、文件和图片,使用搜索快速、轻松地查找任何需要的内容,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号