PHPMailer不适用于Gmail SMTP [英] PHPMailer does not work with Gmail SMTP

查看:90
本文介绍了PHPMailer不适用于Gmail SMTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可在我的xampp本地服务器上使用,但不会在远程主机上发送电子邮件.我收到此错误:

The following code works on my xampp local server, but doesn't send e-mails on a remote host. I get this error:

无法发送邮件.邮件程序错误:SMTP connect()失败

Message could not be sent.Mailer Error: SMTP connect() failed

require_once('header.php');
require_once('PHPMailer/PHPMailerAutoload.php');
function sendMail($address, $message){
    $mail = new PHPMailer;
    //$mail->SMTPDebug = 3;                 // Enable verbose debug output
    $mail->isSMTP();                        // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';         // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                 // Enable SMTP authentication
    $mail->Username = 'mymail@gmail.com';   // SMTP username
    $mail->Password = 'mypass';             // SMTP password
    $mail->SMTPSecure = 'tls';              // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                      // TCP port to connect to
    $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
    ); 
    $mail->setFrom('mymail@gmail.com', 'ID Test');   // Add a recipient
    $mail->addAddress($address);               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    $mail->isHTML(false);                      // Set email format to HTML

    $mail->Subject = 'Twitter search';
    $mail->Body    = $message;
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
}

推荐答案

过去,使用Gmail设置为TLS身份验证协议和端口号587的Gmail服务器时,PHPMailer遇到了麻烦.为我.但是,使用SSL/465从来没有问题.

I've had trouble in the past with PHPMailer when using Gmail servers set to TLS authentication protocol and port number 587. I don't recall that combination ever working for me. However, I've never had a problem using SSL/465.

代替此:

// Your Current Settings
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

尝试一下:

// Updated Settings
$mail->SMTPSecure = 'ssl'; 
$mail->Port = 465;  

更多信息:

  • Google SMTP Settings
  • Difference Between TLS and SSL

这篇关于PHPMailer不适用于Gmail SMTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