Easy Tutorial
❮ Git Graphical 2014 Need To Learn ❯

PHP Sending Emails with phpmailer

Category Programming Techniques

phpMailer is a very powerful PHP email sending class that can set the sending email address, reply address, email subject, HTML web page, upload attachments, and is very easy to use.

Features of phpMailer:

Installation or download methods for phpmailer:

  1. Download from GitHub: https://github.com/PHPMailer/PHPMailer/

  2. Install using composer:

    composer require phpmailer/phpmailer
    

Before sending, you need to have your own email server. For testing, it is actually most convenient to use a free email account you have applied for, without the need to set up your own server. You may need to configure the SMTP service of the mailbox, and most public mailboxes (163, qq, etc.) are closed by default for security reasons.

NetEase mailbox configuration is shown in the following figure:

QQ mailbox related configuration is shown in the following figure:

Email POP3 Server (Port 995) SMTP Server (Port 465 or 587)
qq.com pop.qq.com smtp.qq.com

Of course, other mailboxes besides NetEase and QQ are also possible. Here is an example of PHP code:

Example

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require './src/Exception.php';
require './src/PHPMailer.php';
require './src/SMTP.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
    // Server configuration
    $mail->CharSet = "UTF-8"; // Set email encoding
    $mail->SMTPDebug = 0; // Debug mode output
    $mail->isSMTP(); // Use SMTP
    $mail->Host = 'smtp.163.com'; // SMTP server
    $mail->SMTPAuth = true; // Allow SMTP authentication
    $mail->Username = 'Email username'; // SMTP username, i.e., the username of the mailbox
    $mail->Password = 'Password or authorization code'; // SMTP password, some mailboxes use an authorization code (e.g., 163 mailbox)
    $mail->SMTPSecure = 'ssl'; // Allow TLS or SSL protocol
    $mail->Port = 465; // Server port 25 or 465, depending on the support of the mailbox server

    $mail->setFrom('[email protected]', 'Mailer'); // Sender
    $mail->addAddress('[email protected]', 'Joe'); // Recipient
    // $mail->addAddress('[email protected]'); // Multiple recipients can be added
    $mail->addReplyTo('[email protected]', 'info'); // Reply to which mailbox when replying, it is recommended to be consistent with the sender
    // $mail->addCC('[email protected]'); // CC
    // $mail->addBCC('[email protected]'); // BCC

    // Send attachments
    // $mail->addAttachment('../xy.zip'); // Add attachment
    // $mail->addAttachment('../thumb-1.jpg', 'new.jpg'); // Send attachment and rename

    // Content
    $mail->isHTML(true); // Whether to send in the format of an HTML document, the client can directly display the corresponding HTML content after sending
    $mail->Subject = 'This is the email subject' . time();
    $mail->Body = '<h1>This is the email content</h1>' . date('Y-m-d H:i:s');
    $mail->AltBody = 'If the email client does not support HTML, this content is displayed';

    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Email sending failed: ', $mail->ErrorInfo;
}

The interface after sending an email with attachments is as follows:

>

Article source: https://www.liminghulian.com/article/98

**Click to share notes

Cancel

-

-

-

English:

❮ Git Graphical 2014 Need To Learn ❯