How to send email from localhost in php (XAMPP, WAMP, LAMP)

You may have face trouble sending email from your localhost server XAMPP, WAMP, LAMP. Because When you setup apache in local system it won’t by default setup for SMTP server . So if you want to send email from your localhost you must have to setup 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.



send-email-localhost-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 https://www.google.com/settings/security/lesssecureapps
3. From Access for less secure apps section, select Turn on.

First download latest phpmailer class form here https://github.com/PHPMailer/PHPMailer

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

<?php
require 'PHPMailerAutoload.php'; 
$mailObj = new PHPMailer;  
$to = "[email protected]"; 
$subject = "Test mail"; 
$msg = "This is simple test mail sending by phpmailer class"; 
$mailObj->AddAddress($to, 'Rohit');
$mailObj->SetFrom('[email protected]', 'Example');
$mailObj->AddReplyTo('[email protected]', 'Reply-Example');
$mailObj->Subject = $subject;
$mailObj->AltBody = 'To view the message, please use an HTML compatible email viewer!'; 
$mailObj->MsgHTML($msg);
 
// SMTP Settings
$mailObj->isSMTP();                                      // Set mailer to use SMTP
$mailObj->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mailObj->SMTPAuth = true;                               // Enable SMTP authentication
$mailObj->Username = '[email protected]';                 // SMTP username
$mailObj->Password = 'password';                           // SMTP password
$mailObj->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mailObj->Port = 587;
 
$mailObj->Send();
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 local host 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 🙂

If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Posted in PHP