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
- <?php
- // Message
- $message = "Simple Text Message";
- // Calling
- mail('w3workers@gmail.com', 'Subject of Message', $message);
- ?>
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
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.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Emailer</title>
- <style>
- .subscriber-form{
- margin-top:50px;
- }
- .txt{
- width: 20%;
- padding: 12px 20px;
- margin: 8px 0;
- box-sizing: border-box;
- font-size: 16px;
- }
- .lbl{
- display:block;
- font-size:18px;
- }
- .btn{
- width: 20%;
- background-color: #4CAF50;
- border: none;
- color: white;
- padding: 15px 32px;
- text-align: center;
- text-decoration: none;
- display: inline-block;
- font-size: 16px;
- margin: 3px 2px;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <div class="subscriber-form">
- <form method="post" action="sendmail.php">
- <h2>Subscribe with w3workers</h2>
- <div class='row'>
- <label class="lbl" for="txt_name">Your Name</label>
- <input type="text" id="txt_name" name="txt_name" class="txt" placeholder="Your Name"/>
- </div>
- <div class='row'>
- <label class="lbl" for="txt_email">Your Email</label>
- <input type="text" id="txt_email" name="txt_email" class="txt" placeholder="w3workers@gmail.com"/>
- </div>
- <div class='row'>
- <input type="submit" id="btn_send" name="btn_send" value="Subscribe" class="btn"/>
- </div>
- </form>
- </div>
- </body>
- </html>
Subscribers Form |
Step 4:
Download PHPMailer open source library
Download Link:
www.github.com/PHPMailer/PHPMailer
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
- <?php
- require 'phpmailer/PHPMailerAutoload.php';
- if(isset($_POST['txt_email'])){
- $from_email='w3workers@gmail.com';
- $from_name='Emailer';
- $to=$_POST['txt_email'];
- if (!filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
- $mail = new PHPMailer;
- $mail->isSMTP(); // Set mailer to use SMTP
- $mail->Host = 'smtp.gmail.com'; // Specify SMTP servers
- $mail->SMTPAuth = true; // Enable SMTP auth
- $mail->Username = 'w3workers@gmail.com'; // Gmail Id
- $mail->Password = 'your_gmail_account_password';
- $mail->SMTPSecure = 'tls'; // Enable TLS
- $mail->Port =587 ; // TCP port
- $mail->setFrom($from_email,$from_name);
- $mail->addAddress($to); // Add a recipient
- $mail->addReplyTo($from_email, 'Information');
- $mail->isHTML(true); // Set email format to HTML
- $mail->Subject = 'Thanks For Subscribing';
- $mail->Body = 'This is the HTML message body <b>in bold w3workers</b>';
- $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
- if(!$mail->send()) {
- echo 'Message could not be sent.';
- echo 'Mailer Error: ' . $mail->ErrorInfo;
- } else {
- echo 'Thanks For Subscribing with us !';
- }
- } else {
- echo("$to is not a valid email address");
- }
- }else{
- echo "Invalid Email";
- }
- ?>
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.
Thanks for reading if you have any question add in comments section.
No comments:
Post a Comment