-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_mail.go
136 lines (108 loc) · 3.13 KB
/
send_mail.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
125
126
127
128
129
130
131
132
133
134
135
136
package main
// Author: Andreas Pfister <[email protected]>
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/smtp"
"os"
"strings"
)
// Mail reprents the actual mail settings.
type Mail struct {
senderID string
toIds []string
subject string
body string
}
// SMTPServer represents the settings of the smtp server
type SMTPServer struct {
host string
port string
}
// ServerName builds the servername
func (s *SMTPServer) ServerName() string {
return s.host + ":" + s.port
}
// BuildMessage builds the message sent through the mail server.
// It sets the sender, receiver, subject and the body of the message.
func (mail *Mail) BuildMessage() string {
message := ""
message += fmt.Sprintf("From: %s\r\n", mail.senderID)
if len(mail.toIds) > 0 {
message += fmt.Sprintf("To: %s\r\n", strings.Join(mail.toIds, ";"))
}
message += fmt.Sprintf("Subject: %s\r\n", mail.subject)
message += "\r\n" + mail.body
return message
}
func main() {
senderIDPtr := flag.String("sender", "", "The sender of mail (Required).")
toIdsPtr := flag.String("receiver", "", "Receiver of the mail (Required). \r\n Multiple receivers separated by semicolon. \r\n Mutiple receivers must be within quotes.")
subjectPtr := flag.String("subject", "", "The subject of the mail")
bodyPtr := flag.String("message", "", "The message of the mail")
authPasswortPtr := flag.String("password", "", "The password to authenticate to the smtp server (Required).")
smtpHostname := flag.String("smtp", "", "The hostname of the smtp server (Required).")
smtpPort := flag.String("port", "", "The port of the smtp server (Required).")
flag.Parse()
if *senderIDPtr == "" ||
*toIdsPtr == "" ||
*authPasswortPtr == "" ||
*smtpHostname == "" ||
*smtpPort == "" {
flag.PrintDefaults()
os.Exit(1)
}
mail := Mail{}
mail.senderID = *senderIDPtr
mail.toIds = strings.Split(*toIdsPtr, ";")
mail.subject = *subjectPtr
mail.body = *bodyPtr
messageBody := mail.BuildMessage()
smtpServer := SMTPServer{host: *smtpHostname, port: *smtpPort}
log.Println(smtpServer.host)
auth := smtp.PlainAuth("", mail.senderID, *authPasswortPtr, smtpServer.host)
// Gmail will reject connection if it's not secure
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: smtpServer.host,
}
conn, err := tls.Dial("tcp", smtpServer.ServerName(), tlsconfig)
if err != nil {
log.Panic(err)
}
client, err := smtp.NewClient(conn, smtpServer.host)
if err != nil {
log.Panic(err)
}
// step 1: Use Auth
if err = client.Auth(auth); err != nil {
log.Panic(err)
}
// step 2: add all from and to
if err = client.Mail(mail.senderID); err != nil {
log.Panic(err)
}
for _, k := range mail.toIds {
if err = client.Rcpt(k); err != nil {
log.Panic(err)
}
}
// Data
w, err := client.Data()
if err != nil {
log.Panic(err)
}
_, err = w.Write([]byte(messageBody))
if err != nil {
log.Panic(err)
}
err = w.Close()
if err != nil {
log.Panic(err)
}
client.Quit()
log.Println("Mail sent successfully")
}