Here is Latest Post with How to send email from localhost in php with xampp, wamp, Lamp. If you want to send mail from Localhost then you have to set SMTP configuration in your php.ini file or you have to use external SMTP server. There are lot’s of external SMTP server are available like Gmail, Hotmail, Yahoo etc. They allow you to send email by using their SMTP configuration, you only need to configure some parameter in your local configuration or in your phpmailer code.

Also Read: How to Create Multiple XML sitemap in php

Here is Step for send email from localhost in php

If you are going to use Gmail SMTP server, you need to change account access for less secure apps By doing following steps.
1. Login to your google account.
2. Go to the Less secure apps settings page
3. From Access for less secure apps section, select Turn on.

First download latest phpmailer class form here

After that include PHPMailerAutoload.php on your mailing page and configuring SMTP settings.

require 'PHPMailerAutoload.php'; 
$mailObj = new PHPMailer;  
$to = ""; 
$subject = "Test mail"; 
$msg = "This is simple test mail sending by phpmailer class"; 
$mailObj->AddAddress($to, 'Rohit');
$mailObj->SetFrom('', 'Example');
$mailObj->AddReplyTo('', 'Reply-Example');
$mailObj->Subject = $subject;
$mailObj->AltBody = 'To view the message, please use an HTML compatible email viewer!'; 
// SMTP Settings
$mailObj->isSMTP();                                      // Set mailer to use SMTP
$mailObj->Host = ';';  // Specify main and backup SMTP servers
$mailObj->SMTPAuth = true;                               // Enable SMTP authentication
$mailObj->Username = '';                 // SMTP username
$mailObj->Password = 'password';                           // SMTP password
$mailObj->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mailObj->Port = 587;
if(!$mailObj->Send()) {
echo "There was an error sending the e-mail";
} else {
echo "E-Mail has been sent successfully";

Sending attachment on emails

PHPMailer allow you send multiple attachment on email by adding following lines on your mailer code.

$mailObj->AddAttachment("image-1.jpg");  //Path to file
$mailObj->AddAttachment(("file/intro.pdf", 'Introductory Letter'); //Path to file, // Optional name.

By using above method you can easily send emails form localhost without changing in your php configuration files, It is better idea to test all emailing system in your project locally and then moved fully furnished project on production this will surely reduce your project implementation time, Hope this will help you to send email from local

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.