How to Send Mail from Localhost XAMPP Server with PHP

In our previous tutorial, we have explained How to Send Email with Attachment in PHP. In this tutorial, you will learn how to send mail from localhost with PHP using XAMPP Server.

As the local development environment is developers favorite home, so most of prefers mail sending from local server. The mail sending can be handled easily using built-in PHP mail() function. But this may not work sometimes and we need to make some additional changes to work mail send from localhost.

So here in this tutorial, we will explain how to send email from local xampp server using PHP mail() function. We will also explain about additional changes needed in php.ini file to work it.

Prerequisites

Make sure to install following before stating with this.

  • XAMPP (PHP latest version)
  • A code editor (such as Visual Studio Code or Atom or Sublime Text)
  • A web browser (Google Chrome or Mozilla Firefox or Internet Explorer or Safari etc.)

Step 1: Configure Email Server or SMTP Settings

For sending mails from XAMPP server, we need to configure SMTP settings in PHP.INI file. The SMTP settings will define the email server that used to send the email.


  1. We will open the XAMPP Control Panel and click on the Config button for the Apache server. Then click on the PHP.INI to open the file.
  2. We will search for the [mail function] section in the PHP.ini file. Then uncomment the following lines by removing the semicolon (;) at the beginning of each line.
  3. [mail function]
    ;SMTP = localhost
    ;smtp_port = 25
    ;sendmail_from = localhost
    
  4. We will replace localhost with the hostname or IP address of our SMTP server. As here we will use GMAIL SMTP Server. So will do following changes.
  5. [mail function]
    SMTP = SMTP=smtp.gmail.com
    smtp_port = 25
    sendmail_from = phpzag@gmail.com
    
  6. We will also do authentication changes related to SMTP server. We will open sendmail.ini which is located at c:\xampp\sendmail\sendmail.ini. We will search for [sendmail] and do following changes:
  7. smtp_server=smtp.gmail.com
    smtp_port=25
    auth_username=phpzag@gmail.com
    auth_password=XXXXXXX
    force_sender=phpzag@gmail.com
    
  8. Then restart your XAMPP Server.

Step 2: Write PHP Program to Send Email

After configuring SMTP details, we will create a PHP script using mail() function to send email. We will open code editor and create a send_email.php file at C:\xampp\htdocs. Then write PHP code to send email.

<?php
$to = 'contact@phpzag.com';
$subject = 'PHP Email';
$message = 'I am sending email from Local XAMPP server.';
$headers = 'From: phpzag@gmail.com.com' . "\r\n" .
           'Reply-To: phpzag@gmail.com' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $message, $headers)) {
   echo 'The email sent successfully!';
} else {
   echo 'Error: Email sending failed.';
}
?>

You can replace the values with your email address and message.

Step 3: Run Program

We will start Apache Server in the XAMPP Control Panel. Then open web browser and navigate to http://localhost/send_mail.php.


Conclusion

In this tutorial, we have explained how to configure XAMPP server to send mail from localhost using the PHP mail function. With above steps you can test the email send from your local server. The Servers settings may vary. So if you face any issue, you can add your comment in comment section.

Leave a Reply

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