沃梦达 / 编程问答 / php问题 / 正文

使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时

Phpmailer using smtp with Gmail not working - connection timing out(使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时)

本文介绍了使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了以下链接:

phpmailer 发送 gmail smtp 超时

通过 PHP Mailer 使用 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...

消息无法发送.

邮件程序错误:SMTP 连接()失败.

Mailer Error: SMTP connect() failed.

但是有一次当我在tls"和ssl"之间进行实验时它发送了这个......

However there was one time where it sent this when I experimented between "tls" and "ssl"...

SMTP 错误:无法连接到服务器:连接超时 (110) SMTP connect() 失败.无法发送消息.

SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Message could not be sent.

邮件程序错误:SMTP 连接()失败.

Mailer Error: SMTP connect() failed.

我的代码已附上...我是否遗漏了什么?我问网络托管服务他们是否阻止并给了他们我的代码模板 - 他们说服务器允许连接到 Gmail 的 SMTP.

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.

    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
";
    $headers .= "MIME-Version: 1.0
";
    $headers .= "Content-type: text/html; charset=iso-8859-1
";

    $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;
    }

推荐答案

我研究了一下.使用 php 原生的 fsocketopen 来测试连接并消除大部分潜在问题.用这个写一个简单的php页面:

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:

    $host = "smtp.gmail.com";
    $port = "587";
    $checkconn = fsockopen($host, $port, $errno, $errstr, 5);
    if(!$checkconn){
        echo "($errno) $errstr";
    } else {
        echo 'ok';
    }

你应该返回ok".如果不是,你知道你有一个与 Phpmailer 无关的连接问题.如果是这种情况,它可能是托管公司.如果不是,那可能是您的本地主机和托管公司之间的区别很简单,例如不同版本的 php.

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.

我怀疑这个脚本不会建立连接

I suspect though that this script won't make the connection

这篇关于使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 smtp 和 Gmail 的 Phpmailer 无法正常工作 - 连接超时

基础教程推荐