refactor email send functions

This commit is contained in:
Joerg Lehmann 2022-07-13 19:22:43 +02:00
parent 667051907b
commit b4814970ce
4 changed files with 39 additions and 62 deletions

View File

@ -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
}

83
mail.go
View File

@ -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)
}

View File

@ -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 {

View File

@ -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) {