Skip to main content

If you are using our backend SDK that is lesser than the following versions, please visit the older documentation link here.

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 Passwordless from "supertokens-node/recipe/passwordless";
import Session from "supertokens-node/recipe/session";
import EmailVerification from "supertokens-node/recipe/emailverification"

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

if (isFirstFactor) {
// this is for first factor login
// TODO: create and send email
} else {
// this is for MFA login (only applicable if you are using MFA).
// In this case, the urlWithLinkCode will always be undefined since
// we only support OTP based MFA and not link based MFA
// 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.

Looking for older versions of the documentation?
Which UI do you use?
Custom UI
Pre built UI