How to Send Email from Localhost in PHP

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.

Send Email from Localhost with PHP

The following code snippet will send HTML email from localhost using PHPMailer.

  • Include the PHPMailer library and create an instance of this class.
  • Set SMTP credentials (host, username, password, and port).
  • Specify sender name and email ($mail->setFrom).
  • Set recipient email address ($mail->addAddress).
  • Set email subject ($mail->Subject).
  • Set the body content of the email ($mail->Body).
  • Use the 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.

  • Set file path to addAttachment() method.
// 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

50 Comments

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

Leave a reply

keyboard_double_arrow_up