How can the use of a Mailer class, such as phpMailer, improve the process of sending emails in PHP?
Using a Mailer class like phpMailer can improve the process of sending emails in PHP by providing a more robust and reliable way to send emails. It offers features such as SMTP authentication, HTML email support, attachments, and more, making it easier to customize and send emails efficiently.
<?php
require 'vendor/autoload.php'; // Include phpMailer library
// Create a new PHPMailer instance
$mail = new PHPMailer\PHPMailer\PHPMailer();
// Set up the SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
// Set the email content
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using phpMailer';
// Send the email
if($mail->send()){
echo 'Email sent successfully';
} else {
echo 'Email could not be sent';
}
?>