How can PHP be used to send multipart emails with both HTML and plain text content?
To send multipart emails with both HTML and plain text content using PHP, you can use the PHPMailer library. This library allows you to create multipart emails easily by including both HTML and plain text versions of the email content. By setting the content type to multipart/alternative, the email client can choose which version to display based on the recipient's preferences.
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
// Create a new PHPMailer instance
$mail = new PHPMailer();
// Set the email content type to multipart/alternative
$mail->isHTML(true);
$mail->AltBody = 'This is the plain text version of the email';
// Set the HTML content of the email
$mail->Subject = 'Subject of the email';
$mail->Body = '<p>This is the HTML version of the email</p>';
// Add recipients, set sender, and send the email
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->setFrom('sender@example.com', 'Sender Name');
$mail->send();
Related Questions
- Are there best practices for configuring SMTP settings in PHP mail functions?
- How can the use of incorrect comparison operators in loops lead to unexpected behavior in PHP?
- When benchmarking different methods for splitting a string in PHP, what factors should be considered to ensure accurate results?