mailer/assemble.go

56 lines
1.7 KiB
Go
Raw Permalink Normal View History

2025-01-12 13:03:17 +00:00
package mailer
import "fmt"
/*
use AddTemplate and AddKeyedTemplate to make repeatable emails
*/
// Template creates a repeatable template of your subject/body
func (m Mailer) AddTemplate(name string, subject string, body string) {
m.Send[name] = func(user string) {
m.sendNoKey(user, subject, body)
}
}
// TemplateKeyed returns a function that sends keyed emails
func (m Mailer) AddKeyedTemplate(name, subject, explanation, baseurl, notyou string) {
m.SendKeyed[name] = func(username string, code string) {
m.sendKeyed(username, subject, explanation, baseurl, code, notyou)
}
}
// pasting body together
func (m Mailer) sendKeyed(username string, subject string, explanation string, baseurl string, code string, notyou string) {
fmt.Println("send keyed: " + subject + " " + code + " to " + username)
message := explanation + "\n" +
baseurl + code + "\n\n" +
notyou + "\n\n"
msg := m.assemble(username, subject, message)
m.internal_mail(username, msg)
}
func (m Mailer) sendNoKey(username string, subject string, body string) {
message := body + "\n\n"
msg := m.assemble(username, subject, message)
m.internal_mail(username, msg)
}
// asemble adds the unsubscribe codes and footer to an email
func (m Mailer) assemble(to string, subj string, message string) []byte {
code := m.UnsubscribeCodeFn(to)
msg := []byte("To: " + to + "\r\n" +
"From: " + m.From + "\r\n" +
"Subject: " + subj + "\r\n" +
"List-Unsubscribe post: List-Unsubscribe=One click; \r\n" +
"List-Unsubscribe: <" + m.UnsubscribeUrl + code + ">\r\n" +
"\r\n" +
message +
m.Footer +
"If you want to unsubscribe, reply to this email or use this link:\n\n" +
m.UnsubscribeUrl + code + "\n\n" +
"\r\n" + "\r\n",
)
return msg
}