Victor Sokoliuk
Victor Sokoliuk

Reputation: 445

How to send html format massage using wp_mail()?

Hi I try to send mail using wp_mail(). For me is important the mail body contain the custom HTML template. For this purpose, I create the e-mail template, packed it into a file: mailtpl.php this file placed into the theme root. And in the functions.php I'm using this chunk of code:

$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = 'From: '.get_bloginfo("name").' <'.get_bloginfo("admin_email").'>' . "\r\n";
$message = include 'mailtpl.php';

wp_mail( $email, 'Registration on the site '.get_bloginfo("name"), $message, $headers );

This code is triggered when a new user is registered. But an e-mail comes to the mailbox with no content. Just 1 number displayed in the mail body.

Please help me figure out why this is happening.

Upvotes: 2

Views: 2885

Answers (2)

Fresz
Fresz

Reputation: 1913

As mentioned in comments the file_get_contents() is what was needed as you can't include a file in the email message.

$template = file_get_contents('mailtpl.php', true);

$headers[] = 'Content-type: text/html; charset=utf-8';
$headers[] = 'From: '.get_bloginfo("name").' <'.get_bloginfo("admin_email").'>' . "\r\n";
$message = $template;

wp_mail( $email, 'Registration on the site '.get_bloginfo("name"), $message, $headers );

Upvotes: 2

Victor Sokoliuk
Victor Sokoliuk

Reputation: 445

thanks for @Fresz comment, I used the:

$message = file_get_contents('mailtpl.php', true);

insted the:

$message = include 'mailtpl.php';

And the e-mail message starts forming in the correct way.

Upvotes: 0

Related Questions