How To Send Email With PHP From Localhost Using New Gmail’s App Password Feature— 2022.[Part 1]
As a developer you might need solutions to help you test your product locally. This article gives you as a developer, a solution to test mail sending capabilities on your website. In this article I will be using the following softwares:
- XAMPP
- Gmail
I have not included PHP for obvious reasons(PHP is shipped with XAMPP).
1. Setting Up Your Gmail Account
We need to first set up our gmail account to allow the XAMPP sendmail service to access it. In May 2022, Gmail disabled the Less Secure App option. This article gives you an alternative to this option but your gmail accout needs to have 2-Step Verification enabled.
The steps to set up your gmail account are:
- Open up your gmail account and choose “Manage your Google Account”.
- Select on the Security tab.
- Under Signing in to Google card, make sure 2-Step Verification is on.
4. Select App passwords just below the “2-Step Verification and authenticate your account.
5. You will get a simple form. Under the Select app dropdown, choose Mail.
6. Under the Select device dropdown, choose “Other” and type in the name for your app. I.e “Xampp” and Generate the password.
7. Upon generation, a pop up will appear with the password highlighted in yellow-orange. Copy the passwordand store it for later use.
2. Setting up XAMPP
XAMPP is an open source and cross-platform solution for full stack web applications using PHP and MySQL. We will be using it’s server capabilities to set up our mail sender settings.
First you need to make a few changes to XAMPP’s configuration settings.
- Open php.ini file accessible from C:\xampp\php.
- Find the mail function within the file and make the changes below:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = youremail@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Remember to remove semicolons infront of the edit lines only.
4. In C:\xampp\php\php.ini
find extension=php_openssl.dll
and remove the semicolon from the beginning of that line to make SSL working for gmail in localhost.
3. Open sendmail.ini file accessible from C:\xampp\sendmail.
4. Within the file, make changes to the following lines:
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=youremail@gmail.com
auth_password=******* //The app pasdword generated earlier.
This is the end of part 1. In the next Part 2 we will create a contact form to send email straight from a website.