Send email using the GMail SMTP server from a PHP page


Using Swift mailer, it is quite easy to send a mail through Gmail credentials:

require_once 'swift/lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('', 465, "ssl")

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance('Test Subject')
  ->setFrom(array('' => 'ABC'))
  ->setBody('This is a test mail.');

$result = $mailer->send($message);

I am trying to send an email via GMail's SMTP server from a PHP page, but I get this error:

authentication failure [SMTP: SMTP server does no support authentication (code: 250, response: at your service, [] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES PIPELINING)]

Can anyone help? Here is my code:

require_once "Mail.php";

$from = "Sandra Sender <>";
$to = "Ramona Recipient <>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "";
$port = "587";
$username = "";
$password = "testtest";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
 } else {
  echo("<p>Message successfully sent!</p>");

I don't recommend Pear Mail. It has not been updated since 2010. Also read the source files; the source code is almost outdated, written in PHP 4 style and many errors / bugs have been posted (Google it). I am using Swift Mailer.

Swift Mailer integrates into any web application written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features.

Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own.

Support servers that require username & password and/or encryption.

Protect from header injection attacks without stripping request data content.

Send MIME compliant HTML/multipart emails.

Use event-driven plugins to customize the library.

Handle large attachments and inline/embedded images with low memory use.

It is a free and open source you can Download Swift Mailer and upload to your server. (The feature list is copied from owner website).

The working example of Gmail SSL/SMTP and Swift Mailer is here...

// Swift Mailer Library
require_once '../path/to/lib/swift_required.php';

// Mail Transport
$transport = Swift_SmtpTransport::newInstance('ssl://', 465)
    ->setUsername('') // Your Gmail Username
    ->setPassword('my_secure_gmail_password'); // Your Gmail Password

// Mailer
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject Here')
    ->setFrom(array('' => 'Sender Name')) // can be $_POST['email'] etc...
    ->setTo(array('' => 'Receiver Name')) // your email / multiple supported.
    ->setBody('Here is the <strong>message</strong> itself. It can be text or <h1>HTML</h1>.', 'text/html');

// Send the message
if ($mailer->send($message)) {
    echo 'Mail sent successfully.';
} else {
    echo 'I am sure, your configuration are not correct. :(';

I hope this helps. Happy coding... :)


'auth' => false,

Also, see if port 25 works.

I had this problem also. I set the correct settings and have enabled less secure apps but it still did not work. Finally, I enabled this, and it worked for me. I hope this helps someone.

Send Mail using phpMailer library through Gmail Please donwload library files from Github

 * This example shows settings to use when sending via Google's Gmail servers.
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = '';
// use
// $mail->Host = gethostbyname('');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";

SwiftMailer can send E-Mail using external servers.

here is an example that shows how to use a Gmail server:

require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

//Connect to localhost on port 25
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));

//Connect to an IP address on a non-standard port
$swift =& new Swift(new Swift_Connection_SMTP("", 419));

//Connect to Gmail (PHP5)
$swift = new Swift(new Swift_Connection_SMTP(
    "", Swift_Connection_SMTP::PORT_SECURE, Swift_Connection_SMTP::ENC_TLS));