How to Send Mail from Localhost XAMPP

We have seen in many projects we need to implement email functionality in localhost. New developers think this is so complicated but it’s not. In this article, I will let you know how you can send email using localhost. This is very simple and you don’t need to worry about that. Only you have to follow below steps:

As you know, you need to configure the Xampp and I believe you have already configured the Xampp in your system. So let’s start.

  • Open Xampp control panel
  • Open the php.ini file. There are 2 methods to open the php.ini file. The simple method is to click on apache config, it will show you some files and you need to select PHP(php.ini). It will open the php.ini file. Please see the below screenshot. 
  • Search [mail function] using ctrl+f
  • Configure the settings, you need to uncomment smtp_port, sendmail_from, and sendmail_path and set the value as given below.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=587

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = skytechbot@mail.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
  • Goto the C:\xampp\sendmail location and open sendmail.ini file.
  • Search [sendmail] using ctrl + f
  • Configure the settings, you need to uncomment smtp_port, sendmail_from, and sendmail_path and set the value as given below.
smtp_server=smtp.gmail.com
smtp_port=587
auth_username=your_email@mail.com
auth_password=your-gmail-password
error_logfile=error.log
debug_logfile=debug.log
force_sender=your@gmail.com
hostname=localhost

Send Email Script:

I will let you know the 2 options to send an email:

Using simple PHP mail function

<?php

$to = "receipient@mail.com";
$subject = "Test Email from localhost using simple php mail";
$body = "This is my test email sent from localhost using simple php mail function";
$headers = "From: sender@mail.com";
 
if (mail($to, $subject, $body, $headers))
    echo "Email successfully sent";
else 
    echo "failed";

?>

Using SMTP method

<?php

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();                            
$mail->Host = 'smtp.gmail.com';                             
$mail->Username = 'your_email@mail.com';          
$mail->Password = 'Your Password'; 
$mail->SMTPSecure = 'tls';               
$mail->Port = 587;      

?>

Please Refer to the article Send Email using the PHPMailer Library and Gmail SMTP Server

Thanks for reading, feel free to reach out to me for any comments and suggestions. I hope you found the article helpful and you were able to send email successfully using the localhost Xampp.

Leave a comment

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