How do you send an HTML formatted email with an embedded image or attachment using PHPMailer?
Short answer
PHPMailer lets you set the isHTML method to true to send richly formatted HTML content instead of plain text, and it provides an addAttachment method for including file attachments, along with support for embedding images directly within the HTML body using a content identifier…
Advertisement
AModel answer
PHPMailer lets you set the isHTML method to true to send richly formatted HTML content instead of plain text, and it provides an addAttachment method for including file attachments, along with support for embedding images directly within the HTML body using a content identifier reference, giving you full control over creating professional looking, richly formatted transactional emails.
$mail->isHTML(true);
$mail->Body = '<h1>Welcome!</h1><p>Thanks for joining us.</p>';
$mail->addAttachment('/path/to/invoice.pdf');Real-world example
An e commerce platform sends order confirmation emails as nicely formatted HTML with the company logo embedded directly in the email and the customer's invoice attached as a PDF file, providing a professional experience compared to a plain text email.
Likely follow-ups
- How do you embed an image directly within the email body rather than as an attachment?
- What happens if a recipient's email client does not support HTML formatting?
Related topics