0

I am using the below to send an email to a user each time a custom field within their profile is updated. I can output plain text into the email body using the $message line which is great. How can I adapt this so I can output html where the $message goes?

// IF CUSTOM FIELD CHANGES
function sr_user_profile_update_virtuosity( $user_id, $old_user_data ) {
  $old_user_data = get_transient( 'sr_old_user_data_' . $user_id );
  $user = get_userdata( $user_id );
  if($old_user_data->virtuosity != $user->virtuosity) {
    $admin_email = $user->user_email;
    $message = sprintf( __( 'I want to output HTML here' ) ) . "\r\n\r\n";
    wp_mail( $admin_email, sprintf( __( 'IMPORTANT: Your newly purchased product is ready for you' ), get_option('blogname') ), $message );
  }
}
add_action( 'profile_update', 'sr_user_profile_update_virtuosity', 10, 2 );
5
  • so what's the problem? sprintf() outputs any text you want it to. sprintf('<html><body>%s</body></html>', 'hi mom!') Commented Feb 24, 2016 at 18:08
  • Oh, that's not what happened it just output the code as plain text. I'll try again later... Commented Feb 24, 2016 at 18:17
  • Hi @MarcB that still hasn't worked, it still outputs the code in play text. Any ideas? Commented Feb 24, 2016 at 18:50
  • Side-note: you might want to use PHP_EOL instead of "\r\n".... Commented Feb 24, 2016 at 18:56
  • @cale_b: php_eol is kinda pointless unless you know what the target viewing environment has the same eol character as the system running the script. e.g. php_eol on a windows box is \r\n, which is useless if the produced text will be consumed on linux. Commented Feb 24, 2016 at 19:48

1 Answer 1

1

You need to set the content type to html, by default its text/plain.

You can do this by using wp_mail_content_type filter.

add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
    return 'text/html';
}

For more info see here.

Sign up to request clarification or add additional context in comments.

1 Comment

Copy the relevant code here. Links break, and future visitors won't have a valuable answer to refer to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.