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
- 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";
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) {
                            // 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/passwordless"
    "github.com/supertokens/supertokens-golang/recipe/passwordless/plessmodels"
    "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{
            passwordless.Init(plessmodels.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, 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
from supertokens_python.recipe.emailverification.types import (
    EmailDeliveryOverrideInput as EVEmailDeliveryOverrideInput,
    EmailTemplateVars as EVEmailTemplateVars,
)
from 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=[
        passwordless.init(
            email_delivery=EmailDeliveryConfig(override=custom_email_deliver),
        ),
        # If email verification is enabled
        emailverification.init(
            mode="OPTIONAL",
            email_delivery=EmailDeliveryConfig(
                override=custom_emailverification_delivery
            ),
        ),
    ],
)