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.
- NodeJS
- GoLang
- Python
- Other Frameworks
Important
For other backend frameworks, you can follow our guide on how to spin up a separate server configured with the SuperTokens backend SDK to authenticate requests and issue session tokens.
import supertokens from "supertokens-node";
import Passwordless from "supertokens-node/recipe/passwordless";
import Session from "supertokens-node/recipe/session";
supertokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
Passwordless.init({
emailDelivery: {
override: (originalImplementation) => {
return {
...originalImplementation,
sendEmail: async function ({
isFirstFactor,
codeLifetime, // amount of time the code is alive for (in MS)
email,
urlWithLinkCode, // magic link
userInputCode, // OTP
}) {
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
}
}
}
}
},
}),
Session.init()
]
});
import (
"fmt"
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
"github.com/supertokens/supertokens-golang/recipe/passwordless"
"github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
"github.com/supertokens/supertokens-golang/supertokens"
)
func main() {
supertokens.Init(supertokens.TypeInput{
RecipeList: []supertokens.Recipe{
passwordless.Init(plessmodels.TypeInput{
EmailDelivery: &emaildelivery.TypeInput{
Override: func(originalImplementation emaildelivery.EmailDeliveryInterface) emaildelivery.EmailDeliveryInterface {
(*originalImplementation.SendEmail) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) error {
// amount of time the code is alive for (in MS)
codeLifetime := input.PasswordlessLogin.CodeLifetime
email := input.PasswordlessLogin.Email
// magic link
urlWithLinkCode := input.PasswordlessLogin.UrlWithLinkCode
// OTP
userInputCode := input.PasswordlessLogin.UserInputCode
fmt.Println(codeLifetime)
fmt.Println(email)
fmt.Println(urlWithLinkCode)
fmt.Println(userInputCode)
// TODO: create and send email
return nil
}
return originalImplementation
},
},
}),
},
})
}
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe.passwordless.types import EmailDeliveryOverrideInput, EmailTemplateVars
from supertokens_python.recipe import passwordless
from typing import Dict, Any
from supertokens_python.ingredients.emaildelivery.types import EmailDeliveryConfig
def custom_email_deliver(original_implementation: EmailDeliveryOverrideInput) -> EmailDeliveryOverrideInput:
async def send_email(template_vars: EmailTemplateVars, user_context: Dict[str, Any]) -> None:
# amount of time the code is alive for (in MS)
_ = template_vars.code_life_time
__ = template_vars.email
___ = template_vars.url_with_link_code # magic link
____ = template_vars.user_input_code # OTP
# TODO: create and send email...
original_implementation.send_email = send_email
return original_implementation
init(
app_info=InputAppInfo(
api_domain="...", app_name="...", website_domain="..."),
framework='...',
recipe_list=[
passwordless.init(
email_delivery=EmailDeliveryConfig(override=custom_email_deliver)
)
]
)
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.
important
When using this callback, you must manage sending the email yourself.