fleet/server/mail/users.go
Eric cd63133770
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
2024-01-15 15:28:49 -06:00

55 lines
1.1 KiB
Go

package mail
import (
"bytes"
"html/template"
"time"
"github.com/fleetdm/fleet/v4/server"
)
type ChangeEmailMailer struct {
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
}
var msg bytes.Buffer
err = t.Execute(&msg, cem)
if err != nil {
return nil, err
}
return msg.Bytes(), nil
}
type PasswordResetMailer struct {
// Base URL to use for Fleet endpoints
BaseURL template.URL
// URL for loading image assets
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
}
var msg bytes.Buffer
if err = t.Execute(&msg, r); err != nil {
return nil, err
}
return msg.Bytes(), nil
}