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

Leave a Comment