-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (105 loc) · 3.48 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
package main
import (
"fmt"
"os"
"flag"
"strconv"
"encoding/json"
)
type StringFlags []string
func (i *StringFlags) String() string {
return "my string representation"
}
func (i *StringFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func main() {
var tos StringFlags
var ccs StringFlags
var bccs StringFlags
var attachments StringFlags
httpEnabled := flag.Bool("http", false, "Enable http server")
port := flag.Int("port", 8080, "Start http with port")
endpoint := flag.String("endpoint", "/", "Start http with endpoint")
help := flag.Bool("help", false, "Display this help")
debug := flag.Bool("debug", false, "enable debug (default false)")
provider := flag.String("provider", "", "Set email provider, eg: gmail")
host := flag.String("host", "", "Set email host")
from := flag.String("from", "", "Set Sender email")
subject := flag.String("subject", "", "Set email subject")
body := flag.String("body", "", "Set email body")
username := flag.String("username", "", "Set email username")
password := flag.String("password", "", "Set email password")
plaintext := flag.Bool("plain-text", false, "Send email as plaintext. (default false)")
single := flag.Bool("single", false, "single email per receiver. (default false)")
flag.Var(&tos, "to", "Receiver Email, define multiple to send more than one receiver")
flag.Var(&ccs, "cc", "CC, define multiple to send more than one cc")
flag.Var(&bccs, "bcc", "BCC, define multiple to send more than one bcc")
flag.Var(&attachments, "attachment", "Set email attachments")
flag.Parse()
if *help {
flag.PrintDefaults()
os.Exit(0)
}
if *httpEnabled {
if *port != 8080 {
os.Setenv("PORT", strconv.Itoa(*port))
}
var ep string = *endpoint
var dbg bool = *debug
if os.Getenv("ENDPOINT") != "" {
ep = os.Getenv("ENDPOINT")
}
if os.Getenv("GIN_MODE") != "" {
dbg = os.Getenv("GIN_MODE") != "release"
}
StartServer(ep, dbg)
} else {
if tos == nil || *from == "" {
flag.PrintDefaults()
return
}
var tosStrings = []interface{}{}
var ccsStrings = []interface{}{}
var bccsStrings = []interface{}{}
var attachmentsStrings = []interface{}{}
for _, o := range tos {
tosStrings = append(tosStrings, o)
}
for _, o := range ccs {
ccsStrings = append(ccsStrings, o)
}
for _, o := range bccs {
bccsStrings = append(bccsStrings, o)
}
for _, o := range attachments {
attachmentsStrings = append(attachmentsStrings, o)
}
config := MailConfig{
Provider: *provider,
Host: *host,
Port: *port,
From: *from,
To: tosStrings,
Cc: ccsStrings,
Attachments: attachmentsStrings,
Bcc: bccsStrings,
Subject: *subject,
Body: *body,
Username: *username,
Password: *password,
PlainText: *plaintext,
Single: *single,
SkipAttachmentCheck: true,
Thread: false,
}
if (*debug) {
Normalize(&config)
j, _ := json.MarshalIndent(&config, "", " ")
fmt.Println(string(j))
} else {
SendMail(&config)
}
}
}