2019 使用 Gmail 更新 phpMailer
我知道这是一个老问题,但它仍然出现在谷歌中,我需要更新这个答案。
IsSMTP()
如果您在使用 phpmailer 时遇到问题(很多人会遇到),它仅在您尝试使用 gmail 的 SMTP时注释掉时才有效,那么这就是原因。
当您注释掉时,IsSMTP()
您是在告诉 phpmailer 不要使用 SMTP,默认情况下 phpmailer 会将请求发送到您的本地mail()
。如果您查看此时发送的电子邮件并查看电子邮件的标题,您会发现它来自您的本地服务器,而不是您尝试发送的地址/域。所以是的,注释掉IsSMTP()
会让它起作用,但事实并非如此。从未正确设置的本地服务器发送很可能会导致您的电子邮件成为垃圾邮件。
那么我该如何解决这个问题
简单明了,您很可能使用的是旧版本的 phpmailer,您需要更新版本。说明这一点的简单方法是您如何设置From
地址。如果它看起来像这样,$mail->From = "name@example.com"
那么您使用的是旧版本。
最新版本的 phpmailer 将 From 定义为$mail->setFrom("name@example.com", "First Last")
. 如果你看到了,那么你正在使用更新版本的 phpmailer。
如何正确地做到这一点并使其真正发挥作用
请确保您的防火墙上有 587 的 TCP OUT 端口
SMTP Gmail 仅适用于 tls/587 而不是 ssl/465(ssl 是 1990 年代)
确保您在 gmail 中正确设置了允许安全性较低的应用程序。如果您使用的是 G Suite 帐户,那么您必须让您的管理员启用它(如果尚未启用)。
这是一个完美的例子,如何将新的 phpmailer 与 gmail smtp 一起使用(是的,它确实有效,如果没有,那么你的结果有问题)
如何安装phpmailer
先下载最新版的phpmailer
有两种安装方法。作曲家或手册。您所需要的手动方式是
use PHPMailer\PHPMailer\PHPMailer; <-- make sure these are not in a function
use PHPMailer\PHPMailer\Exception;
require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';
例子
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/src/Exception.php';
require 'path/src/PHPMailer.php';
require 'path/src/SMTP.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}
//Section 2: IMAP
//IMAP commands requires the PHP IMAP Extension, found at: https://php.net/manual/en/imap.setup.php
//Function to call which uses the PHP imap_*() functions to save messages: https://php.net/manual/en/book.imap.php
//You can use imap_getmailboxes($imapStream, '/imap/ssl') to get a list of available folders or labels, this can
//be useful if you are trying to get this working on a non-Gmail IMAP server.
function save_mail($mail)
{
//You can change 'Sent Mail' to any other folder or tag
$path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open($path, $mail->Username, $mail->Password);
$result = imap_append($imapStream, $path, $mail->getSentMIMEMessage());
imap_close($imapStream);
return $result;
}
?>