2022-06-17 14:54:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2022-06-19 06:12:00 +02:00
|
|
|
"log"
|
2022-06-17 14:54:14 +02:00
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Email struct {
|
|
|
|
startTime time.Time
|
|
|
|
items []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmailConfig struct {
|
2022-06-19 06:33:24 +02:00
|
|
|
SmtpHost string `json:"smtp"`
|
|
|
|
FromEmail string `json:"email_from"`
|
|
|
|
ToEmail []string `json:"email_to"`
|
2022-06-17 14:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func SendMail(addr, from, subject, body string, to []string) error {
|
2022-06-19 06:13:52 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail : Start")
|
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
|
|
|
|
|
|
|
|
c, err := smtp.Dial(addr)
|
|
|
|
if err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
2022-06-19 06:20:27 +02:00
|
|
|
log.Printf("SendMail : %s : smtp.Dial (%s)", addr, err)
|
2022-06-19 06:18:34 +02:00
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-06-19 06:18:34 +02:00
|
|
|
|
2022-06-17 14:54:14 +02:00
|
|
|
defer c.Close()
|
2022-06-19 06:18:34 +02:00
|
|
|
|
2022-06-17 14:54:14 +02:00
|
|
|
if err = c.Mail(r.Replace(from)); err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
2022-06-19 06:20:27 +02:00
|
|
|
log.Printf("SendMail : %s : client.Mail (%s)", from, err)
|
2022-06-19 06:18:34 +02:00
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-06-19 06:18:34 +02:00
|
|
|
|
2022-06-17 14:54:14 +02:00
|
|
|
for i := range to {
|
|
|
|
to[i] = r.Replace(to[i])
|
|
|
|
if err = c.Rcpt(to[i]); err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
2022-06-19 06:20:27 +02:00
|
|
|
log.Printf("SendMail : %s : client.Rcpt (%s)", to[i], err)
|
2022-06-19 06:18:34 +02:00
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := c.Data()
|
|
|
|
if err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail : client.Data (%s)", err)
|
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-08 05:15:09 +02:00
|
|
|
msg := "Date: " + time.Now().Format("Mon, 02 Jan 2006 15:04:05 -0700") + "\r\n" +
|
2022-10-08 04:49:28 +02:00
|
|
|
"To: " + strings.Join(to, ",") + "\r\n" +
|
2022-06-17 14:54:14 +02:00
|
|
|
"From: " + from + "\r\n" +
|
|
|
|
"Subject: " + subject + "\r\n" +
|
2022-10-08 05:15:09 +02:00
|
|
|
"Content-Type: text/plain; charset=\"UTF-8\"\r\n" +
|
2022-06-17 14:54:14 +02:00
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
|
|
|
|
|
2022-06-19 06:12:00 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail :\r\n%s", msg)
|
|
|
|
}
|
|
|
|
|
2022-06-17 14:54:14 +02:00
|
|
|
_, err = w.Write([]byte(msg))
|
|
|
|
if err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail : writer.Write (%s)", err)
|
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-06-19 06:18:34 +02:00
|
|
|
|
2022-06-17 14:54:14 +02:00
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
2022-06-19 06:18:34 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail : writer.Close (%s)", err)
|
|
|
|
}
|
2022-06-17 14:54:14 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-06-19 06:18:34 +02:00
|
|
|
|
|
|
|
err = c.Quit()
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SendMail : client.Quit (%s)", err)
|
|
|
|
}
|
|
|
|
return err
|
2022-06-17 14:54:14 +02:00
|
|
|
}
|