add: plaintext email; improved templating

This commit is contained in:
$(pass /github/name)
2024-08-28 11:35:43 +02:00
parent 00ca11d2e6
commit 6ac2b32a1f
10 changed files with 287 additions and 116 deletions

View File

@@ -33,7 +33,6 @@ type SMTPConfig struct {
Host string `json:"Host" envconfig:"SMTP_HOST"`
User string `json:"User" envconfig:"SMTP_USER"`
Password string `json:"Password" envconfig:"SMTP_PASS"`
Mailtype string `json:"Mailtype" default:"html" envconfig:"MAIL_TYPE"`
AdminEmail string `json:"AdminEmail" envconfig:"ADMIN_MAIL"`
Port int `json:"Port" default:"465" envconfig:"SMTP_PORT"`
}
@@ -42,6 +41,7 @@ type TemplateConfig struct {
MailPath string `json:"MailPath" default:"templates/email" envconfig:"TEMPLATE_MAIL_PATH"`
HTMLPath string `json:"HTMLPath" default:"templates/html" envconfig:"TEMPLATE_HTML_PATH"`
StaticPath string `json:"StaticPath" default:"templates/css" envconfig:"TEMPLATE_STATIC_PATH"`
LogoURI string `json:"LogoURI" envconfig:"LOGO_URI"`
}
type RecipientsConfig struct {
@@ -60,6 +60,7 @@ type Config struct {
Templates TemplateConfig `json:"templates"`
Recipients RecipientsConfig `json:"recipients"`
ConfigFilePath string `json:"config_file_path" envconfig:"CONFIG_FILE_PATH"`
WebsiteTitle string `json:"WebsiteTitle" envconfig:"WEBSITE_TITLE"`
BaseURL string `json:"BaseUrl" envconfig:"BASE_URL"`
Env string `json:"Environment" default:"development" envconfig:"ENV"`
DB DatabaseConfig `json:"db"`
@@ -68,16 +69,17 @@ type Config struct {
}
var (
BaseURL string
CFGPath string
CFG Config
Auth AuthenticationConfig
DB DatabaseConfig
Templates TemplateConfig
SMTP SMTPConfig
Recipients RecipientsConfig
Env string
Security SecurityConfig
BaseURL string
WebsiteTitle string
CFGPath string
CFG Config
Auth AuthenticationConfig
DB DatabaseConfig
Templates TemplateConfig
SMTP SMTPConfig
Recipients RecipientsConfig
Env string
Security SecurityConfig
)
var environmentOptions map[string]bool = map[string]bool{
"development": true,
@@ -117,6 +119,7 @@ func LoadConfig() {
Recipients = CFG.Recipients
Security = CFG.Security
Env = CFG.Env
WebsiteTitle = CFG.WebsiteTitle
logger.Info.Printf("Config loaded: %#v", CFG)
}

View File

@@ -128,6 +128,12 @@ func checkWelcomeMail(message *utils.Email, user *models.User) error {
if !strings.Contains(message.Body, config.BaseURL) {
return fmt.Errorf("Base Url (%v) has not been rendered in registration mail.", config.BaseURL)
}
if !strings.Contains(message.Body, config.BaseURL+config.Templates.LogoURI) {
return fmt.Errorf("Logo Url (%v) has not been rendered in registration mail.", config.BaseURL+config.WebsiteTitle)
}
if !strings.Contains(message.Body, config.WebsiteTitle) {
return fmt.Errorf("Website title (%v) has not been rendered in registration mail.", config.WebsiteTitle)
}
return nil
}

View File

@@ -74,7 +74,7 @@ func (s *EmailService) SendVerificationEmail(user *models.User, token *string) e
}
subject := constants.MailVerificationSubject
body, err := ParseTemplate("mail_verification.html", data)
body, err := ParseTemplate("mail_verification.tmpl", data)
if err != nil {
logger.Error.Print("Couldn't send verification mail")
return err
@@ -92,6 +92,8 @@ func (s *EmailService) SendWelcomeEmail(user *models.User) error {
BASEURL string
MembershipID int64
MembershipFee float32
Logo string
WebsiteTitle string
RentalFee float32
}{
Company: user.Company,
@@ -101,10 +103,12 @@ func (s *EmailService) SendWelcomeEmail(user *models.User) error {
MembershipFee: float32(user.Membership.SubscriptionModel.MonthlyFee),
RentalFee: float32(user.Membership.SubscriptionModel.HourlyRate),
BASEURL: config.BaseURL,
WebsiteTitle: config.WebsiteTitle,
Logo: config.Templates.LogoURI,
}
subject := constants.MailWelcomeSubject
body, err := ParseTemplate("mail_welcome.html", data)
body, err := ParseTemplate("mail_welcome.tmpl", data)
if err != nil {
logger.Error.Print("Couldn't send welcome mail")
return err
@@ -130,6 +134,8 @@ func (s *EmailService) SendRegistrationNotification(user *models.User) error {
MembershipID int64
RentalFee float32
MembershipFee float32
Logo string
WebsiteTitle string
}{
Company: user.Company,
FirstName: user.FirstName,
@@ -146,10 +152,12 @@ func (s *EmailService) SendRegistrationNotification(user *models.User) error {
Phone: user.Phone,
IBAN: user.BankAccount.IBAN,
BASEURL: config.BaseURL,
Logo: config.Templates.LogoURI,
WebsiteTitle: config.WebsiteTitle,
}
subject := constants.MailRegistrationSubject
body, err := ParseTemplate("mail_registration.html", data)
body, err := ParseTemplate("mail_registration.tmpl", data)
if err != nil {
logger.Error.Print("Couldn't send admin notification mail")
return err
@@ -159,16 +167,20 @@ func (s *EmailService) SendRegistrationNotification(user *models.User) error {
func (s *EmailService) RelayContactFormMessage(sender string, name string, message string) error {
data := struct {
Message string
Name string
BASEURL string
Message string
Name string
BASEURL string
Logo string
WebsiteTitle string
}{
Message: message,
Name: name,
BASEURL: config.BaseURL,
Message: message,
Name: name,
BASEURL: config.BaseURL,
Logo: config.Templates.LogoURI,
WebsiteTitle: config.WebsiteTitle,
}
subject := constants.MailContactSubject
body, err := ParseTemplate("mail_contact_form.html", data)
body, err := ParseTemplate("mail_contact_form.tmpl", data)
if err != nil {
logger.Error.Print("Couldn't send contact form message mail")
return err