What are some recommended PHP libraries or classes for sending formatted emails?

When sending formatted emails in PHP, it is recommended to use libraries or classes that handle the formatting and sending of emails efficiently. Some popular choices include PHPMailer, Swift Mailer, and Zend Mail. These libraries provide easy-to-use methods for creating HTML emails, adding attachments, and sending emails securely.

// Example using PHPMailer library to send a formatted email

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

require 'vendor/autoload.php'; // Path to PHPMailer autoload file

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your@example.com';
    $mail->Password = 'yourpassword';
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->setFrom('from@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');

    $mail->isHTML(true);
    $mail->Subject = 'Subject of the email';
    $mail->Body = '<h1>Hello, this is a formatted email!</h1>';

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