Using WordPress templating for HTML emails
I am working on a plugin that needs to send out an email after a form submission.
I am using wp_mail()
for this and it works fine. My problem is that in my code the HTML is generated by a bunch of PHP strings being added to a variable like so:
$content = $html_headers;
$content .= 'h1' . the_title($post_id) . '/h1';
$content .= 'p' . $post_body . '/p;
..etc
I currently have more than 30 lines like that; and that allows me to finally do:
//add filter to allow html
add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
//Send email
wp_mail( '[email protected]', 'mail tester', $content, 'From: some one [email protected]' );
//remove filter to allow html (avoids some conflict.)
remove_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
I would prefer it if I could reference a separate file that uses normal WordPress theme template-tags in order to generate the content of the mail, so that in a separate file I'd have something like this:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
html xmlns="http://www.w3.org/1999/xhtml"
head
(headertags)
/head
body
h1?php the_title($post_id); ?/h1
p?php my_custom_template_body_tag(); ?/p
/body
/html
but I don't know how to then return that content to the wp_mail()
function. I have tried using file_get_contents()
but that just ignores PHP generated content, and I've looked into the heredoc syntax. But I find that quite ugly and error prone. Do I have any other options. I really love it if I could do something like this:
$content = parse_and_return_content_of('path/to/template/file', $arg);
Thank you
Topic html-email wp-mail templates Wordpress
Category Web