XAMPP PHP to send mail from localhost less secure app blocked

How to configure XAMPP PHP to send mail from localhost ?

This tutorial guides you on how to configure XAMPP PHP to send mail from localhost. It is possible to send mail from XAMPP localhost using sendmail package which is inbuilt within XAMPP. Let’s see how to configure XAMPP php.ini and sendmail.ini to send mail from localhost.

Configure XAMPP PHP to send mail from localhost

Let’s see how to configure Gmail mail settings in XAMPP php.ini and sendmail.ini to send Gmail mail.

First, you need to open file php.ini file either via XAMPP Control Panel -> Apache Server Module -> Config -> php.ini or from the following path C:\xampp\php\php.ini.

Then look for the following section [mail function] and make the following changes. If you look at the the changes that’s been made, I have commented the default settings and added new settings to enable Gmail mail in php.ini configuration file.

[mail function]
;SMTP=localhost
SMTP=smtp.gmail.com

;smtp_port=25
smtp_port=587

;sendmail_from = [email protected]
sendmail_from = [email protected]

;sendmail_path =
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Secondly, you need to open the sendmail.ini configuration file from the following path C:\xampp\sendmail\sendmail.ini.

Then look for the following section [sendmail] and make the following changes.

[sendmail]

;smtp_server=mail.mydomain.com
smtp_server=smtp.gmail.com

;smtp_port=25
smtp_port=587

error_logfile=error.log

debug_logfile=debug.log

auth_username=<[email protected]>
auth_password=<my-gmail-password>

[email protected]

That’s all. You are done with the configurations or settings that you need to do with XAMPP setup to send mail from localhost.

Don’t forget to restart Apache server from XAMPP Control Panel. Therefore, whatever configuration changes that you have done in php.ini and sendmail.ini files will take effect.

Finally, you need to have an php script to send Gmail mail from XAMPP localhost. You can use the following PHP script to test the above configuration changes.

mail.php

<?php
// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 50 characters
$msg = wordwrap($msg,50);

// send email
mail("[email protected]","My subject",$msg);
?>

Note, you might get “Critical Security Alert” email as shown below. And upon clicking “Check Activity” it will redirect you to myaccount.google.com and shows message which says “Less secure app blocked“. Therefore, you won’t receive the email as expected and you need to allow less secure app access.

XAMPP PHP to send mail from localhost less secure app blocked

Allow access to less secure apps – Gmail

To allow or enable less secure apps blocked from Gmail in sending mails, you need to Turn ON access for less secure apps.

To turn ON access you need to follow the below steps.

1. Log in to your Google Account ( myaccount.google.com )

2. Select Security

3. Then go to “Less secure app access” section and Turn ON access as shown below.

Configure XAMPP PHP to send mail from localhost

That’s All. You should see email in your Gmail to address inbox. Hope this tutorial helped you to configure XAMPP PHP to send mail from localhost :-).

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments