Change email content
To change the content of the default email templates, you can override the getContent
function in the emailDelivery
object. It allows you to return an object that has the following properties:
body
: This is the email's body. This can be HTML or just text as well.isHtml
: If the body is HTML, then this should betrue
.subject
: This is the subject of the email to send.toEmail
: The email will be sent to this email.
Other information like which email ID to send from is specified in the smtpSettings
object.
- NodeJS
- GoLang
- Python
import supertokens from "supertokens-node";
import EmailPassword from "supertokens-node/recipe/emailpassword";
import Session from "supertokens-node/recipe/session";
import { SMTPService } from "supertokens-node/recipe/emailpassword/emaildelivery";
import EmailVerification from "supertokens-node/recipe/emailverification"
import { SMTPService as EmailVerificationSMTPService } from "supertokens-node/recipe/emailverification/emaildelivery";
supertokens.init({
appInfo: {
apiDomain: "...",
appName: "...",
websiteDomain: "..."
},
recipeList: [
EmailPassword.init({
emailDelivery: {
service: new SMTPService({
smtpSettings: { /*...*/ },
override: (originalImplementation) => {
return {
...originalImplementation,
getContent: async function (input) {
// password reset content
let { passwordResetLink, user } = input;
// you can even call the original implementation and modify that
let originalContent = await originalImplementation.getContent(input)
originalContent.subject = "My custom subject";
return originalContent;
}
}
}
})
}
}),
// if email verification is enabled
EmailVerification.init({
emailDelivery: {
service: new EmailVerificationSMTPService({
smtpSettings: { /*...*/ },
override: (originalImplementation) => {
return {
...originalImplementation,
getContent: async function (input) {
// email verification content
let { emailVerifyLink, user } = input;
// you can even call the original implementation and modify that
let originalContent = await originalImplementation.getContent(input)
originalContent.subject = "My custom subject";
return originalContent;
}
}
}
})
}
}),
Session.init()
]
});
import (
"fmt"
"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
"github.com/supertokens/supertokens-golang/recipe/emailpassword"
"github.com/supertokens/supertokens-golang/recipe/emailpassword/epmodels"
"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{
emailpassword.Init(&epmodels.TypeInput{
EmailDelivery: &emaildelivery.TypeInput{
Service: emailpassword.MakeSMTPService(emaildelivery.SMTPServiceConfig{
Settings: emaildelivery.SMTPSettings{ /* ... */ },
Override: func(originalImplementation emaildelivery.SMTPInterface) emaildelivery.SMTPInterface {
originalGetContent := *originalImplementation.GetContent
(*originalImplementation.GetContent) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) (emaildelivery.EmailContent, error) {
// password reset content
passwordResetLink := input.PasswordReset.PasswordResetLink
user := input.PasswordReset.User
fmt.Println(passwordResetLink)
fmt.Println(user)
// you can even call the original implementation and modify that
originalContent, err := originalGetContent(input, userContext)
if err != nil {
return emaildelivery.EmailContent{}, err
}
originalContent.Subject = "My custom subject"
return originalContent, nil
}
return originalImplementation
},
}),
},
}),
// if email verification is enabled
emailverification.Init(evmodels.TypeInput{
EmailDelivery: &emaildelivery.TypeInput{
Service: emailverification.MakeSMTPService(emaildelivery.SMTPServiceConfig{
Settings: emaildelivery.SMTPSettings{ /* ... */ },
Override: func(originalImplementation emaildelivery.SMTPInterface) emaildelivery.SMTPInterface {
originalGetContent := *originalImplementation.GetContent
(*originalImplementation.GetContent) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) (emaildelivery.EmailContent, error) {
// email verification email content
emailVerificationLink := input.EmailVerification.EmailVerifyLink
user := input.EmailVerification.User
fmt.Println(emailVerificationLink)
fmt.Println(user)
// you can even call the original implementation and modify that
originalContent, err := originalGetContent(input, userContext)
if err != nil {
return emaildelivery.EmailContent{}, err
}
originalContent.Subject = "My custom subject"
return originalContent, nil
}
return originalImplementation
},
}),
},
}),
},
})
}
from supertokens_python import init, InputAppInfo
from supertokens_python.recipe import emailpassword
from supertokens_python.ingredients.emaildelivery.types import EmailDeliveryConfig, EmailContent, SMTPSettings
from supertokens_python.recipe.emailpassword.types import SMTPOverrideInput, EmailTemplateVars
from typing import Dict, Any
from supertokens_python.recipe.emailverification.types import SMTPOverrideInput as EVSMTPOverrideInput, EmailTemplateVars as EVEmailTemplateVars
from supertokens_python.recipe import emailverification
def custom_smtp_content_override(original_implementation: SMTPOverrideInput) -> SMTPOverrideInput:
original_get_content = original_implementation.get_content
async def get_content(template_vars: EmailTemplateVars, user_context: Dict[str, Any]) -> EmailContent:
# password reset content
_ = template_vars.password_reset_link
__ = template_vars.user
# you can even call the original implementation and modify that
original_content = await original_get_content(template_vars, user_context)
original_content.subject = "My custom subject"
return original_content
original_implementation.get_content = get_content
return original_implementation
def custom_smtp_email_verification_content_override(original_implementation: EVSMTPOverrideInput) -> EVSMTPOverrideInput:
original_get_content = original_implementation.get_content
async def get_content(template_vars: EVEmailTemplateVars, user_context: Dict[str, Any]) -> EmailContent:
# email verification content
_ = template_vars.email_verify_link
__ = template_vars.user
# you can even call the original implementation and modify that
original_content = await original_get_content(template_vars, user_context)
original_content.subject = "My custom subject"
return original_content
original_implementation.get_content = get_content
return original_implementation
init(
app_info=InputAppInfo(
api_domain="...", app_name="...", website_domain="..."),
framework='...',
recipe_list=[
emailpassword.init(
email_delivery=EmailDeliveryConfig(
service=emailpassword.SMTPService(
smtp_settings=SMTPSettings(...),
override=custom_smtp_content_override
)
)
),
# If email verification is enabled
emailverification.init(
mode="OPTIONAL",
email_delivery=EmailDeliveryConfig(
service=emailverification.SMTPService(
smtp_settings=SMTPSettings(...),
override=custom_smtp_email_verification_content_override
)
)
)
]
)