Skip to main content

Method 3) Custom method

This method allows you provide a callback using which you can send emails however you like. The input to the callback will be email template variables, so you can freely design the email as well. Use this method if you are:

  • Using a third party email service like Mailchimp.
  • You want to do some custom spam protection before sending the email.
  • You already have an email sending infrastructure and want to use that.
import supertokens from "supertokens-node";
import ThirdPartyPasswordless from "supertokens-node/recipe/thirdpartypasswordless";
import Session from "supertokens-node/recipe/session";
import EmailVerification from "supertokens-node/recipe/emailverification"

supertokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
ThirdPartyPasswordless.init({
emailDelivery: {
override: (originalImplementation) => {
return {
...originalImplementation,
sendEmail: async function (input) {
let {
codeLifetime, // amount of time the code is alive for (in MS)
email,
urlWithLinkCode, // magic link
userInputCode, // OTP
} = input;

// TODO: create and send email
}
}
}
},
}),

// if email verification is enabled
EmailVerification.init({
emailDelivery: {
override: (originalImplementation) => {
return {
...originalImplementation,
sendEmail: async function (input) {
// TODO: create and send email verification email

// Or use the original implementation which calls the default service,
// or a service that you may have specified in the emailDelivery object.
return originalImplementation.sendEmail(input);
}
}
}
},
}),
Session.init()
]
});

If you call the original implementation function for sendEmail, it will use the service that you have configured. If you have not configured any service, it will use the default service.

So using this method, you can for example, have your custom way of sending email verification emails, but use the default or SMTP service to send the passwordless login emails.

important

When using this callback, you must manage sending the email yourself.

Which frontend SDK do you use?
supertokens-web-js / mobile
supertokens-auth-react