-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmtp.go
51 lines (39 loc) · 1.01 KB
/
smtp.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
package smtp
import (
"fmt"
"net/smtp"
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/smtp", new(SMTP))
}
type SMTP struct{}
type options struct {
Subject string `js:"subject"`
Message string `js:"message"`
UDW []string `js:"udw"`
}
func check(e error) {
if e != nil {
fmt.Println(e)
}
}
func plainAuth(host string, password string, sender string) smtp.Auth {
return smtp.PlainAuth("", sender, password, host)
}
func (*SMTP) SendMail(host string, port string, sender string, password string, recipient string, options options) {
emailMessage := "From: " + sender + "\r\n" + "To: " + recipient + "\r\n"
if options.Subject != "" {
emailMessage += "Subject: " + options.Subject + "\r\n\r\n"
}
if options.Message != "" {
emailMessage += options.Message
}
if len(options.UDW) == 0 {
options.UDW = []string{recipient}
}
body := []byte(emailMessage)
auth := plainAuth(host, password, sender)
err := smtp.SendMail(host+":"+port, auth, sender, options.UDW, body)
check(err)
}