php PHPMailer 不工作:无法发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10981968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPMailer not working: Message could not be sent
提问by AnchovyLegend
I am trying to create a contact form on my website using PHPMailer. I am having some trouble setting it up. I am trying to use G-mail as my smtp host. I was wondering if anyone can help troubleshoot this?
我正在尝试使用 PHPMailer 在我的网站上创建联系表单。我在设置时遇到了一些麻烦。我正在尝试使用 G-mail 作为我的 smtp 主机。我想知道是否有人可以帮助解决这个问题?
This is my mailer code:
这是我的邮件代码:
<?php
require("class.phpmailer.php");
require("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 467;
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "workingpassword"; // SMTP password
$mail->From = "[email protected]";
$mail->FromName = "Mailer";
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->AddAddress("[email protected]"); // name is optional
$mail->AddReplyTo("[email protected]", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
// $mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
// $mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$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";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
The error message:
错误信息:
Message could not be sent.
Mailer Error: The following From address failed: [email protected]
回答by technoTarek
Have you looked at and tried the info from this Q?
您是否查看并尝试过此 Q 中的信息?
PHPMailer: SMTP Error: Could not connect to SMTP host
PHPMailer:SMTP 错误:无法连接到 SMTP 主机
In particular, does this provide any additional info?
特别是,这是否提供任何其他信息?
$mail->SMTPDebug = 1;
回答by Mike Mackintosh
smtp.gmail.com requires that you use SSL and port 587 or 465.
smtp.gmail.com 要求您使用 SSL 和端口 587 或 465。
See their configuration page: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
查看他们的配置页面:http: //support.google.com/mail/bin/answer.py?hl=zh-CN&answer=13287
回答by Kh?i
Are you running PHP on Windows? Then this might help:
你在 Windows 上运行 PHP 吗?那么这可能会有所帮助:
http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html
http://www.devcha.com/2010/01/php-fsockopen-unable-to-connect-ssl.html
回答by Matiss
Got the same error and the problem was that i was trying to sent email from "boy333@in**" account pretending as "girl333@in**". I just changed
遇到了同样的错误,问题是我试图从假装为“girl333@in**”的“boy333@in**”帐户发送电子邮件。我刚改变
$mail->From = 'girl333@in**'
to username i was actually connecting. So I changed to:
到我实际连接的用户名。所以我改为:
$mail->From = 'boy333@in**'
The idea is that these two fields is with the same usernames.
这个想法是这两个字段具有相同的用户名。
$mail->Username = "boy333";
$mail->From = 'boy333@in**';
回答by Ajitabh Ranjan
<?php
require_once('class.phpmailer.php');
include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "[email protected]"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('[email protected]', 'abc');
$mail->SetFrom('[email protected]', 'def');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Go to google settings and enable 'less secure' applications. It worked for me.
转到谷歌设置并启用“不太安全”的应用程序。它对我有用。
回答by Digelim
ping smtp.gmail.com
For some reason, google redirects SMTP requests to gmail-smtp-msa.l.google.com
出于某种原因,谷歌将 SMTP 请求重定向到 gmail-smtp-msa.l.google.com
PING gmail-smtp-msa.l.google.com (74.125.133.108): 56 data bytes
64 bytes from 74.125.133.108: icmp_seq=0 ttl=43 time=72.636 ms
64 bytes from 74.125.133.108: icmp_seq=1 ttl=43 time=68.841 ms
64 bytes from 74.125.133.108: icmp_seq=2 ttl=43 time=68.500 ms
So you should put the final destination in your config, as PHPMailer does not follow redirections. Also you may try to send emails without SMTPSecure by keeping that field blank.
所以你应该把最终目的地放在你的配置中,因为 PHPMailer 不遵循重定向。您也可以尝试通过将该字段留空来发送没有 SMTPSecure 的电子邮件。
$mail->Host = 'gmail-smtp-msa.l.google.com';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPSecure = '';
$mail->Port = 25;
回答by F. Yasar
If your email in gsuite check two steps: https://support.google.com/a/answer/6260879?hl=enhttps://accounts.google.com/DisplayUnlockCaptcha
如果您在 gsuite 中的电子邮件检查两个步骤:https: //support.google.com/a/answer/6260879 ? hl =en https://accounts.google.com/DisplayUnlockCaptcha
Php Mail Settings; PHPMailer version 5.2.22 Check class.phpmailer.php,
PHP 邮件设置;PHPMailer 版本 5.2.22 检查 class.phpmailer.php,
var $Host = 'smtp.gmail.com';
var $Port = 465;
var $Helo ='domain.net';
public $SMTPSecure = 'ssl';
public $SMTPAutoTLS = true;
public $SMTPAuth = true;
public $SMTPOptions = array();
public $Username = 'gmail-username';//or domain credentials on google mail
public $Password = 'gmail-password';
public $Timeout = 10;
public $SMTPDebug = true;
mail send php
邮件发送php
<?php
require('PHPMailer/class.phpmailer.php');
require('PHPMailer/class.smtp.php');
$headers = "Content-Type: text/html; charset=UTF-8";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="SET NAMES UTF8";
$mail->SMTPDebug = 0;
$mail->CharSet="utf8";
$mail->Debugoutput = 'html';
$mail->host = "smtp.gmail.com";
$mail->port = 465;
$mail->smtpauth = true;
$mail->smtpsecure = 'ssl';
$mail->username = "gmail-username"; //or domain credentials on google mail
$mail->password = "gmail-password";
$mail->SetFrom('from-email', 'from-name');
$mail->AddAddress('to-address', 'to-address');
$mail->Body = 'your-text'; //emailbody
if(!$mail->Send())
{
mysql_close();
echo "<script>alert('Error: " . $mail->ErrorInfo."');</script>";
}
else
{
mysql_close();
echo "<script>alert('success');</script>";
}
?>
This is work for me. Hope it helps.
这对我来说是工作。希望能帮助到你。

