# Overwrite new user email using html blade template

**URL:** https://discourse.roots.io/t/overwrite-new-user-email-using-html-blade-template/24836
**Category:** sage
**Created:** 2023-02-20T17:41:26Z
**Posts:** 9

## Post 1 by @chuckienorton — 2023-02-20T17:41:26Z

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](https://developer.wordpress.org/reference/hooks/wp_new_user_notification_email/).

And i found within this forum how to [send out emails](https://discourse.roots.io/t/email-templates-in-blade-sage9/11115/2).

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

Any help is greatly appreciated!

---

## Post 2 by @strarsis — 2023-02-20T17:43:36Z

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](https://discourse.roots.io/t/sage-10-how-to-return-blade-templates-from-setup-php-for-custom-shortcodes/20538/2))?

---

## Post 3 by @talss89 — 2023-02-20T17:53:57Z

Remember to set the `Content-Type` if sending HTML:

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

---

## Post 4 by @chuckienorton — 2023-02-20T21:25:53Z

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

---

## Post 5 by @chuckienorton — 2023-02-20T21:28:32Z

PS - I am using Save 10 theme.

---

## Post 6 by @strarsis — 2023-02-20T21:37:14Z

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;
}
```

---

## Post 7 by @chuckienorton — 2023-02-20T22:07:01Z

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!

---

## Post 8 by @strarsis — 2023-02-21T00:00:23Z

> [@chuckienorton](#):
>
> needed to anonymize the function

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

---

## Post 9 by @ben — 2023-02-21T16:34:56Z

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!

> **[Rendering Blade Views | Acorn Docs](https://roots.io/acorn/docs/rendering-blade-views/)**
>
> Learn how to render Blade views from anywhere in your WordPress site.
