How to send email from localhost in php

save image Export MySQL Database configure mongodb send email from localhost

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 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 = "hi@iamrohit.in"; 
$subject = "Test mail"; 
$msg = "This is simple test mail sending by phpmailer class"; 
$mailObj->AddAddress($to, 'Rohit');
$mailObj->SetFrom('from@example.com', 'Example');
$mailObj->AddReplyTo('reply-to@example.com', '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 = 'user@gmail.com';                 // 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 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