Support multiple adresses in EMAIL_TO

This commit is contained in:
Glenn Y. Rolland 2018-09-11 17:17:30 +02:00
parent 5f9fd041af
commit 7bfb907c1f
3 changed files with 23 additions and 6 deletions

View file

@ -3,9 +3,11 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
// "github.com/davecgh/go-spew/spew" // "github.com/davecgh/go-spew/spew"
"log" "log"
"os" "os"
// "reflect"
"strconv" "strconv"
) )
@ -31,7 +33,7 @@ type SmtpConfig struct {
type EmailConfig struct { type EmailConfig struct {
From string From string
To string To []string
Subject string Subject string
} }
type Config struct { type Config struct {
@ -48,7 +50,7 @@ func (config *Config) ParseEnv() (int, error) {
// map env variables to config pointers // map env variables to config pointers
dataMap := map[string](ConfigEntry){ dataMap := map[string](ConfigEntry){
"EMAIL_FROM": ConfigEntry{"string", &(config.Email.From), nil}, "EMAIL_FROM": ConfigEntry{"string", &(config.Email.From), nil},
"EMAIL_TO": ConfigEntry{"string", &(config.Email.To), nil}, "EMAIL_TO": ConfigEntry{"stringlist", &(config.Email.To), nil},
"EMAIL_SUBJECT": ConfigEntry{"string", &(config.Email.Subject), nil}, "EMAIL_SUBJECT": ConfigEntry{"string", &(config.Email.Subject), nil},
"TRELLO_URL": ConfigEntry{"string", &(config.Trello.Url), nil}, "TRELLO_URL": ConfigEntry{"string", &(config.Trello.Url), nil},
"TRELLO_TOKEN": ConfigEntry{"string", &(config.Trello.Token), nil}, "TRELLO_TOKEN": ConfigEntry{"string", &(config.Trello.Token), nil},
@ -96,6 +98,10 @@ func (config *Config) ParseEnv() (int, error) {
case "string": case "string":
*(mapEntry.Ptr.(*string)) = envValue *(mapEntry.Ptr.(*string)) = envValue
case "stringlist":
ptrs := strings.Split(envValue, ",")
mapEntry.Ptr = ptrs
case "uint16": case "uint16":
u64, err := strconv.ParseUint(envValue, 10, 16) u64, err := strconv.ParseUint(envValue, 10, 16)
if err != nil { if err != nil {

View file

@ -5,10 +5,11 @@ package main
import ( import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
// "github.com/davecgh/go-spew/spew" // "github.com/davecgh/go-spew/spew"
"log"
"strings" "strings"
// "log"
// "strconv" // "strconv"
"math/rand" "math/rand"
"net/mail" "net/mail"
@ -48,14 +49,23 @@ func NewEmail() *EmailCtx {
func encodeRFC2047(text string) string { func encodeRFC2047(text string) string {
// use mail's rfc2047 to encode any string // use mail's rfc2047 to encode any string
addr := mail.Address{text, ""} addr := mail.Address{Name: text, Address: ""}
return strings.Trim(addr.String(), " \"<@>") return strings.Trim(addr.String(), " \"<@>")
} }
func (email *EmailCtx) SetHeaders(config EmailConfig) { func (email *EmailCtx) SetHeaders(config EmailConfig) {
email.Headers["Return-Path"] = config.From email.Headers["Return-Path"] = config.From
email.Headers["From"] = config.From email.Headers["From"] = config.From
email.Headers["To"] = config.To fmt.Printf("config.To %#v\n", config.To)
if len(config.To) < 1 {
errmsg := "EMAIL_TO must contain at least one value"
log.Panic(errors.New(errmsg))
}
email.Headers["To"] = config.To[0]
if len(config.To) > 1 {
email.Headers["Cc"] = strings.Join(config.To[1:], ",")
}
email.Headers["Subject"] = encodeRFC2047(config.Subject) email.Headers["Subject"] = encodeRFC2047(config.Subject)
email.Headers["Content-Transfer-Encoding"] = "quoted-printable" email.Headers["Content-Transfer-Encoding"] = "quoted-printable"
email.Headers["MIME-Version"] = "1.0" email.Headers["MIME-Version"] = "1.0"

View file

@ -4,7 +4,8 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/adlio/trello" "github.com/adlio/trello"
"gopkg.in/russross/blackfriday.v2" "github.com/russross/blackfriday"
// "gopkg.in/russross/blackfriday.v2"
"log" "log"
"net/url" "net/url"
"os/exec" "os/exec"