Sending mail using phpmailer and gmail as smtp

Hello friends following script will work for sending mail with using of phpmalier class and gmail as smtp.This will be work for you if there is no mail support to your domain. it will be working in localhost also.

Download php mailer class from


PHP Code

After that at the php file write the below code:

$mailer = new PHPMailer();

$mailer->Host = "";

$mailer->SMTPAuth = TRUE;
$mailer->Username = "{somename}";  // Change this to your gmail adress
$mailer->Password = "{password}";  // Change this to your gmail password
$mailer->From = "{someid}";  // This HAVE TO be your gmail adress
$mailer->FromName = "Anil Labs"; // This is the from name in the email, you can put anything you like here
$mailer->Body = "This is the main body from ANIL LABS";
$mailer->Subject = "This is mail from anil labs";
$mailer->AddAddress("{toaddress email id}");  // This is where you put the email adress of the person you want to mail
echo "Message was not sent<br/ >";
echo "Mailer Error: " . $mailer->ErrorInfo;
echo "Message has been sent";

Hope that it will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

12 Responses

  1. rahul says:


    I tried ur code it is giving me following error

    Message was not sent
    Mailer Error: Language string failed to load: connect_host

    please advice

  2. Hi Rahul,

    Thank you for intimating me,

    Just change the gmail smtp like:

    $mailer->Host = ‘’;

    And at class.phpmailer.php file change the

    public $SMTPAuth = true;

    Then it will be work.

  3. harpreet says:

    how to attach the doc file in this code?

  4. jayshree says:

    hey its still not working.its not connecting to smtp

  5. Sreenath says:

    Hello, Anil

    I am getting the error

    SMTP Error: Could not authenticate. Message was not sent
    Mailer Error: SMTP Error: Could not authenticate.

  6. Shishir says:

    Hey every body i m new in this forum……..and i m using this code, but the prob. is if user want’s to contact us as above code then he’ll have to write there email id and password…the prob. is password can any one here tell me how can i use PHPMAILER code without using SMTPAUTH or PASSWORD thnx in advance..:)

  7. sathish says:

    i am new, i don’t know how to send mail, as you said, i downloaded phpmailer inwhich there were some files so i did not know how to use, so just copy and paste your php code, then accures some errors

    Warning: require(includes/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in D:\wamp\www\mail\mailer\email.php on line 11

    Fatal error: require() [function.require]: Failed opening required ‘includes/class.phpmailer.php’ (include_path=’.;C:\php5\pear’) in D:\wamp\www\mail\mailer\email.php on line 11

    please help me

  8. sanjeeviraj says:

    thanks a lot!!!!!!!!!!!!!!!!!!!!!it worked super!!!!my 8 hours search came to end…..

  9. Syed Asfaquz Zaman says:

    Thanks a lot.It working great.

  10. Ash says:

    I am using another SMTP provider…. I get the following error using PHP Mailer

    Message could not be sent.Mailer Error: The following From address failed: [email protected]

    Can someone comment?

  11. Mohd Irshad says:

    this is continously giving me error:

    SMTP Error: Could not authenticate. Message was not sent
    Mailer Error: SMTP Error: Could not authenticate.

  1. October 24, 2018

    […] HTML templates in Codeigniter, earlier post we learned how to send mails using HTML content and using phpmailer and gmail. Now we learn how to send mails in Codeigniter. By follow the below steps we can implement […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.