Send an Email via Gmail SMTP Server in PHP using PHPMailer

In this tutorial, we going to see how to send an email via Gmail SMTP server in PHP using PHPMailer. We send an email with different mail transfer libraries using PHP. In this, We are going to use PHPMailer. These libraries give excellent features like SMTP authentication and more.

Send an Email via Gmail SMTP Server in PHP using PHPMailer

   

Using Gmail SMTP server sending emails by using PHPMailer. PHPMailer library is famous because of its excellent features are available. Some of those features are below,

1. PHPMailer supports both HTML content and plain text.
2. We can able to use an array of email addresses like to, cc, bcc, reply-to.
3. It also gives Secure/MIME encryption with email validation, SMTP authentication including multiple language support.

Before using Gmail SMTP.

To send an email you can use the mail server of another host but for this, you need first to have authentication. E.g. You need to send an email from Gmail mail server first you require Gmail account.

Gmail SMTP needs the TLS encryption so we set it accordingly. If you want to send mail via SMTP, you want to find the Hostname, Port number, SMTPSecure encryption type if authentication is needed you also want the username and password.

Now need to do some changes in your Gmail account settings. First login and open your Gmail account settings.

1. We want to off the 2-Step Verification before using it.

2-Step Verification gmail smtp

2. Next, go to less secure apps. The default Gmail less secure apps are in off. So here, you need to keep Allow less secure apps: ON.

less secure apps google

Required files are needed:

Now we going to use a PHPMailer library which you can download on GitHub.

In this, we going to send an mail using TextBox and TextArea without saving data in the database. Below you can see the full code.

index.php
mail.php

The below code is used to require the PHPMailerAutoload file. This PHPMailerAutoload file is available in PHPMailer library.

style.css

Conclusion:

I believe in this tutorial you feeling happy with sending an email via Gmail SMTP Server in PHP using PHPMailer. Using other SMTP servers also you can try to send mail.

Leave a Reply

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