Send Email Via Gmail SMTP Authentication With PHPMailer
Posted by in PHP August 2, 2011 18 Comments

One more example related to PHPMailer, in this tutorial I’ll give an example on How to send email through Gmail SMTP Authentication.

Sending email function with PHP may not work in almost cases such as on your localhost, hosting without SMTP Server available. Therefore go with an external SMTP server like Gmail to relay your emails is not a bad solution.

All you need is a Gmail account and I’ll do the rest :)

PHP Send Email Through Gmail SMTP Authentication

<?php
require_once('class.phpmailer.php');
 
$mail             = new PHPMailer();
 
$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);
 
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth   = true;                  		// enable SMTP authentication
$mail->SMTPSecure = "ssl";                 		// sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      		// sets GMAIL as the SMTP server
$mail->Port       = 465;                  	 	// set the SMTP port for the GMAIL server
$mail->Username   = "your-email@gmail.com";  	// GMAIL username
$mail->Password   = "your-gmail-password";		// GMAIL password
 
$mail->SetFrom('from-email@domain.com', 'First Last');
 
$mail->Subject    = "Test PHPMailer via Gmail smtp";
 
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
 
$mail->MsgHTML($body);
 
$address = "to-email@domain.com";
$mail->AddAddress($address, "To Name");
 
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
?>

Let change your Gmail username and password on line 15, 16.

Download the source code above.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • oneli

    don’t work…helppp

    • hoanhuynh

      Hi oneli,

      I just upload the source code above, let download & run it.

  • Danish

    Hi,

    Its not working flagging up with error:

    SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

  • meul pang

    thnks
    can u tell me the smtp validatin, thnks

    • hoanhuynh

      simple replace your-email@gmail.com with your Gmail email and your-gmail-password with your Gmail email password on lines 14,15.

  • joel

    Works for me, just send myself an email from my own gmail account.

    • hoanhuynh

      Glad to hear that Joel. Thank you!

  • jhen

    Pls help..i’m getting this error SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

    thanks.

    • hoanhuynh

      Hi jhen,
      I think Firewall may block the connection to smtp.gmail.com via port 465. Please ask your network administrator to solve the issue.
      Hope it helps,
      H2.

  • http://dangdut.nidhomekoplo.com/ nidhome

    Thanks ……
    This Code Works for me …

  • http://www.in.uk.com/forumdetails.php?id=206 site submission

    Valuable information. Lucky me I discovered your site unintentionally, and I’m shocked why this coincidence did not happened earlier! I bookmarked it.

  • http://ei-n.org Bramer

    Fantastic Script Well done.

  • Rehmanbaigmirza

     Invalid address: SMTP -> ERROR: Password not accepted from server: 435 Unable to authenticate at present

    any idea????

    • http://4rapiddev.com/ Hoan Huynh

      It seems that you did enable 2 step verification.

  • simply-logic technologies

    ( ! )
    Deprecated: Function eregi_replace() is deprecated in
    D:wampwwwsivasend-email-via-gmail-smtp-authentication-with-phpmailersend-email.php
    on line 7
    Call Stack
    #TimeMemoryFunctionLocation
    10.0015372736{main}( )..send-email.php:0

    SMTP Error: Could not connect to SMTP host.
    Mailer Error: SMTP Error: Could not connect to SMTP host.

    • http://4rapiddev.com/ Hoan Huynh

      You will need to adjust the Firewall setting to allow connection to smtp.gmail.com on port 645

  • Ali BEN MESSAOUD

    Deprecated: Function eregi_replace() is deprecated in C:Program FilesEasyPHP-5.3.9wwwgmailsend-email.php on line 7

    SMTP Error: Could not connect to SMTP host.
    Mailer Error: SMTP Error: Could not connect to SMTP host.

  • Deni

    SMTP Error: Could not connect to SMTP host.
    Mailer Error: SMTP Error: Could not connect to SMTP host.

    Why guys ? can you help me please ? T_T