📜  PHPMailer - PHP (1)

📅  最后修改于: 2023-12-03 15:18:32.898000             🧑  作者: Mango

PHPMailer - PHP

PHPMailer is a popular open-source PHP library used to send email messages directly from PHP scripts. It is a mature and feature-rich library that offers many functions to create and send emails with ease.

Features

PHPMailer provides several key features:

  • Support for sending HTML and plain-text emails
  • Multiple recipients and CC/BCC support
  • File attachments
  • SMTP authentication and encryption options
  • Easy to integrate with PHP scripts
  • Support for sending emails via PHP’s mail() function, SMTP, or sendmail
Installation

PHPMailer can be installed using Composer or by downloading the library directly from GitHub.

Composer

You can install PHPMailer using Composer by running the following command:

composer require phpmailer/phpmailer
GitHub

Alternatively, you can download PHPMailer directly from the GitHub repository:

git clone https://github.com/PHPMailer/PHPMailer.git
Usage

After installation, you can easily use PHPMailer to send emails in your PHP scripts.

Basic Example

Here's a basic example of how to send an email using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);                               // Create an instance of PHPMailer
try {
    //Server settings
    $mail->SMTPDebug = 0;                                  // Enable verbose debug output
    $mail->isSMTP();                                       // Set mailer to use SMTP
    $mail->Host = 'smtp.example.com';                      // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                                // Enable SMTP authentication
    $mail->Username = 'yourname@example.com';              // SMTP username
    $mail->Password = 'yourpassword';                      // SMTP password
    $mail->SMTPSecure = 'tls';                             // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                     // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('someone@example.com', 'Joe User');   // Add a recipient
    $mail->addReplyTo('info@example.com', 'Information');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Advanced Example

PHPMailer allows you to easily customize and configure your email settings. Here's an advanced example of sending an email using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Create an instance of PHPMailer
try {
    //Server settings
    $mail->SMTPDebug = 2;                                  // Enable verbose debug output
    $mail->isSMTP();                                       // Send using SMTP
    $mail->Host = 'smtp.gmail.com';                        // Set the SMTP server to send through
    $mail->SMTPAuth = true;                                // Enable SMTP authentication
    $mail->Username = 'youremail@gmail.com';               // SMTP username
    $mail->Password = 'yourpassword';                      // SMTP password
    $mail->SMTPSecure = 'tls';                             // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port = 587;                                     // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('youremail@gmail.com', 'Mailer');
    $mail->addAddress('recipient@gmail.com', 'Joe User');   // Add a recipient
    $mail->addAddress('ellen@example.com');                 // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
Conclusion

In summary, PHPMailer is a powerful PHP library that makes it easy to send email messages directly from PHP scripts. It offers many useful features and can be easily integrated into any PHP project.