mailer/example_test.go

50 lines
1.7 KiB
Go
Raw Permalink Normal View History

2025-01-12 13:03:17 +00:00
package mailer_test
import (
"crypto/sha256"
"fmt"
"git.bivouac.wiki/use/mailer"
)
// UnsubFunc creates a code for the user to manage their email preferences
func UnsubFunc(user string) string {
// you could also just as well store random UUIDs in a KV...
salt := "example_code"
h := sha256.New()
// the important part is not being able to reverse the username from the code
h.Write([]byte(user + salt))
hash := fmt.Sprintf("%x", h.Sum(nil))
return hash
}
func ExampleMailer() {
from := "app@ornithopterhosting.com"
pass := "password1234"
host := "mail.ornithopterhosting.com"
m := mailer.New(from,
pass,
host,
465,
"https://app.ornithopter.me/unsubscribe/",
"If you have problems contact cameron@ornithopterhosting.com",
UnsubFunc,
)
// build methods
notyou := "If this wasn't you, please email us at support@ornithopterhosting.com"
register := "Welcome! Click this link to confirm your account:"
registerbase := "https://app.ornithopter.me/auth/register/"
m.AddKeyedTemplate("Register", "Registration", register, registerbase, notyou)
recoveryexplain := "Someone (probably you) requested a password reset.\nTo finish recovering your account, click here:"
recoverybase := "https://app.ornithopter.me/auth/recover/"
m.AddKeyedTemplate("Recovery", "Password Recovery", recoveryexplain, recoverybase, notyou)
passwordchanged := "Someone (probably you) changed your password.\n" +
"If this wasn't you, please email us at support@ornithopterhosting.com\n"
m.AddTemplate("Changed", "Password Changed", passwordchanged)
// send with methods
m.SendKeyed["Register"]("user@foobar.com", "1234")
m.SendKeyed["Recovery"]("user@foobar.com", "1234")
m.Send["Changed"]("user@foobar.com")
}