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
- How can the logic for checking and processing email addresses in the code be improved to handle multiple occurrences of email addresses in the daemonliste.txt file?
- How can regex be used effectively in PHP to extract specific data from a string?
- In what scenarios would using a Template Class for parsing HTML files be beneficial for PHP development projects?