How to Send e-Mail using PHPMailer and Gmail SMTP | Coding Cage

How to Send e-Mail using PHPMailer and Gmail SMTP

By
hello friends after a long time i'm going to post a simple yet useful post here about SMTP mails, i received few email requests from my readers to post email verification feature in my login and registration tutorial i'll post email verification tutorial after this post this was first step to send emails from localhost and online server as well for that you have to use PHPMailer library, so take a quick a look at this tutorial to understand "how to send SMTP mails using PHPMailer"
How to Send e-Mail using PHPMailer and Gmail SMTP


First of all download the PHPMailer library from this link : http://sourceforge.net/projects/phpmailer/

Simple PHP CODE

copy-paste the following script using your editor and save this file as "sendmail.php" and try it in your localhost server

<?php 

 require_once('class.phpmailer.php');
 
    $mail = new PHPMailer();
    $mail->CharSet =  "utf-8";
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = "your_gmail@gmail.com";
    $mail->Password = "your_gmail_password";
    $mail->SMTPSecure = "ssl";  
    $mail->Host = "smtp.gmail.com";
    $mail->Port = "465";
 
    $mail->setFrom('your_gmail@gmail.com', 'your name');
    $mail->AddAddress('to_mail@mail.com', 'receivers name');
 
    $mail->Subject  =  'using PHPMailer';
    $mail->IsHTML(true);
    $mail->Body    = 'Hi there ,
                        <br />
                        this mail was sent using PHPMailer...
                        <br />
                        cheers... :)';
  
     if($mail->Send())
     {
        echo "Message was Successfully Send :)";
     }
     else
     {
        echo "Mail Error - >".$mail->ErrorInfo;
     }
  
?>
That's it
here using this simple script you can send emails, try it in your localhost server, but make sure that you are connected to the internet



