我正在尝试使用 phpmailer 发送电子邮件。这是我写的代码。
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = '*********';
$mail->Port = 25;
$mail->From = '[email protected]';
$mail->FromName = 'Shamir Towsif';
$mail->addAddress('[email protected]', 'Shamir Towsif');
$mail->addReplyTo('[email protected]', 'Information');
$mail->isHTML(true);
$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';
if(!$mail->send()) {
echo "Message could not be sent.\n";
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
这是我收到的错误。
Message could not be sent. Mailer Error: SMTP connect() failed.
我做错了什么?
我面临着类似的问题,但我认为你应该尝试将其添加到你的代码中:
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
这是 PHPMailer 推荐的 GMail 设置,您可以在他们的 Github 页面中看到 示例 .
标题:使用 PhpMailer 和 Gmail 从服务器发送电子邮件
这是我解决类似问题的方法:
从 github (https://github.com/PHPMailer/PHPMailer) 下载 PHPMAILER 到您的计算机。
将其作为 .zip 文件上传到您的服务器,然后通过单击解压图标将其解压。将文件夹重命名为“phpmailer”或任何您想要的名称。
在邮件发送.php文件中放入以下代码:
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->setFrom('[email protected]', 'Name');
$mail->addAddress('[email protected]', 'Name');
$mail->Subject = 'Subject';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
保存 php 文件。
打开浏览器并登录您在 php 脚本中使用的 gmail(google) 帐户。
展开您的设备或应用程序可能不支持 Google 的安全标准。
单击安全性较低的应用程序。
切换允许安全性较低的应用程序:至开(如果为关)。
展开应用程序不支持两步验证
点击继续
在浏览器中打开发送php文件的邮件。
现在电子邮件将被发送。
这个技巧对我有用。我希望它也对你们有用。
快乐编码:-)
通过将这些行添加到标准 PHPMailer 配置中,解决了几乎相同的问题。就像魅力一样。
$mail->SMTPKeepAlive = true;
$mail->Mailer = “smtp”; // don't change the quotes!
遵循使用 TLS 的 SMTP 的所有标准设置以及端口等的默认设置。最终,我在研究解决方案时遇到了这段代码(来自 Simon Chen),https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#评论-89