Having trouble with PHPMailer

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    = '[email protected]'; // 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\r\n';
  $Mail->From        = '[email protected]';
  $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 = '[email protected]';
$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

In such kinds of issues it is important to check how it runs on development environment before you deploy to production, since there are many server issues that might be related to the problem.

Because of that before anything set debug to true and check the messages you get.

$mail->SMTPDebug = 1;

That said, common server issues in such situations are the following.

  1. Lack of SSL support in PHP. You must enable it.

  2. Some kind of firewall might be blocking you from connecting to outbound sockets. You can check that using PHP.

-

$p = fsockopen( '127.0.0.1', <port number>, $errno, $errstr, 5 );
if ( !$p )
    // port is closed or blocked
else
    // port is open and available
    fclose( $p );`

Your Code is not working because you did not set the SMTPSecure option to ssl which is required for gmail account

include_once "/lib/phpmailer/PHPMailer.class.php";
include_once "/lib/phpmailer/SMTP.class.php";
include_once "/lib/phpmailer/POP3.class.php";

$mail = new PHPMailer(true);
$mail->IsSMTP();

try {
    $mail->Host = "smtp.gmail.com"; 
    $mail->SMTPDebug = 2; 
    $mail->SMTPSecure = 'ssl'; //<----------------- You missed this 
    $mail->SMTPAuth = true; 
    $mail->Host = "smtp.gmail.com"; 
    $mail->Port = 465; // 
    $mail->Username = "[email protected]";
    $mail->Password = "xxxxxx";
    $mail->AddAddress('[email protected]', 'John Doe');
    $mail->SetFrom('[email protected]', 'First Last');
    $mail->Subject = 'This is a TEST message';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    $body = "To view the message, please use an HTML compatible email viewer!"; // automatically
    $mail->MsgHTML($body);
    $mail->Send();
    echo "Message Sent OK</p>\n";
} catch ( phpmailerException $e ) {
    echo $e->errorMessage(); 
} catch ( Exception $e ) {
    echo $e->getMessage(); 
}

Output

SMTP -> FROM SERVER:220 mx.google.com ESMTP q22sm2927759bkv.16 
SMTP -> FROM SERVER: 250-mx.google.com at your service, [62.173.54.190] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250 ENHANCEDSTATUSCODES 
SMTP -> FROM SERVER:250 2.1.0 OK q22sm2927759bkv.16 
SMTP -> FROM SERVER:250 2.1.5 OK q22sm2927759bkv.16 
SMTP -> FROM SERVER:354 Go ahead q22sm2927759bkv.16 
SMTP -> FROM SERVER:250 2.0.0 OK 1353341553 q22sm2927759bkv.16 
Message Sent OK

Tags:

Php

Gmail