47 comments:

  1. Thanks for your tutorial. Please describe where and how to install php mailer both on localhost and a web server.

    ReplyDelete
    Replies
    1. download the phpmailer library from the above link or download the code which i have given here and then put these files in your "www" directory folder / in online server you can install in your root folder...

      Delete
  2. Wow nice information and it is very informative.The author clearly describe the topic with good example and languages.Thank you so much for your great sharing.custom essay writing services

    ReplyDelete
  3. Hello it's very nice but I'm not getting email in my gmail id why? Please help me while signing up it tells SMTP Error: Could not authenticate. Please help me to know how to fix it please sir

    ReplyDelete
    Replies
    1. hello nimesh, please check your credentials : email id, password, port no is properly set or not , or make sure that you have not enabled 2-step verification in your gmail id.

      Delete
    2. Hey, i have the same porblem and i have the info put ok, i tested ssl and tls with all ports.... and not working

      Delete
  4. " SMTP Error: Could not authenticate. Mail Error - >SMTP Error: Could not authenticate."
    how to fix this error

    ReplyDelete
    Replies
    1. hello venkata, please check gmail username, password, and smtp port and try to change SMTPDebug value 0, 1, 2 it will give you error messages with details, and there is no need to do change in phpmailer class

      $mail->SMTPDebug = 0;

      Delete
    2. gmail port is :587 not 465

      Delete
  5. You tell us to download the PHPmailer library then don't tell what to do with it. I'm sorry if I'm being a noob. Where should I place it?

    ReplyDelete
    Replies
    1. you can put PHPMailer Library in your Projects root folder ....

      Delete
  6. SMTP Error: Could not connect to SMTP host >> how to fix this error??/

    ReplyDelete
    Replies
    1. hello rana raheel,
      please check gmail credentials, and smtp port and try to change SMTPDebug value 0, 1, 2 it will give you error messages with details, and there is no need to do change in phpmailer class

      $mail->SMTPDebug = 0;

      make sure you have not enabled two step verification in your gmail account

      Delete
  7. Hello Pradeep,

    The SMTP error is where I get stuck (like the rest I see)
    Can you show me where to put my own gmail adress and which port I should put in?
    Is it right to just put $mail->SMTPDebug = 0; in the section below?

    $mail = new PHPMailer();
    $mail->CharSet = "utf-8";
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = "your_gmail@gmail.com";
    $mail->Password = "your_gmail_password";
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com";
    $mail->Port = "465";

    $mail->setFrom('your_gmail@gmail.com', 'your name');
    $mail->AddAddress('to_mail@mail.com', 'receivers name');

    Thx alot!

    ReplyDelete
    Replies
    1. hello there, i clearly shows that where to put your gmail credentials ,

      $mail->Username = "put_your_gmail_id_here@gmail.com";
      $mail->Password = "your_gmail_password";
      $mail->SMTPSecure = "ssl";
      $mail->Host = "smtp.gmail.com";
      $mail->Port = "465";

      $mail->setFrom('put_your_gmail_id_here@gmail.com', 'your name');
      $mail->AddAddress('receivers_gmail_id_here@mail.com', 'receivers name');

      Delete
    2. Hi, i'm getting this error. SMTP Error: Could not connect to SMTP host. Mail Error - >SMTP Error: Could not connect to SMTP host.

      Delete
  8. how to change gmail smtp to office365 smtp

    ReplyDelete
  9. sir can u pls tell the configuration for office365 host

    ReplyDelete
  10. No that wasn't i tried that link

    ReplyDelete
  11. after doing some changes its showing these errors
    SMTP -> FROM SERVER:220 HE1PR03CA0017.outlook.office365.com Microsoft ESMTP MAIL Service ready at Tue, 3 Nov 2015 17:08:37 +0000
    SMTP -> FROM SERVER: 250-HE1PR03CA0017.outlook.office365.com Hello [14.139.242.69] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 XXXXXXXX
    SMTP -> ERROR: AUTH not accepted from server: 504 5.7.4 Unrecognized authentication type
    SMTP -> FROM SERVER:250 2.0.0 Resetting
    SMTP Error: Could not authenticate

    ReplyDelete
    Replies
    1. sorry himanshu, i haven't work with office 365 but the concept was the same, so try to solving it, there's must be something wrong within authentication. or if you have office 365 server and your own mail then you can find your smtp details and configurations there in your webmail.

      Delete
  12. Am still having issues with STMP host
    Could not connect to STMP host

    ReplyDelete
  13. Must i be connected to the internet to make it work
    STMP could not connect to host

    ReplyDelete
    Replies
    1. yes, you must be connected to the internet..

      Delete
  14. Thanks for this guide to using PHPMailer - Certainly the best I've found. I had great issue getting it to work.. I'd checked all the things you mentioned but eventually found that it worked (by sending through my gmail smtp) if I removed the line:
    $mail->IsSMTP();

    and if I want to send via localhost, I remove (or comment out lines 8-13.
    Hope this help someone :)

    ReplyDelete
    Replies
    1. I'm using localhost and still not working. I have commented out $mail->IsSMTP(); and line 8-13. The message is being send but not receive by the receipient. Please post the working code.

      Delete
    2. Removed $mail->IsSMTP(); and worked for me. Thanks.

      Delete
    3. Hello Steven, I removed what you said. It says message was successfully send. but in my email :info@websitename.com , there is no email.

      Delete
  15. how to fix this error
    Call to undefined method PHPMailer::setFrom()

    ReplyDelete
  16. Hello Pradeep,
    Is there any other way for email verification than phpmailer.Kindly help.

    ReplyDelete
    Replies
    1. you can use swiftmailer or sendgrid

      Delete
  17. Dear Pradeep.
    Is there any other way for email verification than through phpmailer.Kindly help

    ReplyDelete
  18. I cannot downloading the code plz help me

    ReplyDelete
    Replies
    1. download link updated, get it from there ...

      Delete
  19. $mail->AddAddress('to_mail@anymail.com', 'receivers name');
    This is not required for me because the user enter their email address in feed back form and then the message is sent to the admin mail address.

    ReplyDelete
  20. I am working from xampp.It showing the Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. 17sm5260251pfq.51 - gsmtp in.
    And the mail is not sending
    Plzzz help me

    ReplyDelete
  21. Hi Pradeep, I added class.phpmailer.php file and got phpmailer to send confirm registration email which lands in my spam atm... but when i follow link to sign in it says wrong details...? I've rechecked what I've typed but still the same. please help

    ReplyDelete

  22. Warning: require_once(mailer/class.phpmailer.php): failed to open stream: No such file or directory in C:\xampp\xampp\htdocs\tags\Class.User.Php on line 112

    Fatal error: require_once(): Failed opening required 'mailer/class.phpmailer.php' (include_path='C:\xampp\xampp\php\PEAR') in C:\xampp\xampp\htdocs\tags\Class.User.Php on line 112

    ReplyDelete
  23. Hi Pradeep,
    I used the codes from this page, had "HTTP 500 error: can not find the page". I tried to trace the code and found the statement below is the problem


    $mail->setFrom(xxxxxx@gmail.com', 'yyyyyy');

    and found that error is caused in the PHPMailer

    public function getSMTPInstance()
    {
    if (!is_object($this->smtp)) {
    $this->smtp = new SMTP; //<===== this is where the problem caused
    }
    return $this->smtp;
    }

    could you help to figure out what is problem? and give advice. I have class.smtp.php and class.phpmailer.php in the same folder.

    ReplyDelete
  24. Hi Pradeep,
    I used the codes from this page, had "HTTP 500 error: can not find the page". I tried to trace the code and found the statement below is the problem


    $mail->setFrom(xxxxxx@gmail.com', 'yyyyyy');

    and found that error is caused in the PHPMailer

    public function getSMTPInstance()
    {
    if (!is_object($this->smtp)) {
    $this->smtp = new SMTP; //<===== this is the problem }
    return $this->smtp;
    }

    could you help to figure out what is problem? and give advice? I have class.smtp.php and class.phpmailer.php in the same folder.

    ReplyDelete
  25. I got this error: Call to undefined method PHPMailer::setFrom(

    ReplyDelete
  26. THANKS this is a good one ! but I have you ever encounter this error from Gmail ? ,




    Email From Mail Delivery Subsystem :


    Delivery to the following recipient failed permanently:

    myemail@mail.com

    Technical details of permanent failure:
    Google tried to deliver your message, but it was rejected by the server for the recipient domain mail.com by mx00.mail.com. .

    The error that the other server returned was:
    550 Requested action not taken: mailbox unavailable

    ReplyDelete
  27. could you help to figure out what is problem? When i Click HERE to Activate .it shows object not found.

    ReplyDelete
  28. Thanks for sharing your thoughts & excitements about future of the web. good post guys and thanks for your article this is simply awesome. thanks for your info! json converter

    ReplyDelete
  29. thank you so much for share the knowledge. It worked like a charm. It worked in the first go with the hosting provider (godaddy). I was very skeptical if this code was kept current or not . But I confirm you guys that as on 23-Mar-2017 - this code works perfectly.

    ReplyDelete
  30. This code worked like a charm. I was skeptical initially but I confirm it works perfect and the code is current as on 23-Mar-2017. Thank you so much for sharing this knowledge. It is very encouraging for hobby learners like myself.

    ReplyDelete