Simple But Work | How to Send Email Using PHP in Your Local / Server
I’ve find out how to create simple sending email using PHP in many articles, I try it and i failed, untill i found an article that explain with very clear. Definately, i’m still got problem and i got the solution to solve this.
I start with requirement that you should prepare before we try to build this and By the way, i using windows 10 for this example but you still practice this in linux.
Requirement
- Install Xampp | you can download here make sure you download Xampp with PHP greater than v5.3
- Text Editor | you can use atom, vscode, notepad++
- if you’re using linux, make sure you install apache server
Let’s Do It
- For sending email, we will use an application in windows called sendmail that usually include on xampp installation. You can check with open your xampp installation folder. usually on C:\xampp\ then check is sendmail folder exist or not.
- If folder didn’t exist, then download sendmail.zip. Open your xampp installation folder (usually on C:\xampp\ ), then create a folder named “sendmail”.
- Extract sendmail.zip on this folder. then open the sendmail.ini
- Configure it as following
- smtp_server=smtp.gmail.com
- smtp_port=465
- smtp_ssl=ssl
- default_domain=localhost
- error_logfile=error.log
- debug_logfile=debug.log
- auth_username=[your_gmail_account_username]@gmail.com
- auth_password=[your_gmail_account_password]
- pop3_server=
- pop3_username=
- pop3_password=
- force_sender=
- force_recipient=
- hostname=localhost
You do not need to specify any value for these properties : pop3_server, pop3_username, pop3_password, force_sender, force_recipient. Make sure the error_logfile and debug_logfile keep blank if you have already send emails. Enable these log is used to see if you don’t get able to send email using sendmail.
- Dont forget to save,
- Now we setting email configuration on php.ini into our configuration above. Let’s open your xampp, and click configure → php.ini
- After open php.ini, use search and type mail function you will direct into this line,
- Adding new configuration as picture below
sendmail_path = “C:\xampp\sendmail\sendmail.exe -t -i”
- Finish, Let’s create new PHP file and configure as follow,
You can change value of these variabel as you want. Then save and let’s try.
- Open on your browser, then see what the message. If you got this message, you success doing this practice.
let’s check on email, you can see like this.
yaay you did it. But if you got this message. You need to jump into troubleshoot on this page.
Troubleshoting
Every problem from sending email using PHP will store on 3 files, debug, crash, error but for easier to read you can open error.txt file and you will get the message.
Username and Password Not Accepted
- This error usually happend if you fill auth_username and auth_password on sendmail.ini with wrong value. Re -check and run again.
- If you make sure your email and password right, then open this link. Don’t forget to open email first. you will direct into this gmail help.
- Click on allow less secure apps to access your account. then you will direct into this articles.
- click on Less secure app access. then you will see this. This message is about if you use this less secure app access. Some apps and device use less secure sign-in technology. It make thirt party like code that we create blocked. So if you wanna send message to email using PHP, you must allow this by clicking button to turn on it.
- Then, open this again and click http://www.google.com/accounts/DisplayUnlockCaptcha to unlock your account and give permission to third party.
After click, you will see this. Just click Continue and Done.
You will get email, and confirm that’s you. And try to send again. and voila you will got success message.