From c13dd9addbf89f716e4ef5cfdf1d673139ffcb68 Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sun, 30 Jun 2024 21:41:41 -0400 Subject: Move to Donetick Org, first commit --- internal/email/sender.go | 509 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 509 insertions(+) create mode 100644 internal/email/sender.go (limited to 'internal/email') diff --git a/internal/email/sender.go b/internal/email/sender.go new file mode 100644 index 0000000..540e54a --- /dev/null +++ b/internal/email/sender.go @@ -0,0 +1,509 @@ +package email + +import ( + "context" + "encoding/base64" + "fmt" + "strings" + + "donetick.com/core/config" + gomail "gopkg.in/gomail.v2" +) + +type EmailSender struct { + client *gomail.Dialer + appHost string +} + +func NewEmailSender(conf *config.Config) *EmailSender { + + client := gomail.NewDialer(conf.EmailConfig.Host, conf.EmailConfig.Port, conf.EmailConfig.Email, conf.EmailConfig.Key) + + // format conf.EmailConfig.Host and port : + + // auth := smtp.PlainAuth("", conf.EmailConfig.Email, conf.EmailConfig.Password, host) + return &EmailSender{ + + client: client, + appHost: conf.EmailConfig.AppHost, + } +} + +func (es *EmailSender) SendVerificationEmail(to, code string) error { + // msg := []byte(fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s\r\n", to, subject, body)) + msg := gomail.NewMessage() + msg.SetHeader("From", "no-reply@donetick.com") + msg.SetHeader("To", to) + msg.SetHeader("Subject", "Welcome to Donetick! Verifiy you email") + // text/html for a html email + htmlBody := ` + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + +
 
+ + + +
 
+ + +` + u := es.appHost + "/verify?c=" + encodeEmailAndCode(to, code) + htmlBody = strings.Replace(htmlBody, "{{verifyURL}}", u, 1) + + msg.SetBody("text/html", htmlBody) + + err := es.client.DialAndSend(msg) + if err != nil { + return err + } + return nil + +} + +func (es *EmailSender) SendResetPasswordEmail(c context.Context, to, code string) error { + msg := gomail.NewMessage() + msg.SetHeader("From", "no-reply@donetick.com") + msg.SetHeader("To", to) + msg.SetHeader("Subject", "Donetick! Password Reset") + htmlBody := ` + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + +
 
+ + + +
 
+ + +` + u := es.appHost + "/password/update?c=" + encodeEmailAndCode(to, code) + + // logging.FromContext(c).Infof("Reset password URL: %s", u) + htmlBody = strings.Replace(htmlBody, "{{verifyURL}}", u, 1) + + msg.SetBody("text/html", htmlBody) + + err := es.client.DialAndSend(msg) + if err != nil { + return err + } + return nil + +} + +// func (es *EmailSender) SendFeedbackRequestEmail(to, code string) error { +// // msg := []byte(fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s\r\n", to, subject, body)) +// msg := gomail.NewMessage() + +// } +func encodeEmailAndCode(email, code string) string { + data := email + ":" + code + return base64.StdEncoding.EncodeToString([]byte(data)) +} + +func DecodeEmailAndCode(encoded string) (string, string, error) { + data, err := base64.StdEncoding.DecodeString(encoded) + if err != nil { + return "", "", err + } + parts := string(data) + split := strings.Split(parts, ":") + if len(split) != 2 { + return "", "", fmt.Errorf("Invalid format") + } + return split[0], split[1], nil +} -- cgit