Tuesday, October 1, 2013

How to get your PHPMailer sample code to work properly with your SMTP Server?

Download PHPMailer recently to test on email sending through SMTP server, out of no luck, it just can't send email through the SMTP server  (Microsoft ESMTP MAIL Service, Version: 5.0.2195.738).

Assume the parameters below represent

SMTP Server: [SMTP_SERVER]
Client Server Name: [CLIENT_SERVER_NAME]

Configure all the necessary parameter in SMTP.php class provided by PHPMailer in examples folder, out of no luck, it just can't send email. Turn on the SMTP debug parameters in SMTP.php

// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug  = 2;
The debug messages are displayed as described below:
SMTP -> FROM SERVER:220 [SMTP_SERVER] Microsoft ESMTP MAIL Service, Version: 5.0.2195.7381 ready at Mon, 23 Sep 2013 14:38:12 +0800 
CLIENT -> SMTP: EHLO [[CLIENT_SERVER_NAME]]
SMTP -> FROM SERVER: 501 5.5.4 Invalid Address
SMTP -> ERROR: EHLO not accepted from server: 501 5.5.4 Invalid Address
CLIENT -> SMTP: HELO [[CLIENT_SERVER_NAME]]
SMTP -> FROM SERVER: 501 5.5.4 Invalid Address
SMTP -> ERROR: HELO not accepted from server: 501 5.5.4 Invalid Address
CLIENT -> SMTP: AUTH LOGIN
SMTP -> ERROR: AUTH not accepted from server: 503 5.5.2 Send hello first
CLIENT -> SMTP: quit
SMTP -> FROM SERVER:221 2.0.0 [SMTP_SERVER] Service closing transmission channel
SMTP Connect() failed.
Mailer Error: SMTP Connect() failed.
Refer to the debug messages, message "Invalid Address" is keep prompting.

Troubleshoot the debug message and discovered that special character '[' and ']' is appended to the [SMTP_SERVER_NAME] value.

Comment out 5 lines of source code at line 720  in "class.smtp.php" as described below and replace with a new 5 lines of source code as described below that removed '[' and ']' characters:
    //if(!$this->SendHello('EHLO', "[" . $host . "]")) {
    //  if(!$this->SendHello('HELO', "[" . $host . "]")) {
    //    return false;
    //  }
    //}
 
    if(!$this->SendHello('EHLO', $host)) {
      if(!$this->SendHello('HELO',$host)) {
        return false;
      }
    }
Try to resend email, it just work like a charm!

No comments: