Send Local Emails with PHPMailer

This section provides a tutorial example on how to send local emails with PHPMailer using mail() function and 'sendmail' program.

PHPMailer supports 3 options on how emails are delivered to the recipients:

Here is a PHP script to test the first 2 options.

<?php
#  PHPMailer-Send-Email-Locally.php
#- Copyright 2019 (c) HerongYang.com. All Rights Reserved.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

$srcDir = "/home/herong/local/php/vendor/phpmailer/phpmailer/src/";
require "$srcDir/PHPMailer.php";
require "$srcDir/SMTP.php";
require "$srcDir/Exception.php";

$mail = new PHPMailer(true);
$mail->SMTPDebug = 4; # highest debugginng level

if ($argv[1]=="mail") {
  $mail->isMail();
} else if ($argv[1]=="sendmail") {
  $mail->isSendmail();
  $mail->$sendmail = "/usr/sbin/sendmail";
} else {
  # default option
}

try {
   $mail->setFrom('joe@herongyang.com', 'Joe Doe');
   $mail->addAddress('herong@herongyang.com', 'Herong Yang');
   $mail->Subject = 'PHPMailer Test - Local Delivery';
   $mail->Body = 'There is a test message from PHPMailer.';
   $mail->send();
} catch (Exception $e) {
   # PHPMailer exceptions
   echo $e->errorMessage();
} catch (\Exception $e) {
   # PHP exceptions
   echo $e->getMessage();
}
?>

Run the above PHP script with different options:

herong$ php PHPMailer-Send-Email-Locally.php mail
...	Sending with mail()
...	Sendmail path: /usr/sbin/sendmail -t -i
...	Envelope sender: joe@herongyang.com
...	To: Herong Yang <herong@herongyang.com>
...	Subject: PHPMailer Test - Local Delivery
...	Headers: Date: Sun, 13 Jun 2021
      From: Joe Doe <joe@herongyang.com>
      Message-ID: <...@mail.herongyang.com>
      X-Mailer: PHPMailer 6.4.1 (https://github.com/PHPMailer/PHPMailer)
      MIME-Version: 1.0
      Content-Type: text/plain; charset=iso-8859-1
...	Additional params: -fjoe@herongyang.com
...	Result: true

herong$ php PHPMailer-Send-Email-Locally.php sendmail
...	Sending with sendmail
...	Sendmail path: /usr/sbin/sendmail
...	Sendmail command: /usr/sbin/sendmail -oi -fjoe@herongyang.com -t
...	Envelope sender: joe@herongyang.com
...	Headers: Date: Sun, 13 Jun 2021
      To: Herong Yang <herong@herongyang.com>
      From: Joe Doe <joe@herongyang.com>
      Subject: PHPMailer Test - Local Delivery
      Message-ID: <...@mail.herongyang.com>
      X-Mailer: PHPMailer 6.4.1 (https://github.com/PHPMailer/PHPMailer)
      MIME-Version: 1.0
      Content-Type: text/plain; charset=iso-8859-1
...	Result: true

herong$ php PHPMailer-Send-Email-Locally.php default
...	Sending with mail()
...	Sendmail path: /usr/sbin/sendmail -t -i
...	Envelope sender: joe@herongyang.com
...	To: Herong Yang <herong@herongyang.com>
...	Subject: PHPMailer Test - Local Delivery
...	Headers: Date: Sun, 13 Jun 2021
      From: Joe Doe <joe@herongyang.com>
      Message-ID: <...@mail.herongyang.com>
      X-Mailer: PHPMailer 6.4.1 (https://github.com/PHPMailer/PHPMailer)
      MIME-Version: 1.0
      Content-Type: text/plain; charset=iso-8859-1
...	Additional params: -fjoe@herongyang.com
...	Result: true

Output confirms that both isMail() and isSendmail() options are working correctly.

Note that PHPMailer only accepts fully qualified email addresses like joe@herongyang.com. It will reject short addresses like "joe" or "joe@localhost".

Table of Contents

 About This Book

 Introduction and Installation of PHP

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

Configuring and Sending Out Emails

 Using Local Windows System as a Mail Server

 Sending Out Emails from PHP Scripts

 Sending Out Emails on Linux Systems

 Install PHPMailer on CentOS Systems

Send Local Emails with PHPMailer

 Send Remote Emails with PHPMailer

 Use SMTPS Protocol with PHPMailer

 PHP 5.6 and PHPMailer 5.2

 Install PHPMailer from Source Code

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB