php 邮件不使用 SMTP 通过 SSL 通过 PHPMailer 发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18535294/
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
Mail not sending with PHPMailer over SSL using SMTP
提问by James Cushing
I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
我正在尝试使用 PHPMailer 通过 SMTP 发送电子邮件,但到目前为止还没有运气。我经历了许多 SO 问题、PHPMailer 教程和论坛帖子,但仍然无法使其正常工作。我将尽可能多地记录我的失败尝试以节省时间,但首先这是我正在使用的代码:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "[email protected]"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('[email protected]', 'James Cushing');
$mail->AddReplyTo("[email protected]","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "[email protected]";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: [email protected] : Called Mail() without being connected
Mailer Error: The following From address failed: [email protected] : Called Mail() without being connected
首先,当我现在运行此代码时,出现两个不同的错误。在我的本地服务器上,我收到错误消息:
SMTP -> 错误:无法连接到服务器:操作超时(60)
以下发件人地址失败:[email protected]:未连接
邮件就调用了邮件()邮件错误:以下发件人地址失败:[email protected]:在未连接的情况下调用 Mail()
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
我在我的 Web 服务器上运行相同的代码时遇到更多或同样的错误,但第一行是:
SMTP -> 错误:无法连接到服务器:网络无法访问 (101)
Obviously it's worth pointing out that I'm not using the literal "[email protected]" but I've substituted my own email out for this post.
显然值得指出的是,我没有使用文字“[email protected]”,但我已经用自己的电子邮件代替了这篇文章。
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "@gmail.com" from the Username property
- A number of other things I can't remember
我尝试过的事情
- 使用 iCloud SMTP 服务器
- 使用不同的端口
- 在我的 php.ini 文件中启用 OpenSSL 扩展
- 从各种 PHPMailer 示例中复制代码
- 使用 Google 的“DisplayUnlockCaptcha”系统启用连接
- 发送到不同的地址 - 从用户名属性中删除“@gmail.com” - 还有一些我不记得的事情
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
这已经让我发疯了大约一天,所以如果有人能解决它,他们将成为英雄。
Thanks
谢谢
回答by ybert
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "[email protected]";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
这是一个工作配置。
try to replace what you have
尝试替换你所拥有的
回答by Asuquo12
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.
不要在端口 465 上使用 SSL,它自 1998 年以来已被弃用,仅用于未获得备忘录的 Microsoft 产品;改为在端口 587 上使用 TLS:因此,下面的代码应该非常适合您。
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the
回答by jacob21
Firstly, use these settings for Google:
首先,为 Google 使用这些设置:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
但是,您设置了什么防火墙?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
如果您要过滤掉 TCP 端口 465/995,也许是 587,您需要配置一些例外或将它们从规则列表中删除。
回答by user1533634
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
每当我的客户端机器更改网络连接(例如,家庭与办公室网络)并且以某种方式重新启动网络服务(或重新启动机器)为我解决问题时,我都会遇到类似的 SMTP 故障。不确定这是否适用于您的情况,但以防万一。
sudo /etc/init.d/networking restart # for ubuntu
回答by Andre Mesquita
First, Google created the "use less secure accounts method" function:
首先,谷歌创建了“使用不太安全的帐户方法”功能:
https://myaccount.google.com/security
https://myaccount.google.com/security
Then created the another permission:
然后创建了另一个权限:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.
希望能帮助到你。