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.htmlfile.$mail->send();: This line sends the message.
Helpful links
More of Phpmailer
- How do I use PHPMailer to attach a ZIP file?
- How do I use PHPMailer with OAuth 2.0?
- How can I use PHPMailer to send emails with a Yahoo account?
- How do I view the log for my PHPMailer emails?
- How do I check which version of PHPMailer I'm using?
- How can I configure PHPMailer to support Polish characters?
- How do I configure PHPmailer to use Zoho SMTP?
- How can I use PHPMailer without SMTP secure?
- How do I configure PHPMailer to use TLS 1.2?
- How can I set a timeout for PHPMailer SMTP?
See more codes...