How To Send Mail In PHP From Localhost Using XAMPP Server
To send mail from localhost XAMPP using Gmail, configure XAMPP after installing it.
Follow the steps below for the same.
Steps to send mail from Localhost XAMPP using Gmail:
Open the XAMPP installation directory.
Go to C:\xampp\php and open the php.ini file.
Find [ smtp ] by pressing ctrl + f.
Search and pass the following values:
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = YourGmailId@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Now, go to C:\xampp\sendmail and open the sendmail.ini file.
Find [ sendmail ] by pressing ctrl + f.
Search and pass the following values:
smtp_server=smtp.gmail.com
smtp_port=587 or 25 //use any of them
error_logfile=error.log ( Remove if there is a semicolon ; )
debug_logfile=debug.log ( Remove if there is a semicolon ; )
auth_username=YourGmailId@gmail.com
auth_password=Your-Gmail-Password
force_sender=YourGmailId@gmail.com(optional)
Here is the actual code you need to write :
Script to send mail:
<?php
$to_email = "receipientMail@gmail.com";
$subject = "Sample Mail Test From Localhost in PHP";
$body = "Hello, This is test Mail send from Localhost ( Message Part )";
$headers = "From: senderEmail";
if (mail($to_email, $subject, $body, $headers)) {
echo "Done..! Email successfully sent to $to_email .";
} else {
echo "Error..! Email Not Sent";
}
?>
Note: If you receive a warning message, configure the "Less secure apps" settings as shown below.
Sometimes not activating the 'less secure apps' is the main reason why the user did not receive the mail.
*Turning on 'less secure apps' settings as mailbox user*
Go to your (Google Account).
On the left navigation panel, click Security.
On the bottom of the page, in the Less secure app access panel, click Turn on access.
If you don't see this setting, your administrator might have turned off less secure app account access (check the instruction above).
Click the Save button.
Now your email will be sent successfully. Have a nice day and share your story in the comment box.

Comments
Post a Comment