PHPmailer:SMTP 连接()失败(不工作)

Posted

技术标签:

【中文标题】PHPmailer:SMTP 连接()失败(不工作)【英文标题】:PHPmailer: SMTP connect() failed (not working) 【发布时间】:2016-02-21 16:40:45 【问题描述】:

我想使用 SMTP 服务器 (gmail) 从 localhost XAMPP 使用 phpmailer 发送邮件。但我不断收到此错误:

无法发送邮件。邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我尝试了很多关于在 php.ini 文件中取消注释 openSSL 的解决方案,更改端口 465("ssl") 和 587("tls"),但它不起作用。

我的代码:

<?php
date_default_timezone_set('Etc/UTC');
'PHPMailerAutoLoad.php';
class.phpmailer.php if not already loaded
$port =465; 
$securetype = 'ssl';
$from = 'myemail@gmail.com';
$name = 'User';
$toemail= "mymail@gmail.com";

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->isSMTPDebug = 1;
$mail->Host = 'smtp.gmail.com'; 

$mail->SMTPAuth = true; 
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = $securetype;
$mail->Port = $port;

$mail->From = $from;
$mail->FromName = $name;
$mail->addAddress($toemail);

$mail->ishtml(true);

$mail->Subject = 'Test Mail Subject!';
$mail->Body    = 'This is SMTP Email Test';

if(!$mail->send()) 
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
  else 
    echo 'Message has been sent';

?>

【问题讨论】:

您使用的是旧的 gmail 示例,表明您尚未阅读或完成链接到的文档告诉您的内容。 【参考方案1】:

您将需要class.phpmailer.php。如果您使用 SMTP,则需要class.smtp.php。 试试这个演示代码:

<?php
      require 'class.phpmailer.php';
            require 'class.smtp.php';
    //SMTP needs accurate times, and the PHP time zone MUST be set
    //This should be done in your php.ini, but this is how to do it if you don't have access to that
    #require '../PHPMailerAutoload.php';
    //Create a new PHPMailer instance
    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP
    $mail->isSMTP();
    //Enable SMTP debugging
    // 0 = off (for production use)
    // 1 = client messages
    // 2 = client and server messages
    $mail->SMTPDebug = 0;
    //Ask for HTML-friendly debug output
    $mail->Debugoutput = 'html';
    //Set the hostname of the mail server
    $mail->Host = 'mail.domain.com';
    // use
    // $mail->Host = gethostbyname('smtp.gmail.com');
    // if your network does not support SMTP over IPv6
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
    $mail->Port = 25;
    //Set the encryption system to use - ssl (deprecated) or tls
    //$mail->SMTPSecure = 'tls';
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //Username to use for SMTP authentication - use full email address for gmail
    $mail->Username = "demo@domain.com";
    //Password to use for SMTP authentication
    $mail->Password = "password";
    //Set who the message is to be sent from
    $mail->setFrom('demo@domain.com', 'subillion');
    //Set an alternative reply-to address
    #$mail->addReplyTo('replyto@example.com', 'First Last');
    //Set who the message is to be sent to
    $mail->addAddress("example@gmail.com");

    //Set the subject line
    $mail->Subject = 'Demo !!';
    //Read an HTML message body from an external file, convert referenced images to embedded,
    $mail->isHTML(true);

    $msgbody = "This is a demo test !";
    $mail->Body = $msgbody;
    //send the message, check for errors
    if (!$mail->send()) 
    // echo "Mailer Error: " . $mail->ErrorInfo;
     else 
        echo "Message sent!";
    
?>

【讨论】:

以上是关于PHPmailer:SMTP 连接()失败(不工作)的主要内容,如果未能解决你的问题,请参考以下文章

“SMTP 连接()失败”。将其配置为从远程主机发送电子邮件时

SMTP connect()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

linux PHPMailer 发送邮件sendmail 发邮件失败问题

SMTP 连接()失败。发送认证邮件时

PHPMailer发送邮件失败:SMTP connect() failed.

带有 mandrill smtp 的 PHPMailer 给出连接错误