localhost上的SMTP connect()失败错误

时间:2017-07-19 08:39:57

标签: php html email smtp

我刚开始用PHP编码。我正在尝试发送邮件,但它会抛出错误邮件程序错误:SMTP连接()失败。

以下是我正在使用的代码:

    date_default_timezone_set('Etc/UTC');

    require 'PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP(); 
    $mail->Host = gethostbyname('ssl://smtp.gmail.com'); 

    $mail->SMTPAuth = true; 
    $mail->AuthType = 'LOGIN';  
    $mail->Username = 'abc@gmail.com';
    $mail->Password = '********';   
    $mail->SMTPSecure = 'tls';  
    $mail->Port = 587;  

    $mail->setFrom('abc@gmail.com','Abc') ;
    $mail->addReplyTo('abc@gmail.com');
    $mail->addAddress("abc@gmail.com",'No REPLY');
    $mail->isHTML(true);

    $mail->Body = "12345";
    $mail->Subject = "<b>OTP for password reset<b>";
    $mail->Body = "Hi! Your OTP is : ";

    if(!$mail->Send()) {
       echo 'Message was not sent.';
       echo "Mailer Error: " . $mail->ErrorInfo;
       } 
    else {
       echo 'Message has been sent.';
    }  

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

改变
$mail->Host = gethostbyname('ssl://smtp.gmail.com');
$mail->SMTPSecure = 'tls';  
$mail->Port = 587; 

$mail->Host = "smtp.gmail.com"; 
$mail->SMTPSecure = 'ssl';  
$mail->Port = 465; 

并检查open_ssl已启用

打开php.ini并搜索此行 ;extension=php_openssl.dll并删除之前的分号,保存文件并重新启动您的网络服务器。