phpmailerHow do I use a HTML file in the body of a PHPMailer message?
To use a HTML file in the body of a PHPMailer message, you can use the msgHTML()
method. This method takes an HTML string, an external HTML file, or an array of attachments.
The following example code shows how to use the msgHTML()
method to send an HTML file as the message body:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
// Set up SMTP
$mail->IsSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';
// Set recipients
$mail->addAddress('[email protected]');
// Set the subject
$mail->Subject = 'Message Subject';
// Set the body
$mail->msgHTML(file_get_contents('message.html'));
// Send the email
$mail->send();
This code will send an email with the contents of the message.html
file as the body of the message.
Code explanation
require 'PHPMailerAutoload.php';
: This line includes the PHPMailer library.$mail = new PHPMailer;
: This line creates a new PHPMailer object.$mail->IsSMTP();
: This line sets the mailer to use SMTP for delivery.$mail->Host = 'smtp.example.com';
: This line sets the SMTP host.$mail->SMTPAuth = true;
: This line sets SMTP authentication to true.$mail->Username = 'username';
: This line sets the SMTP username.$mail->Password = 'password';
: This line sets the SMTP password.$mail->addAddress('[email protected]');
: This line sets the recipient address.$mail->Subject = 'Message Subject';
: This line sets the message subject.$mail->msgHTML(file_get_contents('message.html'));
: This line sets the message body to the contents of themessage.html
file.$mail->send();
: This line sends the message.
Helpful links
More of Phpmailer
- How do I configure the timeout settings for PHPMailer?
- How can I test PHPMailer online?
- How do I use PHPMailer to reply to an email?
- How can I use PHPMailer to send emails in PHP 8.0?
- How can I use PHPMailer to send emails through Outlook?
- How can I configure PHPMailer to work with GoDaddy?
- How do I manually install PHPMailer?
- How do I use PHPMailer to attach a ZIP file?
- How can I use PHPMailer without SMTP secure?
- How can I configure PHPMailer to support Polish characters?
See more codes...