Friday 22 July 2016

Emailing With PHPMailer And GMail SMTP



PHPMailer And GMail SMTP w3workers
PHPMailer And GMail SMTP

Almost every website require emailing function for sending emails to subscribers you can use emailing function in your application according to your website requirement.


1) For sending Autoresponder.

2) For sending Newsletters.
3) For Sending notification about new product.
4) For sending other information like about users account activities etc.

Emailing in PHP is very simple.

If your website hosting provider allows you to send mails from same hosting server (local mail server) in that case you can use 
default mailing function of php for sending emails.

Example of PHP mail() function



  1. <?php
  2. // Message
  3. $message = "Simple Text Message";

  4. // Calling 
  5. mail('w3workers@gmail.com', 'Subject of Message', $message);
  6. ?>


But in case if you want more flexible emailing you can use SMTP configuration with your website.


SMTP stands for Simple Mail Transfer Protocol which is used for sending emails, Various SMTP service provider allows email sending with lower cost and high inbox delivery.


List of some popular SMTP providers 



  • Amazon SES
  • MailGun
  • SendGrid
  • Sparkpost
  • Postmark
apart from this list also GMail Provides email sending from GMail SMTP server free of cost with daily sending limit upto 150 emails.

PHPMailer is the most popular open source flexible emailing library for PHP.


Below is steps for emailing with PHPMailer:


Example of subscriber form for sending Autoresponder.

 Step 1:
Create a directory name "emailer".

Step 2:

Create two php files inside directory "emailer".
index.php
sendmail.php

Step 3: 

Create html form in index.php file.



  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Emailer</title>
  6. <style>
  7. .subscriber-form{
  8.        margin-top:50px;
  9. }
  10. .txt{
  11.       width: 20%;
  12.     padding: 12px 20px;
  13.     margin: 8px 0;
  14.     box-sizing: border-box;
  15.       font-size: 16px;
  16. }
  17. .lbl{
  18.       display:block;
  19.       font-size:18px;
  20. }
  21. .btn{
  22.       width: 20%;
  23.       background-color: #4CAF50;
  24.     border: none;
  25.     color: white;
  26.     padding: 15px 32px;
  27.     text-align: center;
  28.     text-decoration: none;
  29.     display: inline-block;
  30.     font-size: 16px;
  31.     margin: 3px 2px;
  32.     cursor: pointer;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="subscriber-form">
  38. <form method="post" action="sendmail.php">
  39. <h2>Subscribe with w3workers</h2>
  40. <div class='row'>
  41. <label class="lbl" for="txt_name">Your Name</label>
  42. <input type="text" id="txt_name" name="txt_name" class="txt" placeholder="Your Name"/>
  43. </div>
  44. <div class='row'>
  45. <label class="lbl" for="txt_email">Your Email</label>
  46. <input type="text" id="txt_email" name="txt_email" class="txt" placeholder="w3workers@gmail.com"/>
  47. </div>
  48. <div class='row'>
  49. <input type="submit" id="btn_send" name="btn_send" value="Subscribe" class="btn"/> 
  50. </div>
  51. </form>
  52. </div>
  53. </body>
  54. </html>



Subscribers Form w3workers
Subscribers Form

Step 4:
Download PHPMailer open source library

Download Link:
www.github.com/PHPMailer/PHPMailer

PHPMailer GitHub w3workers
PHPMailer GitHub
Click on "Clone or download" button for downloading PHPMailer-master.zip.

Extract PHPMailer-master.zip in "emailer" directory.

and rename folder "PHPMailer-master" to phpmailer.

Step 5:
Get Gmail SMTP Details you need to set in PHPMailer

Host:
smtp.gmail.com

Username:
Your Gmail Id example : w3workers@gmail.com

Password: 
Your Gmail Account Password

SMTPSecure:
tls

port
587

Now Login your GMail account and go to Following Link:

myaccount.google.com/intro/security?pli=1#connectedapps

Allow Less Secure app in Google account

click on "Connected apps & sites" and set "Allow less secure apps: ON".

Step 6:
Now Configure PHPMailer library in sendmail.php file.

Edit: sendmail.php

  1. <?php
  2. require 'phpmailer/PHPMailerAutoload.php';
  3. if(isset($_POST['txt_email'])){
  4. $from_email='w3workers@gmail.com';
  5. $from_name='Emailer';
  6. $to=$_POST['txt_email'];
  7. if (!filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
  8.   $mail = new PHPMailer;
  9. $mail->isSMTP();                // Set mailer to use SMTP
  10. $mail->Host = 'smtp.gmail.com';  // Specify SMTP servers
  11. $mail->SMTPAuth = true;   // Enable SMTP auth
  12. $mail->Username = 'w3workers@gmail.com';  // Gmail Id
  13. $mail->Password = 'your_gmail_account_password';     
  14. $mail->SMTPSecure = 'tls';            // Enable TLS 
  15. $mail->Port =587 ;                         // TCP port  
  16. $mail->setFrom($from_email,$from_name);
  17. $mail->addAddress($to);     // Add a recipient             
  18. $mail->addReplyTo($from_email, 'Information');
  19. $mail->isHTML(true);         // Set email format to HTML
  20. $mail->Subject = 'Thanks For Subscribing';
  21. $mail->Body    = 'This is the HTML message body <b>in bold w3workers</b>';
  22. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  23. if(!$mail->send()) {
  24.     echo 'Message could not be sent.';
  25.     echo 'Mailer Error: ' . $mail->ErrorInfo;
  26. } else {
  27.     echo 'Thanks For Subscribing with us !';
  28. }
  29. } else {
  30.   echo("$to is not a valid email address");
  31. }
  32. }else{
  33. echo "Invalid Email";
  34. }

  35. ?>

So thats all done now your Gmail SMTP emailing Function Completed with PHPMailer API.

You can configure any SMTP service you want with PHPMailer Library and can create many type of Emailing Function in Your Website.


Thanks for reading if you have any question add in comments section.

No comments:

Post a Comment