PHPMailer does not work with Gmail SMTP [closed]

不打扰是莪最后的温柔 提交于 2019-12-01 22:04:58

问题


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

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();
}

回答1:


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.

Instead of this:

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

Try this:

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

More information:

  • Google SMTP Settings
  • Difference Between TLS and SSL


来源:https://stackoverflow.com/questions/40439333/phpmailer-does-not-work-with-gmail-smtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!