
本文档旨在解决在使用 Heroku 应用程序通过 Gmail 账户和 PHPMailer 发送电子邮件时,邮件容易被标记为垃圾邮件的问题。我们将探讨根本原因,并提供一些可行的建议,以提高邮件的送达率,避免被垃圾邮件过滤器拦截。请注意,由于 Gmail 的安全策略限制,直接从 Heroku 服务器使用 Gmail 账户发送邮件存在固有挑战。
在使用 Heroku 应用程序,通过 PHPMailer 和 Gmail 账户发送邮件时,邮件容易进入垃圾箱,主要原因在于:
虽然不能完全保证邮件不被标记为垃圾邮件,但以下是一些可以尝试的方法来提高送达率:
确保 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}";
}注意事项:
如果以上方法仍然无法解决问题,可以考虑使用第三方邮件服务,例如 SendGrid、Mailgun 或 Amazon SES。这些服务专门用于发送事务性邮件,通常具有更高的送达率和更好的信誉。它们通常提供免费套餐,可以满足小型项目的需求。
示例 (使用 SendGrid):
<?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";
}注意事项:
虽然直接从 Heroku 服务器使用 Gmail 账户发送邮件存在挑战,但通过正确配置 PHPMailer、检查 Gmail 账户设置、避免垃圾邮件特征和使用第三方邮件服务,可以显著提高邮件的送达率。建议优先考虑使用第三方邮件服务,因为它们专门用于发送事务性邮件,具有更高的可靠性和更好的信誉。
以上就是使用 Gmail 账户和 PHPMailer 从 Heroku 服务器发送邮件的详细内容,更多请关注php中文网其它相关文章!
gmail邮箱是一款直观、高效、实用的电子邮件应用。免费提供15GB存储空间,可以永久保留重要的邮件、文件和图片,使用搜索快速、轻松地查找任何需要的内容,有需要的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号