Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using wp mail to send an html email. But there's quite a lot of html code in the email, so rather than including all the code in my wp mail function, is it possible to have the code in a separate template and just include this template in the function? Here is what I have

<?php if ( isset( $_POST['submitted'] )) {
   add_filter('wp_mail_content_type',create_function('', 'return "text/html"; '));
   $emailTo = 'person@gmail.com' ;
   $subject = 'This is the subject';
   $body = get_template_part( 'includes/my_email_template' );
   $headers = 'From: My Name' . "\r\n";
   wp_mail($emailTo, $subject, $body, $headers);
}?>

I'd like to be able to put all of my html code in 'my_email_template' but when I try this, no email is sent. Am I including the template incorrectly? Thanks in advance for any answers.

share|improve this question
1  
Syntax error: $emailTo = person@gmail.com ; try $emailTo = 'person@gmail.com';. –  toscho Apr 18 '13 at 19:42
    
You're calling the template incorrectly - see the codex for get_template_part –  vancoder Apr 18 '13 at 19:44
    
Thank you both. That syntax error was just poor copy/paste/adjust from me. I can confirm it's correct in my actual template. –  Xav Apr 18 '13 at 19:53
    
@vancoder I'm not even sure I should be doing get_template_path. The file only contains HTML and a few variables I have set in the file that calls it. Maybe include (TEMPLATEPATH . 'includes/my_email_template.php'); is the correct option? –  Xav Apr 18 '13 at 19:58
    
Does your template file return a string? If you just include it any HTML or echos will just print to the screen rather than get assigned to $body. I imagine that will cause some errors with wp_mail as well. –  s_ha_dum Apr 18 '13 at 20:29
add comment

2 Answers

up vote 2 down vote accepted

Per my comment to your question, I believe the problem is that includeing files, whether directly or using get_template_part isn't likely to give you a string to pass to $body and that is going to cause errors in the code, or at the very least unespected behavior.

I would avoid reading files into memory and just create a function that returns your $body content.

function get_email_body_wpse_96357() {
    $body = '<p>Hi</p>';
    return $body;
}

Then use $body = get_email_body_wpse_96357(); as needed. An advantage of this method is that you can easily pass parameters to the function if you ever decide to do so. You could also use variables in an included file but it can be messy.

If you don't want to load that function all the time, then put it in a file by itself and include that file only when you need the function.

share|improve this answer
    
Thank you s_ha_dum, this works beautifully! –  Xav Apr 18 '13 at 21:35
add comment

The function get_template_part() doesn't return the HTML but echo it (it uses locate_template() which loads the file - echo).

You can either turn output buffering on using ob_start() and put the buffer in to your variable or you can use file_get_contents().

On your case I think the best solution is this:

$body = file_get_contents(TEMPLATEPATH . 'includes/my_email_template.php');
share|improve this answer
    
Thanks for your answer ItayXD but I now have it working using the answer above –  Xav Apr 18 '13 at 21:37
    
I saw, the answer above is a far inferior IMO. The file_get_contents is exactly what you were looking for, and it is far easier to right html file normally than use the above solution. In fact I just can't see how is it any better, I can see only the down side. –  ItayXD Apr 18 '13 at 21:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.