Saturday, 13 July 2013

How to sent mail in php using Gmail SMTP

Hi,

     Have you ever thought how to send mail in PHP with out using mail() ?


     I know you would have used mail() in PHP, then you should also know limitations of it.

     Setting/configuring your outlook,mercury,thunderbird to use  mail() is an hurdle and it doesn't
     provide much flexibiity too.

    Do you know there are many libraries to send mail? indeed power full libraries like
    phpmailer, swiftmailer
.

    These classes or libraries have been written to make sending mail using PHP easier
    and send mail with more flexibility and features.

    In this demo am going to use phpmailer class as swiftmailer is also good but I prefer and
    recommend phpmailer because swiftmailer class has not been updated recently and where
   phpmailer has good documentation and new releases are always on the way.

   Lets start now :)

   First download phpmailer from here .

   Extract it to your local host directory (ex: htdocs in windows xampp).

   Now you have a directory called phpmailer  in your web directory.

   Now write a php script, first include phpmailer class.

         require_once './phpmailer/class.phpmailer.php';
 
   Now instantiate the phpmailer class.
   
          $mail = new PHPMailer(true);

   Now $mail contains instance of PHPMailer class now you can configure all the properties
   of it.

       
$mail->CharSet = 'utf-8'; //sets character set
$to = 'wetechies.2013@gmail.com'; //from whom the mail should be sent

$mail->IsSMTP();  //To inform that we are using SMTP

$mail->SMTPDebug = 2;  //Shows long message set to 1 to avoid it

$mail->Host = "smtp.gmail.com"; // As we are using gmail SMTP

$mail->Port = "465";  // smtp port number

$mail->SMTPSecure = "ssl"; //gmail requires authentication through ssl or tls

$mail->SMTPAuth = true;

// your account details for authentication provide correct user name and password
$mail->Username = "wetechies.2013@gmail.com";

$mail->Password = "mypassword";

$mail->AddReplyTo("wetechies.2013@gmail.com", "nag");

$mail->From = "wetechies.2013@gmail.com";

$mail->FromName = "wetechies blog";

$mail->AddAddress("wetechies.2013@gmail.com", "nagashayan");

$mail->Subject = "hi (PHPMailer test using SMTP)";

$body="Sending mail by using wetechies post on How to use Gmail SMTP to send mail \n";

$mail->WordWrap = 80;

$mail->MsgHTML($body, dirname(__FILE__), true); //Create message bodies and embed images

//you can also add attachments to your mail but change file path
//$mail->AddAttachment('images/phpmailer_mini.gif', 'phpmailer_mini.gif'); // optional name

//$mail->AddAttachment('images/phpmailer.png', 'phpmailer.png'); // optional name



//call send() to send mail
$mail->Send();



PS: The above example sends mail from wetechies.2013@gmail.com to wetechies.2013@gmail.com itself.

      To send to other change $mail->to =" to_whom_you have to send";

If you don't want to take hurdle of writing script don't worry phpmailer helps you in that too.

What? Yes! it generates code for you!!! Amazing right

Once you download phpmailer class go to phpmailer/examples/  check for codegenerator.phps

Change .phps extension to .php run that file, fill out all the details


mail() of PHP, phpmailer class, send mail in php, send mail using external libraries, swift mailer disadvantages, using gmail smtp in php,

 Now fill up your google account details

mail() of PHP, phpmailer class, send mail in php, send mail using external libraries, swift mailer disadvantages, using gmail smtp in php,

SMTP Port - 465
SMTP Server - smtp.gmail.com
SMTP Security - ssl
SMTP Authentication check yes

Provide your username and password of google and click submit.

that's it!!! you are done it gives you the PHP script if authentication is successfull.
you can just run that code and send send the mail.
the generated script just include that in your PHP file :)

You are done, Now send mail to anyone with n no of attachments happily :)

Please leave your doubts or comments below :)



 
 

14 comments:

  1. Its nice blog!!But its not working me its giving error as SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl"

    please help

    ReplyDelete
  2. Hi, shweta have you provided correct username password of your google account? and also check changing ssl with tls and you are trying on your system or using any webhosting services?

    ReplyDelete
  3. Nice post.Thank you so much for sharing.Yiioverflow is a web development company.We have well expert team in Angular JS, Ionic, Yii Framework, Node JS, Laravel, PHP, MySQL, and WordPress.I you want a developer visit.. https://yiioverflow.com/

    ReplyDelete
  4. Wonderful post!!Thank you for sharing this info with us.
    Keep updating I would like to know more updates on this topic
    Very useful content, I would like to suggest this blog to my friends.

    PHP Training

    php course

    ReplyDelete
  5. Thank you for the link building list.I am going jot down this because it will help me a lot.Great blog! Please keep on posting such blog.

    private label website builder

    ReplyDelete

  6. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.PHP Developer Software in India

    ReplyDelete