Any backend PHP programmer is faced with the task of automating mailing on websites, and many who encountered this task for the first time were faced with the key question: “Should I write or not write an smtp client from scratch?” If you decide to independently implement the smtp client functionality in PHP, or any other similar language, then you will be faced with very routine and boring work, because The main mechanism for working with smtp servers is to exchange text messages and nothing more. But, fortunately, this is not required! Everything you need is already written in the hero of today’s article - the PHPMailer class.
The PHPMailer class gives us the ability to easily and quickly write scripts for sending email notifications with a flexible system for debugging this script. The script is written in the PHP programming language and supports cryptographic encryption standards when exchanging messages with the TLS\SSL server.
To use PHPMailer on your server, you need the ability to connect to external SMTP servers using PHP.
Features of SMTP client PHPMailer.
- Sending E-mail with multiple recipients;
- Supports 8 bit, base64, binary mode, and printable format;
- SMTP authorization on the server side;
- Word wrap;
- Send messages as HTML;
- The library has been tested on many SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, Mercury, Courier;
- Works on any win32 and *nix platform;
- Flexibility of debugging work - a system has been implemented to display a detailed log of communication with the SMTP server;
- Manually defined email headers;
- Combining multiple messages and attachments;
- Built-in image support.
- Sending letters in various ways: via smtp server, sendmail and Qmail utility, mail() function
Installation and initial preparation of the PHPMailer SMTP client for operation
First of all, you need to download the latest version of the SMTP library: https://github.com/PHPMailer/PHPMailer
Then unpack the downloaded archive and study the contents of the PHPMailer SMTP library:
/docs: // Directory with documentation for the PHPMailer library
/examples: // Directory with examples of how to use the PHPMailer library
/extras: // Additional utilities for working with mail servers
/language: // Directory with translations for Debug
/test: // Scripts for testing the PHPMailer library
class.phpmailer.php // Class for creating and sending emails using different methods
class.pop3.php // Class for working with POP3
class.smtp.php // Class for working with SMTP
PHPMailerAutoload.php // File for automatically loading PHPMailer classes
Everything else has no functional load and is provided as an informational addition to the PHPMailer library.
Hooray! Now we know what we have when unpacking the library. The only thing left to do is figure out how to work with this matter!
Many people like to create an additional configuration file for the SMTP client of PHPMailer and use it in the future. I am also a follower of this tradition and offer an example of such a file for consideration.
config.php:
$__smtp = array(
"host" => 'smtp.host.com', // SMTP server
"debug" => 2, // Logging level
"auth" => true, // SMTP server authentication. If none, set to false
"port" => '25', // SMTP server port
"username" => 'mail@host.com', // Username required for SMTP authentication
"password" => '123', // Password
"addreply" => 'reply@host.com', // Reply-to email address
"secure" => 'ssl', // Encryption type. For example, ssl or tls
"mail_title" => 'Your email subject!', // Email subject
"mail_name" => 'Sender name' // Sender's name
);
We upload all this wealth to our hosting and prepare for the first initialization and test sending of the first letter using the SMTP client PHPMailer.
Connect and initialize the PHPMailer class
To connect the PHPMailer class, you need to include the PHPMailerAutoload.php and config.php files:
require_once('/path/to/file/config.php'); // Configuration file for your SMTP server
require_once('/path/to/file/PHPMailerAutoload.php'); // File for automatic loading of PHPMailer classes
Next comes the process of configuring the PHPMailer SMTP client and sending the letter. You can look at the example configuration in the /examples directory or use our example of configuration and sending a letter using the PHPMailer class.
PHPMailer SMTP client configuration example:
This example demonstrates the operation of the PHPMailer library as an SMTP client.
require_once('/path/to/file/config.php'); // Configuration file for your SMTP server
require_once('/path/to/file/PHPMailerAutoload.php'); // File for automatic loading of PHPMailer classes
try{
$mail = new PHPMailer(true); // Create an instance of the PHPMailer class
$mail->IsSMTP(); // Set the mode of operation with the SMTP server
$mail->Host = $__smtp['host']; // SMTP server host: IP or domain name
$mail->SMTPDebug = $__smtp['debug']; // SMTP client PHPMailer logging level
$mail->SMTPAuth = $__smtp['auth']; // Whether SMTP authentication is required
$mail->Port = $__smtp['port']; // SMTP server port
$mail->SMTPSecure = $__smtp['secure']; // Encryption type, e.g., ssl or tls
$mail->CharSet="UTF-8"; // Charset for communication with the SMTP server
$mail->Username = $__smtp['username']; // SMTP server username
$mail->Password = $__smtp['password']; // SMTP server account password
$mail->AddAddress('whoto@example.com', 'John Doe'); // Recipient of the email
$mail->AddReplyTo($__smtp['addreply'], 'First Last'); // Alternative reply-to address
$mail->SetFrom($__smtp['username'], $__smtp['mail_title']); // Sender of the email
$mail->Subject = htmlspecialchars($__smtp['mail_title']); // Email subject
$mail->MsgHTML('Message text!'); // Message body
$mail->Send(); return 1;
} catch (phpmailerException $e) {
return $e->errorMessage();
}
Congratulations! Now you have a clear understanding of the capabilities of the SMTP library PHPMailer and are ready to implement any mailing campaigns using the PHP programming language!
Conclusions
We recommend the PHPMailer library as a simple and reliable class for working with SMTP servers. This library will allow you to quickly organize mailing of almost any complexity and carry out flexible debugging of scripts.
We have repeatedly used and continue to use this library in our projects!
Thank you all for your attention!