-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (51 loc) · 1.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"io/ioutil"
"log"
"net/smtp"
)
const PROGRAM_NAME = "gomail"
var (
settingsFile = flag.String("s", GetSettingsFile(), "settings file")
recipientsFile = flag.String("r", GetRecepientsFile(), "recipients file")
mailBodyFile = flag.String("b", "./mail.txt", "mail body file")
)
type Settings struct {
Addr string `yaml:"Addr"`
From string `yaml:"From"`
Host string `yaml:"Host"`
Password string `yaml:"Password"`
Subject string `yaml:"Subject"`
Username string `yaml:"Username"`
}
func BuildMessage(from string, to string, subject string, body []byte) []byte {
msg := append([]byte("MIME-version: 1.0;\r\n"+
"Content-Type: text/plain; charset=utf-8;\r\n"+
"From: "+from+"\r\n"+
"To: "+to+"\r\n"+
"Subject: "+subject+"\r\n\r\n"), body...)
return msg
}
func main() {
flag.Parse()
settings := ReadSettingsFromFile(*settingsFile)
recipients := ReadRecipientsAddressesFromFile(*recipientsFile)
log.Printf("Reading mail content from %s...\n", *mailBodyFile)
mailContent, err := ioutil.ReadFile(*mailBodyFile)
if err != nil {
log.Fatalf("Error reading %s\n", *mailBodyFile)
}
log.Printf("Performing login on %s...\n", settings.Host)
// PlainAuth is using TLS if not on localhost
auth := smtp.PlainAuth("", settings.Username, settings.Password, settings.Host)
for _, to := range recipients {
msg := BuildMessage(settings.From, to, settings.Subject, mailContent)
err := smtp.SendMail(settings.Addr, auth, settings.From, []string{to}, msg)
if err != nil {
log.Fatalf("Cannot send mail: %s\n", err.Error())
} else {
log.Printf("Email sent to %s\n", to)
}
}
}