package main import ( "encoding/base64" "log" "net/smtp" "strings" "time" ) type Email struct { startTime time.Time items []string } type EmailConfig struct { smtpHost string `json:"smtp"` fromEmail string `json:"email_from"` toEmail []string `json:"email_to"` } func SendMail(addr, from, subject, body string, to []string) error { if *debugFlag { log.Printf("SendMail : Start") } r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "") c, err := smtp.Dial(addr) if err != nil { if *debugFlag { log.Printf("SendMail : %s : smtp.Dial (%s)", addr, err) } return err } defer c.Close() if err = c.Mail(r.Replace(from)); err != nil { if *debugFlag { log.Printf("SendMail : %s : client.Mail (%s)", from, err) } return err } for i := range to { to[i] = r.Replace(to[i]) if err = c.Rcpt(to[i]); err != nil { if *debugFlag { log.Printf("SendMail : %s : client.Rcpt (%s)", to[i], err) } return err } } w, err := c.Data() if err != nil { if *debugFlag { log.Printf("SendMail : client.Data (%s)", err) } return err } msg := "To: " + strings.Join(to, ",") + "\r\n" + "From: " + from + "\r\n" + "Subject: " + subject + "\r\n" + "Content-Type: text/html; charset=\"UTF-8\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + base64.StdEncoding.EncodeToString([]byte(body)) if *debugFlag { log.Printf("SendMail :\r\n%s", msg) } _, err = w.Write([]byte(msg)) if err != nil { if *debugFlag { log.Printf("SendMail : writer.Write (%s)", err) } return err } err = w.Close() if err != nil { if *debugFlag { log.Printf("SendMail : writer.Close (%s)", err) } return err } err = c.Quit() if *debugFlag { log.Printf("SendMail : client.Quit (%s)", err) } return err }