Localhost is used as a development server to develop a web application. All the functionality of the web application is tested on the localhost server before moving it to the production server. But, the problem arises when email functionality needs to be tested on the localhost server. Generally, the email sending feature is not working with the PHP built-in functions in localhost.
If the web application is built with PHP, the mail() function is used to send email from the script using PHP. But the PHP mail() function will not work in localhost. In this tutorial, we’ll show how you can send email from localhost in PHP. Using this example script you can send email from any localhost server (XAMPP, WAMP, or any others) using PHP.
We will use the PHPMailer library to send emails from localhost using PHP. The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.
SMTP Server Credentials:
Before getting started create an email account on your server and collect the SMTP credentials (Host, Port, Username, Password, etc.) that will require to be specified in the code later.
The following code snippet will send HTML email from localhost using PHPMailer.
$mail->setFrom
).$mail->addAddress
).$mail->Subject
).$mail->Body
).send()
method of PHPMailer class to send an email.<?php
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Include library files
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
// Create an instance; Pass `true` to enable exceptions
$mail = new PHPMailer;
// Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'email_password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
// Sender info
$mail->setFrom('sender@example.com', 'SenderName');
$mail->addReplyTo('reply@example.com', 'SenderName');
// Add a recipient
$mail->addAddress('recipient@example.com');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
// Set email format to HTML
$mail->isHTML(true);
// Mail subject
$mail->Subject = 'Email from Localhost by CodexWorld';
// Mail body content
$bodyContent = '<h1>How to Send Email from Localhost using PHP by CodexWorld</h1>';
$bodyContent .= '<p>This HTML email is sent from the localhost server using PHP by <b>CodexWorld</b></p>';
$mail->Body = $bodyContent;
// Send email
if(!$mail->send()) {
echo 'Message could not be sent. Mailer Error: '.$mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
Note that: If you want to use Gmail as an SMTP server, set your Google email address as SMTP username and password as SMTP password.
Send Email via SMTP Server in PHP using PHPMailer
You can send emails with multiple attachments from localhost with PHPMailer.
// Add attachments
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
I don’t want to enable less secure apps, i m using my company mail account to send mails then what to do?
Hey can this code be used to send OTP to the user? Can you specify the steps for the same?
Hi!
Can you give us some example contact form using gmail in locallhost
Thank you!
Great stuff guys! Struggled with it for days, stumbled upon your website, followed the instructions, and guess what!? Thanks a lot!
how to change SMPT settings
Message could not be sent.Mailer Error: SMTP connect() failed.
This error occurred because of wrong SMTP configuration. Check the SMTP settings, whether it configured correctly.
thanks a lot it is working
Hi, Thank you CodexWorld.. It worked for me.. i can send email from localhost.
Thanks bro.. successful
Can you send me ( require ‘PHPMailer/PHPMailerAutoload.php’; ) file or inform me where it is.
Download the source code, all the necessary files including PHPMailerAutoload.php are included in it.
Can i ask question? It is possible that i can see name and comment in my gmail? Yes the email is working but how about the name and comment section in my contact form? Please reply. Thanks in advance! 🙂
If you want a contact form email script, see this tutorial – https://www.codexworld.com/create-simple-contact-form-php-send-email/
Hi,
I have $mail->addAddress($emails[$i]); inside a loop, where I want to send each and every email to one person only. However, I found out that this operation keeps the old emails and adds a new email in every loop.
Could you please tell me how do I do if I want to assign only one new email and discard the old email in every loop?
Thanks.
Add the following line of code after mail send (
$mail->send()
).Hi,
I have the following script:
$bodyContent = ‘Please click this link to activate your account: Activate‘;
However, in the email received the link is not clickable and when I hover it, it does not show the address at the bottom left of the browser.
Thanks a lot.
You have specified the wrong formatted URL in Activate anchor. Provide the correct link in Activate anchor.
hi, its working properly.. but how can i send a mail to more recipient… please tel me how to send mail to more people
You can send an email to multiple recipients by using
addAddress()
method.Just wanted to take a minute to thank you. This is exactly what I was looking for and it works great. Thanks you 😀
Greeting,
I face missing opensll problem. I did go through several method to enable it or install it. Still couldn’t solve it. Looking forward if you can suggest me something that I can do about this.
To solve OpenSSL error you need to turn on Access for less secure apps in Google security settings.
Problem resolved by using this, if someone is using PHP MAILER…..
$mail->AddEmbeddedImage(‘img/1book_logo.png’,’1book_logo’);
$bodyContent = ”;
$mail->Body = $bodyContent;
Hello! I experience problems including phpMailer in my script. It works well when I follow example and emails are being sent but when I add this script to mine and change body, subject and addresses to variables that does not work at all. In addition, the path to PHPMailerAutoload.php in my redactor turns red despite the fact that it is absolutely correct.
Please kindly tell me what I have to set more, thanks
I tried your email code in php. but it showed the following error
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
kindly tell me what I have to set more.
@Pranaya Probably this error occurs when PHPMailer is unable to contact the SMTP server you have specified in the Host property.
Greetings Sir,
How would i add Logo in the mail.Please do tell about this, i would like to add Logo in my mail thats in image file format.
@Robin You can insert the <img> tags into the
$bodyContent
variable. For reference follow this guide – http://www.codexworld.com/send-beautiful-html-email-using-php/great tutorial thanks codexworld
can you please tell me
how to send images or attachment with this script from localhost.
how to use those attachment lines in this.
@Shubham To send the email with images and attachments follow this guide – http://www.codexworld.com/send-html-email-multiple-attachments-php/
Thank you so much!!! You saved my day… and thanks for sharing..
can i send mail from my mail list(importing mail addresses from txt or csv or excel file)?
Yes, you can send multiple emails from your mailing list.
thanx
Great tutorial.. I have searched for more than 100s of tutorials for this mailing from localhost purpose but none of them worked for me.. This one is great… Worked for me… Thanks CodexWorld.
It’s cool.
Thanks for sharing!
sir it’s showing Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
now what should i do ?
@Neha Probably this error occurs when PHPMailer is unable to contact the SMTP server you have specified in the Host property.
nice tutorial its works
works perfectly ! gracias
Nice tutorial it works perfectly
Wow…. Really very awesome….
Thanks a lot for sharing such a nice tricks..
Thanks. Very neat and good tutorial.
how to make recipient address dynamic
i want email to be sent when the button is clicked and to the email address written in the text area
@Vipul Create a HTML form with an input field and submit the user provided email to a PHP file (
sendEmail.php
).In this file (
sendEmail.php
), you can get the email from the input field. Now place it in the recipient address.Very good Tutorial, thanks for helping
hi! i do all steps but it does’nt work. and this is the error :
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
@Fatima Probably this error occurs when PHPMailer is unable to contact the SMTP server you have specified in the Host property.