3 years ago
Zaid Bin Khalid
7,498 Views
3
In this tutorial, you will learn how to send an email from your localhost using PHP . Many developers have this problem whenever they want to test the code where they are sending an email.
You can test your code online on your server where already all the things are set up or you can test your code on localhost (on your local system or machine) .
Usually, developers use some servers to run server-side language on a local machine. I am using Wamp , Xampp server on my local machine. if you are using this type of server then you need to set up a few things or follow the few steps which I mentioned below.
What you Need for setup
You must have a server like I have Xampp on my local PC. I am using Gmail SMTP for setting up email from the server, so you must have any SMTP server or Gmail Account . You must know the username (email) and password. You can edit the .ini files in text editor.
smtp_sever : The SMTP Host server name like , smtp.gmail.com
smtp_port : The port number (Ex port: 465) auth_username : Your SMTP usernameauth_password : Your SMTP password
Step# 1:
In the first step, you need to open the php.ini file where you can set some SMTP detail that you will use. This file you will find in your server it could be Wamp , Xampp , Lamp any just find the file and open it. In my case, I have a XAMPP server and I installed this server on my S Derive (I am using windows OS ).
In PHP we can use the mail() function to send an email but from localhost, you can’t send an email with this configuration.
sendmail_from (Add your gamil email ID) sendmail_path (Give the path of your sendemail exe with -t flag)
In my case, the path of sendemail.exe is given below you must change the path with your directory structure.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "\"D:\server1\sendmail.exe\" -t"
Path of sendemail.exe
Step# 2:
In the second step, we have to update the sendemail.exe file with GMAIL SMTP settings. Click here to see the official SMTP details of GMAIL . Open this file and change the below parameters.
smtp_server smtp_port auth_username (I mentioned [email protected] this email not exist) auth_password force_sender (Optional) force_recipient (Optional)
Step# 3:
After these changes restart the server and try the below code to send an email. Other posts related to send email are listed below.
Send an Email using PHP mail() function Validate Emails and send using PHPMailer and JQuery Ajax Free HTML email template
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: LearnCodeWeb <[email protected] >';
$headers[] = 'From: LearnCodeWeb <[email protected] >';
$headers[] = 'Cc: [email protected] ';
$headers[] = 'Bcc: [email protected] ';
mail('[email protected] ', 'I am testing Learncodeweb code', 'Thank you LCW to save my life', implode("\r\n", $headers));
Important Note:
If you are not able to get an email after testing the code you must set up a few things on your Gmail Account . You must enable the Less Secure App ON to click here to check the settings . And if you have already enabled the two-step verification on your account then disable the two-step verification after that you will be able to ON/OFF the Less Secure App settings.
3 years ago
Zaid Bin Khalid
7,498 Views
3