The most used web application functionality is sending mails from a script. There are various email sending libraries that you can use to do so. However one of the most talked-about libraries is the PHPMailer library. However, PHP mails have their own setbacks, and so it is recommended to send emails via SMTP. it might sound easy to a tech-savvy person, but a little difficult for a person who has no idea about it.
However, if you have landed on this page, then I feel that you know a little about it or want to know. So today we are going to see how to send mail using SMTP in PHP script?
Why Use SMTP to Send Mails?
So if you know about the PHP script then you would be aware that the PHP mail() function is basically used to send emails. And so whenever you use the mail() function to send an email in PHP, your web server is involved to send it. This may lead to some sending issues like the recipient might not get the mail or so. And to overcome such delivery failure issues, SMTP comes to the rescue. It is one of the most recommended ways to send an email in PHP script. because when you send an email through SMTP, your mail server is involved rather than the webserver.
When to Use SMTP to Send PHP Mail?
The best way to send a PHP mail via SMTP is with the help of the PHPMailer Library. It offers the option to use PHP and send emails through the SMTP server. However one should always opt for this option when you are trying to build a custom mail form. And in case you are using WordPress like software, then you can choose the plugins or built-in tools to send emails via the server.
How to Send Mail Using SMTP in PHP?
You can use the PHPMailer library as it offers various configuration options to customize and configure the sending of emails as per your needs. You can send the HTML or text emails with single or multiple attachments. So let’s get started with the process to send an HTML email using SMTP in PHP.
Step 1- Use the PHPMailer Library
We are going to use the PHPMailer library to send the PHP mail via the SMTP server. You will have to include the PHPMailer library files and begin the PHPMailer object.
There is no need to download the library separately. All the files required are included in the PHP library source code only.
Step 2- SMTP Configuration
According to your SMTP server credentials, you need to give the following details-
- Server host- $mail->Host
- Username- $mail->Username
- Password- $mail->Password
- Port- $mail->Port
Step 3- Email Configuration
Next, you need to specify some basic email settings like the name and email of the sender, mail of the recipient, subject, etc. Set the isHTML() function to true if you want to send an email in an HTML format.
Step 4- Send Mail with Attachment
You can also add attachments to your mail. Just use the addAttachment() function from the PHPMailer class. If you wish to add multiple attachments, then use the same function multiple times.
Step 5- Send Your Mail to Multiple Recipients
The last step is to add the recipients to your mail. Here also you can add multiple recipients. The function that you will have to use is addAddress().
That’s it. it might look a little difficult, but the images would help you understand it better.
How to Send Mail using Gmail SMTP Server?
If you want to use the Gmail SMTP server then here are a few changes that you need to make in the Google account settings before you start with the PHP code.
Step 1- Enter into your Google account. Then open the My Account Page.
Step 2- In the Sign-in and security section and click on the Signing into Google option.
Step 3- Turn off the 2-step verification option under the Password and Sign-in method section.
Step 4- Next toggle the option Allow less secure app to on in the Connected apps and sites list.
So now you will be able to use Gmail SMTP to send mails in PHP script. You can check out the following site for the complete PHP code that you can use to send mail using the SMTP server.
I hope that this guide was helpful and now you can easily send emails in PHP via the SMTP server. Gmail SMTP using PHPMailer is the easiest option. You can try using other SMTP servers as well to send emails. In case of any query share it with us in the comment section.