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 use PHPMailer to attach a ZIP file?
- How can I set up PHPMailer to use Zimbra SMTP?
- How can I configure PHPMailer to support Polish characters?
- How can I use PHPMailer SMTPDebug to debug my email sending process?
- How can I use PHPMailer to connect to a POP3 server?
- How do I view the log for my PHPMailer emails?
- How can I add a new line to the body of an email using PHPMailer?
- How can I use PHPMailer in Yii 1?
- How can I configure PHPMailer to work with GoDaddy?
- How can I use PHPMailer to send emails through Yandex?
See more codes...