How can PHP developers efficiently combine HTML content and attachments when sending emails using PHP mail functions?
When sending emails using PHP mail functions, PHP developers can efficiently combine HTML content and attachments by using the PHPMailer library. This library allows for easy attachment of files to emails, as well as the ability to include HTML content in the email body.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Include the PHPMailer Autoload file
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set the email parameters
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject of the email';
$mail->isHTML(true);
$mail->Body = '<p>This is the HTML content of the email</p>';
// Attach a file to the email
$file_path = 'path/to/attachment.pdf';
$mail->addAttachment($file_path);
// Send the email
if($mail->send()) {
echo 'Email sent successfully';
} else {
echo 'Email could not be sent.';
}