duancaoqin6683 2012-11-12 13:04 采纳率: 0%
浏览 89
已采纳

遇到PHPMailer问题

I am trying to use PHPMailer to send a gmail email. I followed this post

In order to do this, I set up a function shown below:

function sendEmail($email, $name) {
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    //IsSMTP(); // send via SMTP I commented it cos it gives an error
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = 'email@gmail.com'; // Changed my email
    $mail->Password = "password";// Changed my password
    $mail->From = 'email@gmail.com';
    $mail->FromName = 'FROM NAME';


    $mail->AddAddress($email);

    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "Subject";
    $mail->Body = "Body";

    if (!$mail->Send()) {
        return false;
    } else {
        return true;
    }
}

Unfortunately, it keeps on returning false. Can you please tell me whats wrong with the code?

Edit: The error which I am getting is shown below:

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

SMTP Error: Could not connect to SMTP host.

UPDATED CODE:

 $Mail = new PHPMailer();
    $Mail->IsSMTP(); // Use SMTP
    $Mail->Host = "smtp.gmail.com"; // Sets SMTP server
    $Mail->SMTPDebug = 2; // 2 to enable SMTP debug information 
    $Mail->SMTPAuth = TRUE; // enable SMTP authentication
    $Mail->SMTPSecure = "tls"; //Secure conection
    $Mail->Port = 587; // set the SMTP port
    $Mail->Username = EMAIL; // SMTP account username
    $Mail->Password = PASS; // SMTP account password
    $Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
    $Mail->CharSet = 'UTF-8';
    $Mail->Encoding = '8bit';
    $Mail->Subject = 'SUB';
    $Mail->ContentType = 'text/html; charset=utf-8
';
    $Mail->From = EMAIL;
    $Mail->FromName = 'FROM NAME';
    $Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line

    $Mail->AddAddress($email); // To:
    $Mail->isHTML(TRUE);
    $Mail->Body = "Hi";
    $Mail->AltBody = "Hi";
    $Mail->Send();
    $Mail->SmtpClose();
  • 写回答

7条回答 默认 最新

  • duano3557 2012-11-20 08:40
    关注

    Here is a working example:

    <?php
    function SendMail( $ToEmail, $MessageHTML, $MessageTEXT ) {
      require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
      $Mail = new PHPMailer();
      $Mail->IsSMTP(); // Use SMTP
      $Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
      $Mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
      $Mail->SMTPAuth    = TRUE; // enable SMTP authentication
      $Mail->SMTPSecure  = "tls"; //Secure conection
      $Mail->Port        = 587; // set the SMTP port
      $Mail->Username    = 'MyGmail@gmail.com'; // SMTP account username
      $Mail->Password    = 'MyGmailPassword'; // SMTP account password
      $Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
      $Mail->CharSet     = 'UTF-8';
      $Mail->Encoding    = '8bit';
      $Mail->Subject     = 'Test Email Using Gmail';
      $Mail->ContentType = 'text/html; charset=utf-8
    ';
      $Mail->From        = 'MyGmail@gmail.com';
      $Mail->FromName    = 'GMail Test';
      $Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line
    
      $Mail->AddAddress( $ToEmail ); // To:
      $Mail->isHTML( TRUE );
      $Mail->Body    = $MessageHTML;
      $Mail->AltBody = $MessageTEXT;
      $Mail->Send();
      $Mail->SmtpClose();
    
      if ( $Mail->IsError() ) { // ADDED - This error checking was missing
        return FALSE;
      }
      else {
        return TRUE;
      }
    }
    
    $ToEmail = 'Name@example.com';
    $ToName  = 'Name';
    
    $Send = SendMail( $ToEmail, $MessageHTML, $MessageTEXT );
    if ( $Send ) {
      echo "<h2> Sent OK</h2>";
    }
    else {
      echo "<h2> ERROR</h2>";
    }
    die;
    ?>
    

    I tried this script and had no problem sending several messages.

    UPDATED:

    This is the typical response from Gmail on success:

    SMTP -> FROM SERVER:220 mx.google.com ESMTP 20sm6345523qek.6
    SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                         250-SIZE 35882577
                         250-8BITMIME
                         250-STARTTLS
                         250 ENHANCEDSTATUSCODES
    SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
    SMTP -> FROM SERVER: 250-mx.google.com at your service, [181.155.13.39]
                         250-SIZE 35882577
                         250-8BITMIME
                         250-AUTH LOGIN PLAIN XOAUTH XOAUTH2
                         250 ENHANCEDSTATUSCODES
    SMTP -> FROM SERVER:250 2.1.0 OK 20sm6345523qek.6
    SMTP -> FROM SERVER:250 2.1.5 OK 20sm6345523qek.6
    SMTP -> FROM SERVER:354 Go ahead 20sm6345523qek.6
    SMTP -> FROM SERVER:250 2.0.0 OK 1353474062 20sm6345523qek.6
    SMTP -> FROM SERVER:221 2.0.0 closing connection 20sm6345523qek.6
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP