Overwrite new user email using html blade template

I’d like send new users an html email, and manage that email in a file like /resources/views/emails/welcome.blade.php.

I am aware that there is a filter to overwrite the WP email here.

And i found within this forum how to send out emails.

But I’m unclear how I would use that filter and return a blade php created html email.

Any help is greatly appreciated!

1 Like

You use a hook for the custom mail HTML and render the template to a string and return it instead.

add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $message =  \App\template('emails/welcome', compact('user', 'blogname'));
    $wp_new_user_notification_email['message'] = $message;
    return $wp_new_user_notification_email;
}

In this (admittedly untested) example the blade template receives the $user and $blogname variables and the rendered template is used as the email message.

Is this a Sage 9 or Sage 10 theme (Sage 10 template rendering)?

4 Likes

Remember to set the Content-Type if sending HTML:

$wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');

3 Likes

Wow such a quick reply thanks! I’ll test soon and mark solved when i can confirm. Thanks!

PS - I am using Save 10 theme.

Sage 10 has a different method for rendering Blade-PHP templates (thanks @talss89):

use function Roots\view;

add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
    $message =  view('emails/welcome', compact('user', 'blogname'))->render();
    $wp_new_user_notification_email['message'] = $message;

    $wp_new_user_notification_email['headers'] = [ 'Content-Type: text/html; charset=UTF-8' ];

    return $wp_new_user_notification_email;
}
3 Likes

This is it! Thanks so much!!!

In order to not get a callback error I needed to anonymize the function. Here is what I ended up with:

add_filter('wp_new_user_notification_email', function ($wp_new_user_notification_email, $user, $blogname) {
    $key = get_password_reset_key($user);
    $encoded_user_login = rawurlencode($user->user_login);
    $password_reset_link = network_site_url('wp-login.php?action=rp&key=' . $key . '&login=' . $encoded_user_login, 'login');

    $message =  view('emails/welcome', compact('user', 'blogname', 'password_reset_link'))->render();
    $wp_new_user_notification_email['message'] = $message;
    $wp_new_user_notification_email['headers'] = [ 'Content-Type: text/html; charset=UTF-8' ];

    return $wp_new_user_notification_email;
}, 10, 3);

Thank you so much for the quick amazing help!

3 Likes

Ah right, the example didn’t use namespaces, but this is used in setup.php in recent Sage
__NAMESPACE__ . '\\function_name'

1 Like

Y’all are awesome. I added a new guide to the Acorn docs w/ the email snippet from above and gave everyone in this topic credit — thanks for making this topic @chuckienorton and thanks @strarsis and @talss89 for jumping in!

5 Likes