forked from wrouesnel/emailcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (102 loc) · 3.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"os"
"crypto/tls"
"crypto/x509"
"github.com/jordan-wright/email"
"gopkg.in/alecthomas/kingpin.v2"
"io/ioutil"
"net"
"net/smtp"
)
var (
username = kingpin.Flag("username", "Username to authenticate to the SMTP server with").Envar("EMAIL_USERNAME").String()
password = kingpin.Flag("password", "Password to authenticate to the SMTP server with").Envar("EMAIL_PASSWORD").String()
//usetls = kingpin.Flag("use-tls", "Use TLS to authenticate").Envar("EMAIL_USETLS").Bool()
host = kingpin.Flag("host", "Hostname").Envar("EMAIL_HOST").String()
port = kingpin.Flag("port", "Port number").Envar("EMAIL_PORT").Default("25").Uint16()
tlsHost = kingpin.Flag("tls-host", "Hostname to use for verifying TLS (default to host if blank)").Envar("EMAIL_TLSHOST").String()
attachments = kingpin.Flag("attach", "Files to attach to the email.").Envar("EMAIL_ATTACH").ExistingFiles()
subject = kingpin.Flag("subject", "Subject line of email.").Envar("EMAIL_SUBJECT").String()
body = kingpin.Flag("body", "Body of email. Read from stdin if blank.").Envar("EMAIL_BODY").String()
from = kingpin.Flag("from", "From address for email").Envar("EMAIL_FROM").String()
to = kingpin.Arg("to", "Email recipients").Strings()
timeout = kingpin.Flag("timeout", "Timeout for mail sending").Envar("EMAIL_TIMEOUT").Duration()
poolsize = kingpin.Flag("concurrent-sends", "Max concurrent email send jobs").Envar("EMAIL_CONCURRENT_SENDS").Default("1").Int()
sslInsecure = kingpin.Flag("insecure-skip-verify", "Disable TLS certificate authentication").Envar("EMAIL_INSECURE").Default("false").Bool()
sslCA = kingpin.Flag("cacert", "Specify a custom CA certificate to verify against").Envar("EMAIL_CACERT").String()
)
var Version = "0.0.0-dev"
func main() {
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Version(Version)
kingpin.Parse()
if *timeout == 0 {
*timeout = -1
}
var bodytxt []byte
if *body == "" {
var err error
bodytxt, err = ioutil.ReadAll(os.Stdin)
if err != nil {
println(err)
os.Exit(1)
}
} else {
bodytxt = []byte(*body)
}
err := func() error {
tlsConf := new(tls.Config)
if *tlsHost != "" {
tlsConf.ServerName = *tlsHost
} else {
tlsConf.ServerName = *host
}
tlsConf.InsecureSkipVerify = *sslInsecure
if *sslCA != "" {
certs := x509.NewCertPool()
pemData, err := ioutil.ReadFile(*sslCA)
if err != nil {
println("Error loading custom root CA:", *sslCA)
return err
}
certs.AppendCertsFromPEM(pemData)
tlsConf.RootCAs = certs
}
sendPool, perr := email.NewPool(
net.JoinHostPort(*host, fmt.Sprintf("%v", *port)),
*poolsize,
smtp.PlainAuth("", *username, *password, *host),
tlsConf,
)
if perr != nil {
println("Error creating email pool:", perr.Error())
return perr
}
//defer sendPool.Close()
for _, recipient := range *to {
m := email.NewEmail()
m.From = *from
m.To = []string{recipient}
m.Subject = *subject
m.Text = bodytxt
for _, filename := range *attachments {
_, err := m.AttachFile(filename)
if err != nil {
println(err)
return err
}
}
if err := sendPool.Send(m, *timeout); err != nil {
println("Error sending mail:", recipient, err.Error())
}
}
return nil
}()
if err != nil {
println("Error sending mail:", err.Error())
os.Exit(1)
}
os.Exit(0)
}