How to Use PHP Code to Send Email From Localhost

Send using the PHP mail() function

You can use the built-in mail() function in PHP to send emails from a localhost. Here’s an example of how to use it:

<?php
$to = "recipient@example.com";
$subject = "Test email from localhost";
$message = "This is a test email sent from a localhost using PHP.";
$headers = "From: sender@example.com" . "\r\n" .
    "CC: another@example.com";

mail($to, $subject, $message, $headers);
?>

Keep in mind that in order to send email from localhost, you will need to have a mail server installed and configured on your local machine. If you are using Windows, you can use software like XAMPP or WAMP which come bundled with a mail server, but on Linux or MacOS, you need to install and configure a mail server like postfix or exim.

Also, note that some hosting providers block outbound traffic on port 25, so you may need to use a different port or configure your mail server to use a different SMTP server to send email.

Send using PHPMailer library

Additionally, you can also use a library such as PHPMailer to send email from a localhost. It provides more advanced features such as support for sending emails via SMTP and attaching files. Here’s an example of how to use it to send an email:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 0;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'username';                     // SMTP username
    $mail->Password   = 'password';                               // SMTP password
    $mail->SMTPSecure = 'ssl';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->Port       = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('sender@example.com', 'Sender Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Test email from localhost';
    $mail->Body    = 'This is a test email sent from a localhost using PHPMailer.';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

You will need to set the host, username and password of your mail server. Also, you need to adjust the security settings to match with the mail server.

It is important to note that the mail() function and the PHPMailer library both have their own advantages and disadvantages. The mail() function is a built-in PHP function that is simple to use but has limited functionality, while PHPMailer is a third-party library that offers more advanced features but requires a bit more setup and configuration.

Prerequisites to send email from localhost using PHP

In order to send emails from localhost using PHP, you will need to have the following prerequisites:

  1. A web server: You will need to have a web server such as Apache or Nginx installed and running on your local machine.
  2. PHP: You will need to have PHP installed and configured to work with your web server.
  3. A mail server: You will need to have a mail server installed and configured on your local machine. Some commonly used mail servers for sending email from localhost are Postfix, Exim, and Sendmail.
  4. SMTP Configuration: You will need to have the correct SMTP configuration settings for your mail server. This includes the host, port, username, and password for the SMTP server.
  5. PHP extension for sending mail: If you want to use the built-in mail() function in PHP, you need to ensure that the PHP mail extension is installed and configured properly.
  6. Firewall settings : If you are using a firewall, you may need to configure it to allow outbound traffic on port 25, which is the standard port for sending email.
  7. If you’re using a library like PHPMailer, you will need to include the library files in your project and configure it correctly.

It’s also important to note that some hosting providers block outbound traffic on port 25, so you may need to use a different port or configure your mail server to use a different SMTP server to send email.

Leave a Comment

Related Posts