From b4814970cee2c1a98c998db6a26d5b1383eef4ee Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Wed, 13 Jul 2022 19:22:43 +0200 Subject: [PATCH] refactor email send functions --- helper.go | 10 +++--- mail.go | 83 ++++++++++++++++++-------------------------------- persistence.go | 4 +-- tracker.go | 4 +-- 4 files changed, 39 insertions(+), 62 deletions(-) diff --git a/helper.go b/helper.go index dc1ce7d..4bbec7b 100644 --- a/helper.go +++ b/helper.go @@ -21,9 +21,9 @@ func Contains(a []string, x string) bool { } func getenv(key, fallback string) string { - value := os.Getenv(key) - if len(value) == 0 { - return fallback - } - return value + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value } diff --git a/mail.go b/mail.go index dd6f64d..b0bd9e4 100644 --- a/mail.go +++ b/mail.go @@ -1,27 +1,39 @@ package main import ( - "bytes" "log" "net/smtp" ) -func sendEmail(username, confirm_id string) { - c, err := smtp.Dial("127.0.0.1:25") +func sendEmail(mail_to, mail_default_authuser, mail_message string) { + var auth smtp.Auth + if getenv("MAILSERVER_USER", "") != "" { + // Set up authentication information. + auth = smtp.PlainAuth( + "", + getenv("MAILSERVER_USER", ""), + getenv("MAILSERVER_PASSWORD", ""), + getenv("MAILSERVER_HOST", "127.0.0.1"), + ) + } + // Connect to the server, authenticate, set the sender and recipient, + // and send the email all in one step. + err := smtp.SendMail( + getenv("MAILSERVER_HOST", "127.0.0.1")+":"+getenv("MAILSERVER_PORT", "25"), + auth, + getenv("MAILSERVER_USER", mail_default_authuser), + []string{mail_to}, + []byte(mail_message), + ) + if err != nil { log.Fatal(err) } - defer c.Close() - // Set the sender and recipient. - c.Mail("register@wo-bisch.ch") - c.Rcpt(username) - // Send the email body. - wc, err := c.Data() - if err != nil { - log.Fatal(err) - } - defer wc.Close() +} + +func sendEmailConfirm(username, confirm_id string) { mail_message := "To: " + username + ` +From: register@wo-bisch.ch Subject: Passwortaenderung auf https://wo-bisch.ch, bitte bestaetigen Lieber Benutzer von wo-bisch.ch @@ -37,28 +49,12 @@ Mit freundlichen Grüssen -- wo-bisch.ch` - buf := bytes.NewBufferString(mail_message) - if _, err = buf.WriteTo(wc); err != nil { - log.Fatal(err) - } + sendEmail(username, "mail@wo-bisch.ch", mail_message) } func sendPaymentConfirmationEmail(username, charge_data string, amount int64) { - c, err := smtp.Dial("127.0.0.1:25") - if err != nil { - log.Fatal(err) - } - defer c.Close() - // Set the sender and recipient. - c.Mail("info@wo-bisch.ch") - c.Rcpt(username) - // Send the email body. - wc, err := c.Data() - if err != nil { - log.Fatal(err) - } - defer wc.Close() mail_message := "To: " + username + ` +From: info@wo-bisch.ch Subject: Zahlungsbestaetigung wo-bisch.ch Lieber Benutzer von wo-bisch.ch @@ -71,28 +67,12 @@ Mit freundlichen Grüssen -- wo-bisch.ch` - buf := bytes.NewBufferString(mail_message) - if _, err = buf.WriteTo(wc); err != nil { - log.Fatal(err) - } + sendEmail(username, "mail@wo-bisch.ch", mail_message) } func sendOrderEmail(username, body string) { - c, err := smtp.Dial("127.0.0.1:25") - if err != nil { - log.Fatal(err) - } - defer c.Close() - // Set the sender and recipient. - c.Mail("info@wo-bisch.ch") - c.Rcpt(username) - // Send the email body. - wc, err := c.Data() - if err != nil { - log.Fatal(err) - } - defer wc.Close() mail_message := "To: " + username + ` +From: info@wo-bisch.ch Subject: Bestelleingang auf https://wo-bisch.ch Lieber Administrator @@ -105,8 +85,5 @@ Mit freundlichen Grüssen -- wo-bisch.ch` - buf := bytes.NewBufferString(mail_message) - if _, err = buf.WriteTo(wc); err != nil { - log.Fatal(err) - } + sendEmail(username, "mail@wo-bisch.ch", mail_message) } diff --git a/persistence.go b/persistence.go index 1bc8550..87b62bc 100644 --- a/persistence.go +++ b/persistence.go @@ -26,7 +26,7 @@ func newPool() *redis.Pool { // Dial is an application supplied function for creating and // configuring a connection. Dial: func() (redis.Conn, error) { - c, err := redis.Dial("tcp", getenv("REDIS_CONNECTION_STRING",":6379")) + c, err := redis.Dial("tcp", getenv("REDIS_CONNECTION_STRING", ":6379")) if err != nil { panic(err.Error()) } @@ -452,7 +452,7 @@ func updateUser(username, password string) { return } - sendEmail(username, confirm_id) + sendEmailConfirm(username, confirm_id) } func checkLoginCredentials(username, password string) bool { diff --git a/tracker.go b/tracker.go index e76ce3d..7b5c607 100644 --- a/tracker.go +++ b/tracker.go @@ -5,7 +5,7 @@ import ( "log" "net/http" "regexp" - "strings" + "strings" ) // tracker handler @@ -125,7 +125,7 @@ func save_tracker_settingsHandler(response http.ResponseWriter, request *http.Re fmt.Fprintf(response, "{ \"rc\": 16, \"msg\": \"greenzone must be specified in URL\" }") return } - mygreenzone := strings.ReplaceAll(greenzone[0]," ","") + mygreenzone := strings.ReplaceAll(greenzone[0], " ", "") match3, _ := regexp.MatchString(`^$|^[0-9]+\.[0-9]+,[0-9]+\.[0-9]+,[0-9]+\.[0-9]+,[0-9]+\.[0-9]+$`, mygreenzone) if !(match3) {