-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickmailer.go
91 lines (74 loc) · 2.17 KB
/
quickmailer.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
package main
import (
"flag"
"log"
"net/smtp"
"net/textproto"
"path/filepath"
"github.com/jordan-wright/email"
)
type emailParams struct {
server string
sendserver string
port string
passwd string
from string
fromname string
to string
subject string
body string
attachFile string
attachDir string
}
func main() {
settings := getArgs()
fromline := "<" + settings.from + ">"
if settings.fromname != "" {
fromline = settings.fromname + " " + fromline
}
e := &email.Email{
To: []string{settings.to},
From: fromline,
Subject: settings.subject,
Text: []byte(settings.body),
Headers: textproto.MIMEHeader{},
}
if settings.attachFile != "" {
_, err := e.AttachFile(settings.attachFile)
if err != nil {
log.Fatal(err)
}
}
if settings.attachDir != "" {
files, _ := filepath.Glob(settings.attachDir + "/*")
for _, elem := range files {
_, err := e.AttachFile(elem)
if err != nil {
log.Fatal(err)
}
}
}
err := e.Send(settings.sendserver, smtp.PlainAuth("", settings.from, settings.passwd, settings.server))
if err != nil {
log.Fatal(err)
}
}
func getArgs() emailParams {
var params emailParams
flag.StringVar(¶ms.server, "server", "", "smtp server without port to use")
flag.StringVar(¶ms.port, "port", "587", "smtp port to use")
flag.StringVar(¶ms.passwd, "passwd", "", "password for smtp auth")
flag.StringVar(¶ms.from, "from", "", "email to send from")
flag.StringVar(¶ms.to, "to", "", "email to send to")
flag.StringVar(¶ms.subject, "subject", "Your Files", "subject line")
flag.StringVar(¶ms.body, "body", "See attached", "some body text")
flag.StringVar(¶ms.attachFile, "attach", "", "single file to attach")
flag.StringVar(¶ms.attachDir, "attachDir", "", "directory to attach files from")
flag.StringVar(¶ms.fromname, "fromName", "", "name to send email as")
flag.Parse()
if (params.server == "") || (params.port == "") || (params.passwd == "") || (params.from == "") || (params.to == "") {
log.Fatal("Required flags: -server, -passwd, -port, -from, -to")
}
params.sendserver = params.server + ":" + params.port
return params
}