How can PHP Mailer classes improve the process of sending emails with attachments?
Sending emails with attachments in PHP can be complex and error-prone. Using PHP Mailer classes simplifies this process by providing a convenient and reliable way to send emails with attachments. These classes handle the encoding and formatting of attachments, making it easier for developers to include files in their email messages.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set up the email parameters
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Email with Attachment';
$mail->Body = 'This is a test email with attachment.';
// Add an attachment
$mail->addAttachment('/path/to/file.pdf', 'document.pdf');
// Send the email
if($mail->send()) {
echo 'Email sent successfully';
} else {
echo 'Error sending email: ' . $mail->ErrorInfo;
}