How To Guide to Enable Email Sending with XAMPP and PHP

Hi, my pals You’ll discover how to set up XAMPP to send mail from localhost in PHP today in this blog. For those who are unaware, XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. We can work on local servers using XAMPP and test PHP code and MySQL database projects on local copies of websites.

Developers must send emails as part of the experiment, and as we all know, using PHP to send mail from localhost might be much more difficult if we don’t know how to set up XAMPP correctly.

You can see this blog post, How to Send Email in Node.js with Nodemailer & Gmail, before beginning this one if you wish to send an email using Node.js apps.

After installing XAMPP, we need to setup it in order to use it to send mail from localhost. Two files, one called PHP and the other Sendmail, need to be modified in order to set up the XAMPP server to send mail from the local host.

You Might Like This:

First, navigate to the installation location for XAMPP, enter the XAMPP folder, and then repeat the following steps: XAMPP is now installed in the C directory.

  1. Navigate to (C:xampp\php) and launch the PHP configuration setting file. Next, locate the [mail function] by swiping down or by using the keyboard shortcut ctrl+f to search it immediately. Finally, locate the subsequent lines and enter these values. Keep in mind that there could be a semicolon; just remove it at the beginning of each line, as seen below.
[mail function]
For Win32 only.
http://php.net/smtp
SMTP=smtp.gmail.com
http://php.net/smtp-port
smtp_port=587
sendmail_from = your_email_address_here
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

That’s all there is to this file. To save it and close it, use Ctrl+S.

  1. Next, navigate to (C:\xampp\sendmail) and launch the sendmail configuration file. Next, locate sendmail by swiping down or by using the keyboard shortcut Ctrl+f to search it immediately. Finally, locate the subsequent lines and enter these values. Keep in mind that there could be a semicolon; just remove it at the beginning of each line, as seen below.
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=your_email_address_here
auth_password=your_password_here
force_sender=your_email_address_here (it's optional)

That’s all for this file, press ctrl+s to save this file and then close it. After all changes in the two files, don’t forget to restart your apache server.

You’ve finished making the necessary adjustments to these files. to verify whether the modifications you’ve made are accurate. Create a PHP file first, then paste the following scripts into it. Make sure the PHP file has the.php extension. Once the codes are pasted, enter your information in the designated variables. Put the email address of the recipient in the $receiver variable and the email subject and do in the $subject variable, accordingly.

<?php
$receiver = "receiver email address here";
$subject = "Email Test via PHP using Localhost";
$body = "Hi, there...This is a test email send from Localhost.";
$sender = "From:sender email address here";

if(mail($receiver, $subject, $body, $sender)){
    echo "Email sent successfully to $receiver";
}else{
    echo "Sorry, failed while sending mail!";
}
?>

Simply click this PHP file to open it in your browser after finishing these procedures. The message “Email sent successfully to..” will display if your mail is sent successfully, while the message “Sorry, failed while sending mail!” will show if it is not.

Verify whether the recipient of your email received it after sending it. If so, fantastic—all of the adjustments were executed flawlessly. If not, make sure that every modification you made previously is accurate.

Note: On May 30, 2022, Google discontinued the “Less secure apps” functionality. Hence, in order to use Gmail to send mail from localhost, you must take an additional step and perform certain file modifications.

You must use an app password that is 16 characters long in place of your Google account password, which should be entered as auth_password in the sendmail configuration file.

Go to manage your Google account > to establish an app password. Select Security. Click “Sign in to Google,” then choose App passwords > After choosing App & Device, a 16-character code will appear on screen; this is your password. Read the Official Google Article for more information about setting app passwords.

Bipul author of nerdy tutorial
Bipul

Hello my name is Bipul, I love write solution about programming languages.

Articles: 146

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *