-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
example_test.go
226 lines (177 loc) · 5.64 KB
/
example_test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package example
import (
"testing"
"time"
mail "github.com/xhit/go-simple-mail/v2"
)
// Some variables to connect and the body.
var (
htmlBody = `<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hello Gophers!</title>
</head>
<body>
<p>This is the <b>Go gopher</b>.</p>
<p><img src="cid:Gopher.png" alt="Go gopher" /></p>
<p>Image created by Renee French</p>
</body>
</html>`
host = "localhost"
port = 25
username = "[email protected]"
password = "santiago"
encryptionType = mail.EncryptionNone
connectTimeout = 10 * time.Second
sendTimeout = 10 * time.Second
)
// TestSendMailWithAttachment send a simple html email.
func TestSendMail(t *testing.T) {
client := mail.NewSMTPClient()
//SMTP Client
client.Host = host
client.Port = port
client.Username = username
client.Password = password
client.Encryption = encryptionType
client.ConnectTimeout = connectTimeout
client.SendTimeout = sendTimeout
client.KeepAlive = false
//Connect to client
smtpClient, err := client.Connect()
if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
}
//NOOP command, optional, used for avoid timeout when KeepAlive is true and you aren't sending mails.
//Execute this command each 30 seconds is ideal for persistent connection
err = smtpClient.Noop()
if err != nil {
t.Error("Expected nil, got", err, "noop to client")
}
//Create the email message
email := mail.NewMSG()
email.SetFrom("From Example <[email protected]>").
AddTo("[email protected]").
SetSubject("New Go Email")
email.SetBody(mail.TextHTML, htmlBody)
email.AddAlternative(mail.TextPlain, "Hello Gophers!")
//Some additional options to send
email.SetSender("[email protected]")
email.SetReplyTo("[email protected]")
email.SetReturnPath("[email protected]")
email.AddCc("[email protected]")
email.AddBcc("[email protected]")
//Add inline too!
email.Attach(&mail.File{FilePath: "C:/Users/sdelacruz/Pictures/Gopher.png", Inline: true})
//Attach a file with path
email.Attach(&mail.File{FilePath: "C:/Users/sdelacruz/Pictures/Gopher.png"})
//Attach the file with a base64
email.Attach(&mail.File{B64Data: "Zm9v", Name: "filename"})
//Set a different date in header email
email.SetDate("2015-04-28 10:32:00 CDT")
//Send with low priority
email.SetPriority(mail.PriorityLow)
// always check error after send
if email.Error != nil {
t.Error("Expected nil, got", email.Error, "generating email")
}
//Pass the client to the email message to send it
err = email.Send(smtpClient)
//Get first error
email.GetError()
if err != nil {
t.Error("Expected nil, got", err, "sending email")
}
}
// TestSendMultipleEmails send multiple emails in same connection.
func TestSendMultipleEmails(t *testing.T) {
client := mail.NewSMTPClient()
//SMTP Client
client.Host = host
client.Port = port
client.Username = username
client.Password = password
client.Encryption = encryptionType
client.ConnectTimeout = connectTimeout
client.SendTimeout = sendTimeout
//For authentication you can use AuthPlain, AuthLogin or AuthCRAMMD5
client.Authentication = mail.AuthPlain
//KeepAlive true because the connection need to be open for multiple emails
//For avoid inactivity timeout, every 30 second you can send a NO OPERATION command to smtp client
//use smtpClient.Client.Noop() after 30 second of inactivity in this example
client.KeepAlive = true
//Connect to client
smtpClient, err := client.Connect()
if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
}
for _, to := range toList {
err = sendEmail(htmlBody, to, smtpClient)
if err != nil {
t.Error("Expected nil, got", err, "sending email")
}
}
}
func sendEmail(htmlBody string, to string, smtpClient *mail.SMTPClient) error {
//Create the email message
email := mail.NewMSG()
email.SetFrom("From Example <[email protected]>").
AddTo(to).
SetSubject("New Go Email")
//Get from each mail
email.GetFrom()
email.SetBody(mail.TextHTML, htmlBody)
//Send with high priority
email.SetPriority(mail.PriorityHigh)
// always check error after send
if email.Error != nil {
return email.Error
}
//Pass the client to the email message to send it
return email.Send(smtpClient)
}
// TestWithTLS using gmail port 587.
func TestWithTLS(t *testing.T) {
client := mail.NewSMTPClient()
//SMTP Client
client.Host = "smtp.gmail.com"
client.Port = 587
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionSTARTTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second
//KeepAlive is not settted because by default is false
//Connect to client
smtpClient, err := client.Connect()
if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
}
err = sendEmail(htmlBody, "[email protected]", smtpClient)
if err != nil {
t.Error("Expected nil, got", err, "sending email")
}
}
// TestWithTLS using gmail port 465.
func TestWithSSL(t *testing.T) {
client := mail.NewSMTPClient()
//SMTP Client
client.Host = "smtp.gmail.com"
client.Port = 465
client.Username = "[email protected]"
client.Password = "asdfghh"
client.Encryption = mail.EncryptionSSLTLS
client.ConnectTimeout = 10 * time.Second
client.SendTimeout = 10 * time.Second
//KeepAlive is not settted because by default is false
//Connect to client
smtpClient, err := client.Connect()
if err != nil {
t.Error("Expected nil, got", err, "connecting to client")
}
err = sendEmail(htmlBody, "[email protected]", smtpClient)
if err != nil {
t.Error("Expected nil, got", err, "sending email")
}
}