From 683a8a5fc11cb2d1440308094790f25a111c8204 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Wed, 13 Jul 2022 19:30:59 +0200 Subject: [PATCH] refactor email send functions --- alert.go | 2 +- mail.go | 43 ++++++++++++++++++++++++++----------------- persistence.go | 14 +++++++------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/alert.go b/alert.go index f983a18..7d982d8 100644 --- a/alert.go +++ b/alert.go @@ -78,7 +78,7 @@ func sendAlert(deveui string, alertMessage string) { if emailalarmactive == "1" { emails := strings.Split(email, ",") for _, em := range emails { - sendEmail(em, alertMessage) + sendEmailMessage(em, alertMessage) } } } diff --git a/mail.go b/mail.go index 9c33a5f..926bac8 100644 --- a/mail.go +++ b/mail.go @@ -1,27 +1,39 @@ package main import ( - "bytes" "log" "net/smtp" ) -func sendEmail(username, message 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("info@wo-bisch.ch") - c.Rcpt(username) - // Send the email body. - wc, err := c.Data() - if err != nil { - log.Fatal(err) - } - defer wc.Close() +} + +func sendEmailMessage(username, message string) { mail_message := "To: " + username + ` +From: info@wo-bisch.ch Subject: ` + message + ` Lieber Benutzer von wo-bisch.ch @@ -30,8 +42,5 @@ Lieber Benutzer von wo-bisch.ch -- 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 b804ccf..60a46b1 100644 --- a/persistence.go +++ b/persistence.go @@ -3,8 +3,8 @@ package main import ( "github.com/gomodule/redigo/redis" "log" - "time" "os" + "time" ) var globalPool *redis.Pool @@ -13,11 +13,11 @@ const alertsentPrefix string = "alertsent:" const devPrefix string = "dev:" 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 } func newPool() *redis.Pool { @@ -29,7 +29,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()) }