PHP Logophpmailer/phpmailer

PHPMailer is a popular open-source PHP class that provides a full-featured email creation and transfer library. It allows PHP developers to send emails from their applications much more robustly and with more features than the built-in `mail()` function. The native `mail()` function has several limitations, such as poor error handling, difficulty with HTML emails, attachments, and direct SMTP server communication. PHPMailer overcomes these challenges by offering a comprehensive solution for sending emails.

Key features of PHPMailer include:
* SMTP Support: Direct support for sending emails via an SMTP server, including authentication, which is crucial for reliability and avoiding spam filters.
* HTML Emails: Easy creation and sending of rich HTML emails, along with an automatic plain-text alternative for email clients that don't support HTML.
* Attachments: Simple methods for adding file attachments to emails.
* Embedded Images: Ability to embed images directly within HTML email bodies (CID embedding).
* Secure SMTP: Support for SSL/TLS encryption for secure communication with SMTP servers.
* Character Sets: Robust support for various character sets, allowing for internationalized emails.
* Error Handling: Detailed error reporting and debugging capabilities, making it easier to diagnose issues.
* Multiple Recipients: Support for To, CC, and BCC recipients.

Installation is typically done via Composer, the PHP dependency manager, by running `composer require phpmailer/phpmailer`. Once installed, you can include the autoloader and start using the `PHPMailer` class to configure your email settings and send messages.

Example Code

<?php
// Autoload files using Composer (if installed via Composer)
require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP; // Optional, but good practice for clarity

$mail = new PHPMailer(true); // Passing true enables exceptions

try {
    // Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output (for testing, turn off in production)
    $mail->isSMTP(); // Send using SMTP
    $mail->Host       = 'smtp.example.com'; // Set the SMTP server to send through
    $mail->SMTPAuth   = true; // Enable SMTP authentication
    $mail->Username   = 'your_email@example.com'; // SMTP username
    $mail->Password   = 'your_smtp_password'; // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable implicit TLS encryption (PHPMailer::ENCRYPTION_STARTTLS for explicit)
    $mail->Port       = 465; // TCP port to connect to; use 587 if you set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    // Recipients
    $mail->setFrom('from@example.com', 'Sender Name'); // Sender's email and name
    $mail->addAddress('recipient@example.com', 'Recipient Name'); // Add a recipient
    // $mail->addAddress('another@example.com'); // Name is optional
    // $mail->addReplyTo('info@example.com', 'Information');
    // $mail->addCC('cc@example.com');
    // $mail->addBCC('bcc@example.com');

    // Attachments (Optional)
    // Uncomment the lines below to add attachments
    // $mail->addAttachment('/path/to/your/file.tar.gz'); // Add attachments (absolute path)
    // $mail->addAttachment('/path/to/your/image.jpg', 'new.jpg'); // Optional name for the attachment

    // 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 plain text body 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}";
}
?>