简体   繁体   English

phpmailer在Gmail上使用smtp无法正常工作-连接超时

[英]Phpmailer using smtp with Gmail not working - connection timing out

I've looked into the following links: 我研究了以下链接:

phpmailer send gmail smtp timeout phpmailer发送gmail smtp超时

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. SMTP错误:无法连接到服务器:连接超时(110)SMTP connect()失败。 Message could not be sent. 无法发送信息。

Mailer Error: SMTP connect() failed. 邮件错误: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 = "myemail@gmail.com";
    $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;
    }

I dug into it. 我挖了它。 Use fsocketopen, which is native to php, to test the connection and eliminate most of the potential problems. 使用PHP固有的fsocketopen来测试连接并消除大多数潜在问题。 Write a simple php page with this: 用这个写一个简单的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. 如果不是这样,您就会知道与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. 如果不是这样,那么可能是关于本地主机和托管公司之间的区别(例如不同版本的php)的一些简单说明。

I suspect though that this script won't make the connection 我怀疑尽管此脚本无法建立连接

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 希望它能工作

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. 然后重试您的phpmailer电子邮件发送。 You will see the entire SMTP conversation on standard error output. 您将在标准错误输出中看到整个SMTP对话。 If you're using a web server, look in the web server log file. 如果您使用的是Web服务器,请查看Web服务器日志文件。

I could then see the error response from gmail. 然后,我可以看到来自gmail的错误响应。 Gmail was not accepting the login. Gmail不接受登录。

The error within the smtp conversation refers to an article . smtp对话中的错误是指一篇文章 It gives some tips: 它给出了一些提示:

  1. Allow less secure apps to use your account . 允许安全性较低的应用使用您的帐户
  2. Login to gmail from a web browser. 从网络浏览器登录gmail。
  3. Confirm the gmail login captcha . 确认gmail登录验证码 (It may not actually display a captcha to you, but this was the additional step that suddenly allowed my email to go through.) (它实际上可能不会向您显示验证码,但这是突然让我的电子邮件通过的附加步骤。)

Check to make sure you can reach gmail from your webhost. 检查以确保可以从您的网络主机访问gmail。 I'm assuming it's linux. 我假设它是Linux。 SSH in and on the command line type 命令行中的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 它必须是localhost和您的提供程序之间的配置差异

This question has many duplicates, so here's a canned answer: 这个问题有很多重复,所以这是一个罐头答案:

  1. Base your code on the gmail example provided with PHPMailer 将代码基于PHPMailer提供的gmail示例
  2. Check out the troubleshooting docs 查看故障排除文档
  3. Be aware of this issue , related to what Larry K said. 请注意与Larry K所说的有关的问题

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. 或者尝试更改您的密码,然后重试。

Add

date_default_timezone_set('Etc/UTC');

before including the autoloader. 包括自动装带器之前。 SMTP Needs to have the timezone set which was my issue. SMTP需要设置时区,这是我的问题。

Download sendmail for Windows from http://www.glob.com.au/sendmail/sendmail.zip Copy 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) force_sender=you-sender@yourdomain.com force_recipient=you@yourdomain.com By the way I uncommented debug_logfile so I can see what data is being sent to my SMTP server. 您可能还希望配置这两个字段(或发送可能不起作用)force_sender=you-sender@yourdomain.com force_recipient=you@yourdomain.com顺便说一下,我没有对debug_logfile进行注释,因此我可以看到正在发送什么数据给我SMTP服务器。

edit c:\\php\\php.ini 编辑c:\\ php \\ php.ini

sendmail_from = you@yourdomain.com sendmail_from = you@yourdomain.com

; ; For Unix only. 仅适用于Unix。 You may supply arguments as well (default: "sendmail -t -i"). 您也可以提供参数(默认值:“ sendmail -t -i”)。 sendmail_path = C:/usr/lib/sendmail.exe -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,然后双击exe文件,启动sendmail.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,并使用以下命令将sendmail作为守护程序/服务

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". 安装所需的版本,不要忘记安装“文件到Windows文件夹”。 Do a restart and try again, you will have it solved. 重新启动并重试,即可解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM