Pre / Post Hooks / Override
We provide you funtionality to run code before and after an email is sent. You can use this for:
- Logging purposes
- Spam protection purposes
- Modifying the email template variables before sending the emails
- NodeJS
- GoLang
- Python
import supertokens from "supertokens-node";import ThirdPartyEmailPassword from "supertokens-node/recipe/thirdpartyemailpassword";import Session from "supertokens-node/recipe/session";import EmailVerification from "supertokens-node/recipe/emailverification"
supertokens.init({ appInfo: { apiDomain: "...", appName: "...", websiteDomain: "..." }, recipeList: [ ThirdPartyEmailPassword.init({ emailDelivery: { override: (originalImplementation) => { return { ...originalImplementation, sendEmail: async function (input) { // TODO: run some logic before sending the email await originalImplementation.sendEmail(input);
// TODO: run some logic post sending the email } } } }, }),
// if email verification is enabled EmailVerification.init({ emailDelivery: { override: (originalImplementation) => { return { ...originalImplementation, sendEmail: async function (input) { // TODO: run some logic before sending the email await originalImplementation.sendEmail(input);
// TODO: run some logic post sending the email } } } }, }), Session.init() ]});
import ( "github.com/supertokens/supertokens-golang/ingredients/emaildelivery" "github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword" "github.com/supertokens/supertokens-golang/recipe/thirdpartyemailpassword/tpepmodels" "github.com/supertokens/supertokens-golang/recipe/emailverification" "github.com/supertokens/supertokens-golang/recipe/emailverification/evmodels" "github.com/supertokens/supertokens-golang/supertokens")
func main() { supertokens.Init(supertokens.TypeInput{ RecipeList: []supertokens.Recipe{ thirdpartyemailpassword.Init(&tpepmodels.TypeInput{ EmailDelivery: &emaildelivery.TypeInput{ Override: func(originalImplementation emaildelivery.EmailDeliveryInterface) emaildelivery.EmailDeliveryInterface { originalSendEmail := *originalImplementation.SendEmail
(*originalImplementation.SendEmail) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) error { // TODO: run some logic before sending the email
err := originalSendEmail(input, userContext) if err != nil { return err }
// TODO: run some logic post sending the email return nil }
return originalImplementation }, }, }),
// if email verification is enabled emailverification.Init(evmodels.TypeInput{ Mode: evmodels.ModeRequired, EmailDelivery: &emaildelivery.TypeInput{ Override: func(originalImplementation emaildelivery.EmailDeliveryInterface) emaildelivery.EmailDeliveryInterface { originalSendEmail := *originalImplementation.SendEmail
(*originalImplementation.SendEmail) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) error { // TODO: run some logic before sending the email
err := originalSendEmail(input, userContext) if err != nil { return err }
// TODO: run some logic post sending the email return nil }
return originalImplementation }, }, }), }, })}
from supertokens_python import init, InputAppInfofrom supertokens_python.recipe.thirdpartyemailpassword.types import EmailDeliveryOverrideInput, EmailTemplateVarsfrom supertokens_python.recipe import thirdpartyemailpasswordfrom typing import Dict, Anyfrom supertokens_python.ingredients.emaildelivery.types import EmailDeliveryConfigfrom supertokens_python.recipe.emailverification.types import EmailDeliveryOverrideInput as EVEmailDeliveryOverrideInput, EmailTemplateVars as EVEmailTemplateVarsfrom supertokens_python.recipe import emailverification
def custom_email_deliver(original_implementation: EmailDeliveryOverrideInput) -> EmailDeliveryOverrideInput: original_send_email = original_implementation.send_email
async def send_email(template_vars: EmailTemplateVars, user_context: Dict[str, Any]) -> None: # TODO: run some logic before sending the email
resp = await original_send_email(template_vars, user_context)
# TODO: run some logic after sending the email return resp original_implementation.send_email = send_email return original_implementation
def custom_emailverification_delivery(original_implementation: EVEmailDeliveryOverrideInput) -> EVEmailDeliveryOverrideInput: original_send_email = original_implementation.send_email
async def send_email(template_vars: EVEmailTemplateVars, user_context: Dict[str, Any]) -> None:
# TODO: run some logic before sending the email
resp = await original_send_email(template_vars, user_context)
# TODO: run some logic after sending the email
return resp
original_implementation.send_email = send_email return original_implementation
init( app_info=InputAppInfo( api_domain="...", app_name="...", website_domain="..."), framework='...', recipe_list=[ thirdpartyemailpassword.init( email_delivery=EmailDeliveryConfig(override=custom_email_deliver) ),
# If email verification is enabled emailverification.init( mode="OPTIONAL", email_delivery=EmailDeliveryConfig(override=custom_emailverification_delivery)) ])