How To Send Mail From Localhost XAMPP in PHP | New method to sendmail in localhost

It is very easy to send mail through online medium, open gmail, enter the email of the one you want to send and send it easily but by coding in php programming language it becomes a bit difficult to send mail through localhost.

How To Send Mail From Localhost XAMPP in PHP
Send mail from localhost in php

Hello friends my name is Ramakrishna Sahu and today we are going to see how to send mail with the help of google smtp and localhost and php. 

See, earlier when we used to send mail from localhost, after turning on Google's less secure app access, we could easily send mail by entering our Gmail and password in C:\xampp\Sendmail\sendmail.ini file, but Both Google and Yahoo have blocked less secure app access in their SMTP so that you can't send mail from localhost and from any less secure app or website. 

That's why today we are going to tell you how to send mail from localhost without less secure app access. So let's see the new method to send emails from localhost without any delay.

How to send mail from localhost in PHP

To send mail from localhost PHP has a predefined variable store with the help of which we can send mail. To send mail from localhost, some configuration has to be done in php.ini and mailsend.ini files. follow the below steps :

Step 1 - If any localhost server (xampp) will be on in your system, then turn it off.

Step 2 - Click on the location where the xampp or www folder is located, then open the php.ini configuration file in any text editor inside the PHP folder.

Step 3 - Press ctrl + f to find the mail function.

Step 4 - Search & pass the below-mentioned values:

  • SMTP=smtp.gmail.com (google smtp or yahoo smtp)
  • smtp_port=587 (25,993 is this optional.)
  • sendmail_from = milopleInc@gmail.com /*Your gmail id*/
  • sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" (first locate sendmail.exe in your xampp installation directory)

Step 5 - Save and close the php.ini configuration file.

Now half of our work is done, now we have to make some changes in mailsend.ini configuration file.

Step 6 - Open sendmail.ini config file inside C:\xampp\sendmail.

Step 7 Press ctrl + f & find sendmail.

Step 8 - Search & pass the below-mentioned values.

  • smtp_port=587  /* or 25 */
  • error_logfile=error.log  /*If it is in the comment then remove it from the comment (The comment is displayed with a semicolon (;)) */
  • debug_logfile=debug.log  /*If it is in the comment then remove it from the comment */
  • auth_username=rkonline.in@gmail.com  /*Your Gmail id*/
  • auth_password=**********  /*Your Gmail password*/
  • force_sender=rkonline.in@gmail.com  /*Optional*/

Step 9 - Save and close the sendmail.ini configuration file.

PHP code To send mail from localhost using gmail

  • now start or restart your xampp server to fetch values of the file we saved.
  • Open any text editor like Sublime or VSstudio.
  • create a new php file example-mail.php inside C:\xampp\htdocs\
  • Copy the below code and paste it in your example-mail.php file and save it.

<?php
$to_email = "receipient@gmail.com";
$subject = "Test email to send from XAMPP";
$body = "Hi, This is test mail to check how to send mail from Localhost Using Gmail ";
$headers = "From: sender email";

if (mail($to_email, $subject, $body, $headers))

{
    echo "Email successfully sent to $to_email...";
}

else
{
    echo "Email sending failed!";
}
?>

 It is very easy to send mail through online medium, open Gmail, enter the email of the one you want to send and send it easily but by coding in PHP programming language it becomes a bit difficult to send mail through localhost.

Hello friends my name is Ramakrishna Sahu and today we are going to see how to send mail with the help of google SMTP and localhost and PHP. 

Read Also Introduction to HTML

See, earlier when we used to send mail from localhost, after turning on Google's less secure app access, we could easily send mail by entering our Gmail and password in C:\xampp\Sendmail\sendmail.ini file, but Both Google and Yahoo have blocked less secure app access in their SMTP so that you can't send mail from localhost and from any less secure app or website. 

That's why today we are going to tell how to send mail from localhost without less secure app access. So let's see the new method to send email from localhost without any delay.

How to send mail from localhost in PHP

To send mail from localhost PHP has a predefined variable store with the help of which we can send mail. To send mail from localhost, some configuration has to be done in php.ini and mailsend.ini files. follow the below steps :

Step 1 - If any localhost server (xampp) will be on in your system, then turn it off.

Step 2 - Click on the location where the xampp or www folder is located, then open the php.ini configuration file in any text editor inside the PHP folder.

Step 3 - Press ctrl + f to find the mail function.

Step 4 - Search & pass the below-mentioned values:

  • SMTP=smtp.gmail.com (google smtp or yahoo smtp)
  • smtp_port=587 (25,993 is this optional.)
  • sendmail_from = milopleInc@gmail.com /*Your gmail id*/
  • sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" (first locate sendmail.exe in your xampp installation directory)

Step 5 - Save and close the php.ini configuration file.

Now half of our work is done, now we have to make some changes in mailsend.ini configuration file.

Step 6 - Open sendmail.ini config file inside C:\xampp\sendmail.

Step 7 Press ctrl + f & find sendmail.

Step 8 - Search & pass the below-mentioned values.

  • smtp_port=587  /* or 25 */
  • error_logfile=error.log  /*If it is in the comment then remove it from the comment (The comment is displayed with a semicolon (;)) */
  • debug_logfile=debug.log  /*If it is in the comment then remove it from the comment */
  • auth_username=rkonline.in@gmail.com  /*Your Gmail id*/
  • auth_password=**********  /*Your Gmail password*/
  • force_sender=milopleInc@gmail.com  /*Optional*/

Step 9 - Save and close thesendmail.ini configuration file.

PHP code To send mail from localhost using gmail

  • now start or restart your xampp server to fetch values the file we saved.
  • Open any text editor like Sublime or VSstudio.
  • create a new php file example-mail.php inside C:\xampp\htdocs\
  • Copy the below code and paste it in your example-mail.php file and save it.

<?php
$to_email = "receipient@gmail.com";
$subject = "Test email to send from XAMPP";
$body = "Hi, This is test mail to check how to send mail from Localhost Using Gmail ";
$headers = "From: sender email";

if (mail($to_email, $subject, $body, $headers))

{
    echo "Email successfully sent to $to_email...";
}

else
{
    echo "Email sending failed!";
}
?>

If you have followed my given instruction correctly then you have completed almost 90% of the work but our important work is still pending, without this, you cannot send mail, you must be seeing some error.

The steps of Google Generate App Password to send email from localhost 

So let's get it done quickly

  • First of all, we have to open our google account
  • Click on Manage Google Account
  • In the left navbar, you will see the link to Security, click on it
  • Scroll down and turn on the two-step verification step.
  • After 2-Step Verification is on, click on App Password just below it.
  • Now you will be asked to log in again, type your Gmail password and login and click on the next button.
  • On clicking, some such options will be visible in front of you, Select the app and device for which you want to generate the app password. In Select app you have to select your website or app or click on generate button by clicking on other will do.
  • Now open sendmail.ini
  • And paste the generated password here in user_password and save and close
  • the open browser and go to localhost/example-mail.php 

Congratulations Email Send Successfully

If the email send message shows then your process is complete If mail sending fails then you have not followed your instruction properly

If you get an error, you can ask us by taking a screenshot in the comment box.

If you liked this post then please do not forget to share and follow this post, now see you in the next tutorial till then Goodbye.

Post a Comment

0 Comments