Email Tutorials - Herong's Tutorial Examples - v1.04, by Herong Yang
PHP 5.6 and PHPMailer 5.2
This section provides a tutorial example on how to install PHPMailer 5.2 in CentOS 8 for PHP 5.6 to send out emails to remote STMPS server.
If you are still using PHP 5.6 engine for some reason, you can use PHPMailer 5.2 to send out emails. Here is what I did on my CentOS 8 computer.
1. Check PHP version:
herong$ php -version PHP 5.6.40 (cli) (built: May 12 2020 15:27:05) Copyright (c) 1997-2016 The PHP Group
2. Install Remi's RPM repository, where PHPMailer packages are hosted.
herong$ export url=http://rpms.remirepo.net/enterprise/8/remi/x86_64 herong$ wget $url/remi-release-8.3-1.el8.remi.noarch.rpm herong$ sudo rpm -Uvh remi-release-8.3-1.el8.remi.noarch.rpm
3. Install php-PHPMailer from Remi's RPM repository.
herong$ sudo dnf --enablerepo=remi install php-PHPMailer ... Installed: php-PHPMailer-5.2.28-6.el8.remi.noarch php-common-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 php-intl-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64 php-mbstring-7.2.24-1.module_el8.2.0+313+b04d0a66.x86_64
4. Find the path name where php-PHPMailer is installed:
herong$ sudo find / -name PHPMailer /usr/share/php/PHPMailer herong$ sudo ls -l /usr/share/php/PHPMailer -rw-r--r-- 1 2464 Mar 19 2020 class.phpmaileroauthgoogle.php -rw-r--r-- 1 7216 Mar 19 2020 class.phpmaileroauth.php -rw-r--r-- 1 148027 Mar 26 17:43 class.phpmailer.php -rw-r--r-- 1 11006 Mar 19 2020 class.pop3.php -rw-r--r-- 1 43495 Mar 19 2020 class.smtp.php -rw-r--r-- 1 1216 Mar 26 17:43 PHPMailerAutoload.php
5. Run the following PHP script to test. It works!
<?php
# PHPMailer-PHP-5.php
#- Copyright (c) 2019-2023 HerongYang.com. All Rights Reserved.
$path = "/usr/share/php/PHPMailer";
require "$path/class.phpmailer.php";
$mail = new PHPMailer(true);
$mail->CharSet = "utf-8"; # must be the first
$mail->SMTPDebug = 4;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'TLS';
$mail->Host = "smtp.example.com";
$mail->Port = 587;
$mail->Username = 'herong@herongyang.com';
$mail->Password = 'TopScret';
try {
$mail->setFrom('herong@herongyang.com', 'Herong Yang');
$mail->addAddress('joe@herongyang.com', 'Joe Doe');
$mail->Subject = 'PHPMailer 5.2 and PHP 5.6 Test';
$mail->Body = 'There is a test message from PHPMailer.';
$mail->AddCC('amy@herongyang.com', '');
$mail->AddAttachment('/home/herong/tmp/junk.pdf');
$mail->send();
} catch (Exception $e) {
echo $e->errorMessage();
} catch (\Exception $e) {
echo $e->getMessage();
}
?>
You should avoid using "rpm" command to install PHPMailer package files directly. It is not smart enough to manage dependencies automatically. See the example below:
herong$ export url=http://rpms.remirepo.net/enterprise/8/remi/x86_64 herong$ wget $url/php-PHPMailer-5.2.28-6.el8.remi.noarch.rpm heron$ sudo rpm -Uvh php-PHPMailer-5.2.28-6.el8.remi.noarch.rpm error: Failed dependencies: php-date is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-filter is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-hash is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-intl is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-mbstring is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-openssl is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-pcre is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch php-spl is needed by php-PHPMailer-5.2.28-6.el8.remi.noarch
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