php Phpmailer 在 Gmail 中使用 smtp 不起作用 - 连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19216335/
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 using smtp with Gmail not working - connection timing out
提问by redber2009
I've looked into the following links:
我查看了以下链接:
phpmailer send gmail smtp timeout
send email using Gmail SMTP server through PHP Mailer
通过 PHP Mailer 使用 Gmail SMTP 服务器发送电子邮件
http://uly.me/phpmailer-and-gmail-smtp/
http://uly.me/phpmailer-and-gmail-smtp/
...and tried to implement for myself a combination of those however...most of the time it sends this message...
...并试图为自己实现这些的组合,但是......大多数时候它发送这条消息......
Message could not be sent.
Mailer Error: SMTP connect() failed.
无法发送消息。
邮件程序错误:SMTP connect() 失败。
However there was one time where it sent this when I experimented between "tls" and "ssl"...
然而,当我在“tls”和“ssl”之间进行试验时,有一次它发送了这个......
SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.
Mailer Error: SMTP connect() failed.
SMTP 错误:无法连接到服务器:连接超时 (110) SMTP connect() 失败。无法发送消息。
邮件程序错误:SMTP connect() 失败。
My code is attached...did I somehow miss something? I asked the web hosting service if they're blocking and gave them a template of my code - they said the server allows connections to Gmail's SMTP.
我附上了我的代码……我是不是错过了什么?我问网络托管服务他们是否阻止并给了他们我的代码模板 - 他们说服务器允许连接到 Gmail 的 SMTP。
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = 'true';
$mail -> SMTPSecure = 'tls';
$mail -> SMTPKeepAlive = true;
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> IsHTML(true);
$mail -> Username = "[email protected]";
$mail -> Password = "mypassword";
$mail -> SingleTo = true;
$to = xxx;
$from = xxx;
$fromname = xxx;
$subject = xxx;
$message = xxx
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$mail -> From = $from;
$mail -> FromName = $fromname;
$mail -> AddAddress($to);
$mail -> Subject = $subject;
$mail -> Body = $message;
if(!$mail -> Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail-> ErrorInfo;
exit;
}
回答by Casey
I dug into it. Use fsocketopen, which is native to php, to test the connection and eliminate most of the potential problems. Write a simple php page with this:
我深入研究了它。使用php原生的fsocketopen测试连接,排除大部分潜在问题。用这个写一个简单的php页面:
$host = "smtp.gmail.com";
$port = "587";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}
You should get back "ok". If not you know you have a connection problem that has nothing to do with Phpmailer. If that's the case it's probably the hosting company. If not then it's probably something simple about the difference between your local host and the hosting company like different versions of php.
你应该回复“好的”。如果不是,您知道您遇到了与 Phpmailer 无关的连接问题。如果是这种情况,则可能是托管公司。如果不是,那么您的本地主机和托管公司之间的区别可能很简单,例如不同版本的 php。
I suspect though that this script won't make the connection
我怀疑这个脚本不会建立连接
回答by Krimson
Use ssl
使用 ssl
$mail -> SMTPSecure = 'ssl';
Port should be 465
端口应该是 465
$mail -> Port = 465;
Change your host to:
将您的主机更改为:
$mail -> Host = 'ssl://smtp.gmail.com';
Hopefully it works
希望它有效
回答by Larry K
I had this same problem and solved it:
我遇到了同样的问题并解决了它:
First, turn on smtp error logging in phpmailer:
首先,在phpmailer中开启smtp错误日志:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
Then retry your phpmailer email send. You will see the entire SMTP conversation on standard error output. If you're using a web server, look in the web server log file.
然后重试您的 phpmailer 电子邮件发送。您将在标准错误输出中看到整个 SMTP 对话。如果您使用的是 Web 服务器,请查看 Web 服务器日志文件。
I could then see the error response from gmail. Gmail was not accepting the login.
然后我可以看到来自 gmail 的错误响应。Gmail 不接受登录。
The error within the smtp conversation refers to an article. It gives some tips:
smtp 对话中的错误是指一篇文章。它给出了一些提示:
- Allow less secure apps to use your account.
- Login to gmail from a web browser.
- Confirm the gmail login captcha. (It may not actually display a captcha to you, but this was the additional step that suddenly allowed my email to go through.)
- 允许安全性较低的应用程序使用您的帐户。
- 从网络浏览器登录到 Gmail。
- 确认gmail 登录验证码。(它实际上可能不会向您显示验证码,但这是突然允许我的电子邮件通过的附加步骤。)
回答by Synchro
This question has many duplicates, so here's a canned answer:
这个问题有很多重复,所以这是一个罐头答案:
- Base your code on the gmail example provided with PHPMailer
- Check out the troubleshooting docs
- Be aware of this issue, related to what Larry K said.
- 将您的代码基于PHPMailer 提供的 gmail 示例
- 查看故障排除文档
- 请注意这个问题,与 Larry K 所说的有关。
回答by Casey
Check to make sure you can reach gmail from your webhost. I'm assuming it's linux. SSH in and on the command line type
检查以确保您可以从您的虚拟主机访问 gmail。我假设它是Linux。SSH 输入和在命令行输入
telnet smtp.gmail.com 587
You should get back
你应该回来
Connected to smtp.something
It has to be a configuration difference between localhost and your provider
它必须是本地主机和您的提供者之间的配置差异
回答by Rafael Fix
That might probably be Gmail blocking your access.
这可能是 Gmail 阻止了您的访问。
Go to your security configurations and see if it's blocking any access..
转到您的安全配置,看看它是否阻止任何访问..
Or try to change your password and try again.
或者尝试更改密码并重试。
回答by Morgan Green
Add
添加
date_default_timezone_set('Etc/UTC');
before including the autoloader. SMTP Needs to have the timezone set which was my issue.
在包括自动加载器之前。SMTP 需要设置时区,这是我的问题。
回答by Newtron
Download sendmail for Windows from http://www.glob.com.au/sendmail/sendmail.zipCopy sendmail.exe and sendmail.ini into C:/usr/lib/
从http://www.glob.com.au/sendmail/sendmail.zip下载 Windows 版 sendmail 将 sendmail.exe 和 sendmail.ini 复制到 C:/usr/lib/
Edit sendmail.ini and enter your mail account credentials.
编辑 sendmail.ini 并输入您的邮件帐户凭据。
You might want to configure these 2 fields as well (or sending may not work) [email protected] [email protected] By the way I uncommented debug_logfile so I can see what data is being sent to my SMTP server.
您可能还想配置这 2 个字段(或发送可能不起作用) [email protected] [email protected] 顺便说一下,我取消了 debug_logfile 的注释,以便我可以看到哪些数据正在发送到我的SMTP 服务器。
edit c:\php\php.ini
编辑 c:\php\php.ini
sendmail_from = [email protected]
sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). sendmail_path = C:/usr/lib/sendmail.exe -t -i
; 仅适用于 Unix。您也可以提供参数(默认值:“sendmail -t -i”)。sendmail_path = C:/usr/lib/sendmail.exe -t -i
Restart apache Start sendmail.exe either from [Start] > Run > C:/usr/lib/sendmail.exe or Go to C:/usr/lib in Windows Explorer and then DoubleClick on the exe file.
重新启动 apache 从 [开始] > 运行 > C:/usr/lib/sendmail.exe 或转到 Windows 资源管理器中的 C:/usr/lib 启动 sendmail.exe,然后双击 exe 文件。
And this solution appears in Sendmail Wamp Php
这个解决方案出现在Sendmail Wamp Php
I tried it on Windows 10 now and it runs with gmail account
我现在在 Windows 10 上尝试过,它使用 gmail 帐户运行
Added:
添加:
Open CMD and make a sendmail as daemon/service using
打开 CMD 并使用发送邮件作为守护进程/服务
sc create SendMail binpath= "C:\usr\lib\sendmail.exe"
Now, be sure you have OpenSSL installed on your system, you can download it from here: https://slproweb.com/products/Win32OpenSSL.html
现在,请确保您的系统上安装了 OpenSSL,您可以从这里下载:https: //slproweb.com/products/Win32OpenSSL.html
Installs the version what do you need and dont remember to install "files to windows folder". Do a restart and try again, you will have it solved.
安装您需要的版本,不记得安装“文件到 Windows 文件夹”。重新启动并再试一次,您将解决它。