Remove isEnabled from config

This commit is contained in:
Vishal 2024-03-28 17:44:08 +05:30
parent 1f75633c5c
commit d5f0334a34
2 changed files with 102 additions and 91 deletions

View file

@ -157,22 +157,29 @@ key:
jwt:
secret: i2DecQmfGreG6q1vBj5tCokhlN41gcfS2cjOs9Po-u8=
# SMTP configuration (optional)
#
# Configure credentials here for sending mails from museum (e.g. OTP emails).
#
# The smtp credentials will be used if the host is specified. Otherwise it will
# try to use the transmail credentials. Ideally, one of smtp or transmail should
# be configured for a production instance.
smtp:
host:
port:
username:
password:
# Zoho Zeptomail config (optional)
# Use case: Sending emails
#
# This is an alternative to the `smtp` configuration for sending emails. If this
# is set (and SMTP credentials are not set), then museum will use the transmail
# SDK for sending emails using Zoho Zeptomail.
transmail:
# Transmail token
# Mail agent: dev
key:
# AWS SES
# Use case: Sending mails
smtp:
isEnabled: false
host:
port:
username:
password:
# Apple config (optional)
# Use case: In-app purchases
apple:
@ -288,4 +295,4 @@ jobs:
# By default, this job is disabled.
enabled: false
# If provided, only objects that begin with this prefix are pruned.
prefix: ""
prefix: ""

View file

@ -21,88 +21,12 @@ import (
// Send sends an email
func Send(toEmails []string, fromName string, fromEmail string, subject string, htmlBody string, inlineImages []map[string]interface{}) error {
if len(toEmails) == 0 {
return ente.ErrBadRequest
}
authKey := viper.GetString("transmail.key")
silent := viper.GetBool("internal.silent")
if authKey == "" || silent {
log.Infof("Skipping sending email to %s: %s", toEmails[0], subject)
return nil
}
var to []ente.ToEmailAddress
for _, toEmail := range toEmails {
to = append(to, ente.ToEmailAddress{EmailAddress: ente.EmailAddress{Address: toEmail}})
}
mail := &ente.Mail{
BounceAddress: ente.TransmailEndBounceAddress,
From: ente.EmailAddress{Address: fromEmail, Name: fromName},
Subject: subject,
Htmlbody: htmlBody,
InlineImages: inlineImages,
}
if len(toEmails) == 1 {
mail.To = to
smtpHost := viper.GetString("smtp.host")
if smtpHost != "" {
return sendViaSMTP(toEmails, fromName, fromEmail, subject, htmlBody, inlineImages)
} else {
mail.Bcc = to
return sendViaTransmail(toEmails, fromName, fromEmail, subject, htmlBody, inlineImages)
}
postBody, err := json.Marshal(mail)
if err != nil {
return stacktrace.Propagate(err, "")
}
reqBody := bytes.NewBuffer(postBody)
client := &http.Client{}
req, err := http.NewRequest("POST", ente.TransmailEndPoint, reqBody)
if err != nil {
return stacktrace.Propagate(err, "")
}
req.Header.Set("accept", "application/json")
req.Header.Set("content-type", "application/json")
req.Header.Set("authorization", authKey)
_, err = client.Do(req)
return stacktrace.Propagate(err, "")
}
func SendTemplatedEmail(to []string, fromName string, fromEmail string, subject string, templateName string, templateData map[string]interface{}, inlineImages []map[string]interface{}) error {
body, err := getMailBody(templateName, templateData)
if err != nil {
return stacktrace.Propagate(err, "")
}
isSESEnabled := viper.GetBool("smtp.isEnabled")
if isSESEnabled {
return sendViaSMTP(to, fromName, fromEmail, subject, body, inlineImages)
} else {
return Send(to, fromName, fromEmail, subject, body, inlineImages)
}
}
func GetMaskedEmail(email string) string {
at := strings.LastIndex(email, "@")
if at >= 0 {
username, domain := email[:at], email[at+1:]
maskedUsername := ""
for i := 0; i < len(username); i++ {
maskedUsername += "*"
}
return maskedUsername + "@" + domain
} else {
// Should ideally never happen, there should always be an @ symbol
return "[invalid_email]"
}
}
// getMailBody generates the mail html body from provided template and data
func getMailBody(templateName string, templateData map[string]interface{}) (string, error) {
htmlbody := new(bytes.Buffer)
t := template.Must(template.New(templateName).ParseFiles("mail-templates/" + templateName))
err := t.Execute(htmlbody, templateData)
if err != nil {
return "", stacktrace.Propagate(err, "")
}
return htmlbody.String(), nil
}
func sendViaSMTP(toEmails []string, fromName string, fromEmail string, subject string, htmlBody string, inlineImages []map[string]interface{}) error {
@ -167,3 +91,83 @@ func sendViaSMTP(toEmails []string, fromName string, fromEmail string, subject s
return nil
}
func sendViaTransmail(toEmails []string, fromName string, fromEmail string, subject string, htmlBody string, inlineImages []map[string]interface{}) error {
if len(toEmails) == 0 {
return ente.ErrBadRequest
}
authKey := viper.GetString("transmail.key")
silent := viper.GetBool("internal.silent")
if authKey == "" || silent {
log.Infof("Skipping sending email to %s: %s", toEmails[0], subject)
return nil
}
var to []ente.ToEmailAddress
for _, toEmail := range toEmails {
to = append(to, ente.ToEmailAddress{EmailAddress: ente.EmailAddress{Address: toEmail}})
}
mail := &ente.Mail{
BounceAddress: ente.TransmailEndBounceAddress,
From: ente.EmailAddress{Address: fromEmail, Name: fromName},
Subject: subject,
Htmlbody: htmlBody,
InlineImages: inlineImages,
}
if len(toEmails) == 1 {
mail.To = to
} else {
mail.Bcc = to
}
postBody, err := json.Marshal(mail)
if err != nil {
return stacktrace.Propagate(err, "")
}
reqBody := bytes.NewBuffer(postBody)
client := &http.Client{}
req, err := http.NewRequest("POST", ente.TransmailEndPoint, reqBody)
if err != nil {
return stacktrace.Propagate(err, "")
}
req.Header.Set("accept", "application/json")
req.Header.Set("content-type", "application/json")
req.Header.Set("authorization", authKey)
_, err = client.Do(req)
return stacktrace.Propagate(err, "")
}
func SendTemplatedEmail(to []string, fromName string, fromEmail string, subject string, templateName string, templateData map[string]interface{}, inlineImages []map[string]interface{}) error {
body, err := getMailBody(templateName, templateData)
if err != nil {
return stacktrace.Propagate(err, "")
}
return Send(to, fromName, fromEmail, subject, body, inlineImages)
}
func GetMaskedEmail(email string) string {
at := strings.LastIndex(email, "@")
if at >= 0 {
username, domain := email[:at], email[at+1:]
maskedUsername := ""
for i := 0; i < len(username); i++ {
maskedUsername += "*"
}
return maskedUsername + "@" + domain
} else {
// Should ideally never happen, there should always be an @ symbol
return "[invalid_email]"
}
}
// getMailBody generates the mail html body from provided template and data
func getMailBody(templateName string, templateData map[string]interface{}) (string, error) {
htmlbody := new(bytes.Buffer)
t := template.Must(template.New(templateName).ParseFiles("mail-templates/" + templateName))
err := t.Execute(htmlbody, templateData)
if err != nil {
return "", stacktrace.Propagate(err, "")
}
return htmlbody.String(), nil
}