How to Send Email from Localhost in PHP


Most of the web projects have the email sending functionality. Before uploading to the live server email functionality need to be checked on the local server. But PHP mail() function will not work at the localhost. In this article, we’ll show how you can send email from localhost in PHP. Using this simple PHP script you can send email from any localhost server, like XAMPP, WAMP, or any others.

We’ll use PHPMailer for helping to send email from localhost. Not only the text email, you can send HTML email from localhost in PHP using PHPMailer. Download the PHPMailer from GitHub, also you can get it from our downloadable package.

To using Gmail SMTP server, you should need to change account access for less secure apps. Don’t worry! Just follow the below steps.

That enough!

At first, include PHPMailerAutoload.php file in your script and create an instance of PHPMailer class.
Set your Google email address as SMTP username and password as SMTP password.
Insert the HTML email body content into the $bodyContent variable.

<?php
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                            // Set mailer to use SMTP
$mail->Host 'smtp.gmail.com';             // Specify main and backup SMTP servers
$mail->SMTPAuth true;                     // Enable SMTP authentication
$mail->Username 'Email Address' // SMTP username
$mail->Password 'Email Account Password'// SMTP password
$mail->SMTPSecure 'tls';                  // Enable TLS encryption, `ssl` also accepted
$mail->Port 587;                          // TCP port to connect to

$mail->setFrom('info@example.com''CodexWorld');
$mail->addReplyTo('info@example.com''CodexWorld');
$mail->addAddress('john@gmail.com');   // Add a recipient
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->isHTML(true);  // Set email format to HTML

$bodyContent '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';

$mail->Subject 'Email from Localhost by CodexWorld';
$mail->Body    $bodyContent;

if(!
$mail->send()) {
    echo 
'Message could not be sent.';
    echo 
'Mailer Error: ' $mail->ErrorInfo;
} else {
    echo 
'Message has been sent';
}
?>

You can send multiple attachments with email by using the following code.

// Add attachments
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg''new.jpg'); // Optional name

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You

46 Comments

  1. Santosh Said...
  2. Michael Said...
    • CodexWorld Said...
  3. Tony Josh Said...
  4. Fawad Khaliq Said...
  5. Mohd Zaidi Said...
  6. Anand Singh Said...
    • CodexWorld Said...
  7. Pher Said...
  8. Deddy Andy Said...
    • CodexWorld Said...
  9. Deddy Andy Said...
    • CodexWorld Said...
  10. Ponrajkumar J Said...
    • CodexWorld Said...
  11. Waleed Said...
  12. Kogilavany Said...
    • CodexWorld Said...
  13. Robin Tyagi Said...
  14. Kirill Said...
  15. Pranaya Padhy Said...
    • CodexWorld Said...
  16. Robin Tyagi Said...
  17. Masroor Said...
  18. Shubham Agrawal Said...
  19. Kat Said...
  20. Bibek Said...
    • CodexWorld Said...
  21. Shikha Said...
  22. Mohit Joshi Said...
  23. Sadiki Said...
  24. Neha Singh Said...
    • CodexWorld Said...
  25. Sudi Omary Said...
  26. Samuel Said...
  27. Hans Said...
  28. Sankhnad Mishra Said...
  29. Shivateja Said...
  30. Vipul Said...
    • CodexWorld Said...
  31. Amir Said...
  32. Fatima Said...
    • CodexWorld Said...

Leave a reply