Email Tutorials - Herong's Tutorial Examples - v1.04, by Herong Yang
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 (c) 2019-2023 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 debugging 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
Postfix - Mail Transport Agent (MTA)
SSL/TLS Secure Connections with Postfix Server
Dovecot - IMAP and POP3 Server
SSL/TLS Secure Connections with Dovecot Server
Email Client Tools - Mail User Agents (MUA)
Mozilla Thunderbird - Mail User Agents (MUA)
►PHPMailer - PHP Package for Sending Emails
Install PHPMailer on CentOS Systems
►Send Local Emails with PHPMailer
Send Remote Emails with PHPMailer
Use SMTPS Protocol with PHPMailer
Install PHPMailer from Source Code
Send Email Attachments with PHPMailer
Send Email in HTML with PHPMailer