diff options
| author | fusion32 <marcopuzziello@gmail.com> | 2025-08-01 19:48:26 -0300 |
|---|---|---|
| committer | fusion32 <marcopuzziello@gmail.com> | 2025-08-01 19:48:26 -0300 |
| commit | d84651a899a6104fce73d95b9c571627c55e5d16 (patch) | |
| tree | bbaf7f7646dec22dc9fdb0f136d5ba5534b1fddf /mail.go | |
| download | web-d84651a899a6104fce73d95b9c571627c55e5d16.tar.gz web-d84651a899a6104fce73d95b9c571627c55e5d16.zip | |
bare bones website
Diffstat (limited to 'mail.go')
| -rw-r--r-- | mail.go | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,44 @@ +package main + +import ( + "fmt" + "net/smtp" + "strings" +) + +var ( + g_SmtpAuth smtp.Auth +) + +func InitMail() bool { + // TODO(fusion): I'm not entirely sure we can safely reuse smtp.Auth across + // many threads. We might need to build a new auth struct everytime we need + // to send an e-mail. + g_SmtpAuth = smtp.PlainAuth("", g_SmtpUser, g_SmtpPassword, g_SmtpHost) + return true +} + +func ExitMail() { + // no-op +} + +func BuildMailMessage(From, To, Subject, Body string) string { + Message := strings.Builder{} + Message.WriteString("MIME-Version: 1.0\r\n") + Message.WriteString("Content-Type: text/html; charset=utf-8\r\n") + if From != "" { + fmt.Fprintf(&Message, "From: %v\r\n", From) + } + if To != "" { + fmt.Fprintf(&Message, "To: %v\r\n", To) + } + fmt.Fprintf(&Message, "Subject: %v\r\n", Subject) + fmt.Fprintf(&Message, "\r\n%v\r\n", Body) + return Message.String() +} + +func SendMail(To, Subject, Body string) error { + Message := BuildMailMessage(g_SmtpSender, To, Subject, Body) + return smtp.SendMail(JoinHostPort(g_SmtpHost, g_SmtpPort), + g_SmtpAuth, g_SmtpSender, []string{To}, []byte(Message)) +} |
