Skip to main content

Sending emails

Privy.sendEmail() allows you to send emails to your users without ever seeing the plaintext emails in plaintext.

Take on user emails

Taking on user emails works the same way as other data in Privy (see accessing data).

You can easily do so as follows:

//setup
import {PrivyClient} from '@privy-io/privy-browser'; //or privy-node for server-side
const client = new PrivyClient(PRIVY_API_KEY, PRIVY_API_SECRET);

// Single field named 'email'
const email = await client.put(userId, 'email', 'foo@example.com');

Send user email using the privy-node client.

You can then use Privy to send emails to any user who has this field populated. Note: sendEmail must be called via an admin privy-node client; this functionality is not supported by privy-browser.

info

Sending user emails is only available to requesters associated to the admin role.

const client = new PrivyClient(process.env.PRIVY_API_KEY, process.env.PRIVY_API_SECRET);

await client.sendEmail(userId, 'Email Subject', '<p>HTML or plaintext content to send</p>');
// true

In the event that the user email is missing, you will receive an error from the sendEmail() call.

Sending dynamic emails

You can also incorporate other user data in emails using Handlebars template syntax in your HTML content. Let's say we have a username field and want to use that to personalize the email, we can do the following:

// Store the username
await client.put(userId, 'username', 'foo_bar');

// Send the email
await client.sendEmail(userId, 'Test Subject', 'Hello{{#if username}} {{username}}{{/if}}!', [
'username',
]);

Note that you can (and should) check for data field availability in your Handlebars as seen above.

Demo

You can see sendEmail() in action in the following demo.