From cd63133770259596e00867b83ae357440d1d4045 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 15 Jan 2024 15:28:49 -0600 Subject: [PATCH] Dynamically set copyright year in email templates (#16092) Related to #15758 Changes: - Updated the copyright year in email templates to be set to the current year when the email is sent. Testing steps: 1. Configure a local Fleet instance to send emails to Mailpit 2. Activate SMTP to send a test email. 3. Invite a new user to the Fleet instance. 4. Change the email address of a user. 5. Log out of the Fleet instance and send a password reset email. 6. Go to the Mailpit dashboard and look at the copyright years in the emails sent by the Fleet instance --- server/mail/invite.go | 11 +++++++---- server/mail/mail.go | 6 ++++-- server/mail/templates/change_email_confirmation.html | 2 +- server/mail/templates/invite_token.html | 2 +- server/mail/templates/password_reset.html | 2 +- server/mail/templates/smtp_setup.html | 2 +- server/mail/users.go | 12 +++++++++--- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/server/mail/invite.go b/server/mail/invite.go index 599ba6aae1..2a9d8b9607 100644 --- a/server/mail/invite.go +++ b/server/mail/invite.go @@ -3,6 +3,7 @@ package mail import ( "bytes" "html/template" + "time" "github.com/fleetdm/fleet/v4/server" "github.com/fleetdm/fleet/v4/server/fleet" @@ -11,13 +12,15 @@ import ( // InviteMailer is used to build an email template for the invite email. type InviteMailer struct { *fleet.Invite - BaseURL template.URL - AssetURL template.URL - InvitedBy string - OrgName string + BaseURL template.URL + AssetURL template.URL + InvitedBy string + OrgName string + CurrentYear int } func (i *InviteMailer) Message() ([]byte, error) { + i.CurrentYear = time.Now().Year() t, err := server.GetTemplate("server/mail/templates/invite_token.html", "email_template") if err != nil { return nil, err diff --git a/server/mail/mail.go b/server/mail/mail.go index e4a379e194..8e5ea5c7e8 100644 --- a/server/mail/mail.go +++ b/server/mail/mail.go @@ -275,11 +275,13 @@ func dialTimeout(addr string, tlsConfig *tls.Config) (client *smtp.Client, err e // SMTPTestMailer is used to build an email message that will be used as // a test message when testing SMTP configuration type SMTPTestMailer struct { - BaseURL template.URL - AssetURL template.URL + BaseURL template.URL + AssetURL template.URL + CurrentYear int } func (m *SMTPTestMailer) Message() ([]byte, error) { + m.CurrentYear = time.Now().Year() t, err := server.GetTemplate("server/mail/templates/smtp_setup.html", "email_template") if err != nil { return nil, err diff --git a/server/mail/templates/change_email_confirmation.html b/server/mail/templates/change_email_confirmation.html index 37aa992260..7c0e5c936b 100644 --- a/server/mail/templates/change_email_confirmation.html +++ b/server/mail/templates/change_email_confirmation.html @@ -162,7 +162,7 @@

- © 2022 Fleet Device Management Inc.
+ © {{.CurrentYear}} Fleet Device Management Inc.
All trademarks, service marks, and company names are the property of their respective owners.

diff --git a/server/mail/templates/invite_token.html b/server/mail/templates/invite_token.html index 724de357ac..2051d6f533 100644 --- a/server/mail/templates/invite_token.html +++ b/server/mail/templates/invite_token.html @@ -192,7 +192,7 @@

- © 2022 Fleet Device Management Inc.
+ © {{.CurrentYear}} Fleet Device Management Inc.
All trademarks, service marks, and company names are the property of their respective owners.

diff --git a/server/mail/templates/password_reset.html b/server/mail/templates/password_reset.html index 735a0c3a22..f8f701df91 100644 --- a/server/mail/templates/password_reset.html +++ b/server/mail/templates/password_reset.html @@ -167,7 +167,7 @@

- © 2022 Fleet Device Management Inc.
+ © {{.CurrentYear}} Fleet Device Management Inc.
All trademarks, service marks, and company names are the property of their respective owners.

diff --git a/server/mail/templates/smtp_setup.html b/server/mail/templates/smtp_setup.html index bea81ded60..ca1abee299 100644 --- a/server/mail/templates/smtp_setup.html +++ b/server/mail/templates/smtp_setup.html @@ -141,7 +141,7 @@

- © 2022 Fleet Device Management Inc.
+ © {{.CurrentYear}} Fleet Device Management Inc.
All trademarks, service marks, and company names are the property of their respective owners.

diff --git a/server/mail/users.go b/server/mail/users.go index b90aa5b48a..1738dadc83 100644 --- a/server/mail/users.go +++ b/server/mail/users.go @@ -3,17 +3,20 @@ package mail import ( "bytes" "html/template" + "time" "github.com/fleetdm/fleet/v4/server" ) type ChangeEmailMailer struct { - BaseURL template.URL - AssetURL template.URL - Token string + BaseURL template.URL + AssetURL template.URL + Token string + CurrentYear int } func (cem *ChangeEmailMailer) Message() ([]byte, error) { + cem.CurrentYear = time.Now().Year() t, err := server.GetTemplate("server/mail/templates/change_email_confirmation.html", "email_template") if err != nil { return nil, err @@ -33,9 +36,12 @@ type PasswordResetMailer struct { AssetURL template.URL // Token password reset token Token string + // Current year for copyright year + CurrentYear int } func (r PasswordResetMailer) Message() ([]byte, error) { + r.CurrentYear = time.Now().Year() t, err := server.GetTemplate("server/mail/templates/password_reset.html", "email_template") if err != nil { return nil, err