I want to use a template as the message in an email which I send with wp_mail. But I get the error: "failed to open stream: No such file or directory".

add_filter( 'wp_mail_content_type', 'set_content_type' );

$to = 'mymailg@hotmail.com';
$subject = 'welcome';
$message = file_get_contents('email_template.html' );
$headers = "From: me<noreply@me.com>";
$mailsent = wp_mail( $to,$subject,$message,$headers );

remove_filter( 'wp_mail_content_type', 'set_content_type' );

This code is inside a template-part. "email_template.html" is in the same folder as this template-part.

  • Have you tried ./email_template.html? – s_ha_dum Oct 30 '15 at 15:26
  • I got "Warning: file_get_contents(./email_template.html): failed to open stream" – Galivan Oct 30 '15 at 15:30
  • 1
    There should be a path associated with that error. Is it correct? – s_ha_dum Oct 30 '15 at 15:43
  • 1
    where is the template part relative to the file that's loading it? use dirname(__FILE__) to get the path relative to the template part and not the parent file. – Milo Oct 30 '15 at 15:47
  • I got it to work! The full path was missing, so I added file_get_contents(TEMPLATEPATH . '/email_template.html' ); Is this always required?? – Galivan Oct 30 '15 at 15:56
$get_file_content = fopen(TEMPLATEPATH . '/mail/create-account-member.php', "r");   

// Lê cada uma das linhas do arquivo
while(!feof($get_file_content)) { 
   $message .= fgets($get_file_content) . "<br/>";

wp_mail( $user_email, $subject, $message, $headers );
  • 2
    Can you explain a bit how your snippet is supposed to work? Preferably in an edit to your answer. Also, Spanish comments are a bit tough: Not everybody speaks that language. – kaiser May 29 '16 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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