Sending Out Emails on Linux Systems

This section provides a tutorial example on how to use the mail() function to send an email to the local SMTP mail server.

If you are using a Linux system, you can also send out emails from PHP scripts as shown in this tutorial.

1. Make sure that the "sendmail" command is working:

herong$ which sendmail
/usr/sbin/sendmail

herong$ sendmail -r "herong@localhost" joe@localhost
Subject: Last Test

Hi Joe, This is the last test. Bye!
.

jeo$ mailx ~/Maildir
& $
Message 334:
From herong@localhost Sat Jun 11 14:13:01 2021
Return-Path: <herong@localhost>
X-Original-To: joe@localhost
Delivered-To: jeo@localhost
Subject: Last Test
Date: Sat, 12 Jun 2021 14:13:00 +0800 (CST)
From: Herong Yang <herong@localhost>
Status: RO

Hi Joe, This is the last test. Bye!

2. Check the "sendmail" log file:

herong$ sudo grep joe /var/log/maillog
Jun 12 14:04:43 mail postfix/local[697852]: ...: to=<joe@localhost>,
relay=local, delay=23, delays=23/0.01/0/0.07, dsn=2.0.0,
status=sent (delivered to maildir)

3. Write PHP script to use the built-in mail() function.

<?php
#  Mail-Test-2.php
#- Copyright 2009 (c) HerongYang.com. All Rights Reserved.

echo 'SMTP = ' . ini_get('SMTP') . "\n";
echo 'smtp_port = ' . ini_get('smtp_port') . "\n";
echo 'sendmail_from = ' . ini_get('sendmail_from') . "\n";
echo 'post_max_size = ' . ini_get('post_max_size') . "\n";
echo 'sendmail_path = ' . ini_get('sendmail_path') . "\n";

$to = "doe@localhost";
$subject = "Testing";
$message = "This is a test.";
$headers = "From: herong@localhost";
mail($to, $subject, $message, $headers);
?>

4. Run the above PHP script, you should see the following output:

herong$ php Mail-Test-2.php

SMTP = localhost
smtp_port = 25
sendmail_from =
post_max_size = 10M
sendmail_path = /usr/sbin/sendmail -t -i

5. Check the "sendmail" log file:

herong$ sudo grep joe /var/log/maillog

Jun 11 17:26:16 mail postfix/local[703828]: ***: to=<doe@localhost>,
relay=local, delay=0.12, delays=0.08/0/0/0.04, dsn=2.0.0,
status=sent (delivered to maildir)

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 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