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

View file

@ -5,10 +5,11 @@ package main
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
// "github.com/davecgh/go-spew/spew"
"log"
"strings"
// "log"
// "strconv"
"math/rand"
"net/mail"
@ -48,14 +49,23 @@ func NewEmail() *EmailCtx {
func encodeRFC2047(text string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{text, ""}
addr := mail.Address{Name: text, Address: ""}
return strings.Trim(addr.String(), " \"<@>")
}
func (email *EmailCtx) SetHeaders(config EmailConfig) {
email.Headers["Return-Path"] = 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["Content-Transfer-Encoding"] = "quoted-printable"
email.Headers["MIME-Version"] = "1.0"

View file

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