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
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 ({ codeLifetime, // amount of time the code is alive for (in MS) email, urlWithLinkCode, // magic link userInputCode, // OTP }) { // 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, InputAppInfofrom supertokens_python.recipe.passwordless.types import EmailDeliveryOverrideInput, EmailTemplateVarsfrom supertokens_python.recipe import passwordlessfrom typing import Dict, Anyfrom 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.