In what ways can using a pre-built mailer class like PHPMailer simplify the process of sending emails and ensure proper handling of special characters?

Using a pre-built mailer class like PHPMailer simplifies the process of sending emails by providing a set of functions and methods specifically designed for handling email sending tasks. PHPMailer also ensures proper handling of special characters by encoding them properly, which helps prevent issues like garbled text or broken formatting in the email content.

<?php
require 'vendor/autoload.php';

// Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();

// Set up the email parameters
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

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

// Set the email subject and body
$mail->Subject = 'Subject of the email';
$mail->Body = 'This is the body of the email with special characters like ü, é, and ñ.';

// Send the email
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>