-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_SMTP.go
118 lines (99 loc) · 2.47 KB
/
mod_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
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
package main
import (
"log"
mail "github.com/xhit/go-simple-mail"
)
// SendMails - send emails
//Authentication type: 1 = AuthPlain, 2 = AuthLogin, 3 = AuthCRAMMD5
//Encryption type: 1 = EncryptionNone, 2 = EncryptionSSL, 3 = EncryptionTLS
func SendMails(host string, port int, encryptionType int, user string, password string, from string, to []string, subject string, body string, attach string) (err error) {
server := mail.NewSMTPClient()
server.Host = host
server.Port = port
server.Username = user
server.Password = password
server.KeepAlive = false
/*
switch authenticationType {
case 1:
server.Authentication = mail.AuthPlain
case 2:
server.Authentication = mail.AuthLogin
case 3:
server.Authentication = mail.AuthCRAMMD5
}
*/
switch encryptionType {
case 1:
server.Encryption = mail.EncryptionNone
case 2:
server.Encryption = mail.EncryptionSSL
case 3:
server.Encryption = mail.EncryptionTLS
}
smtpClient, err := server.Connect()
if err != nil {
log.Println(err)
return err
}
email := mail.NewMSG()
email.SetFrom(from)
email.SetSubject(subject)
email.SetBody(mail.TextHTML, body)
for _, v := range to {
email.AddTo(v)
}
err = email.Send(smtpClient)
if err != nil {
log.Println(err)
return err
}
log.Println("Email Sent")
return nil
}
// SendMail - send email
//Authentication type: 1 = AuthPlain, 2 = AuthLogin, 3 = AuthCRAMMD5
//Encryption type: 1 = EncryptionNone, 2 = EncryptionSSL, 3 = EncryptionTLS
func SendMail(host string, port int, encryptionType int, user string, password string, from string, to string, subject string, body string, attach string) (err error) {
server := mail.NewSMTPClient()
server.Host = host
server.Port = port
server.Username = user
server.Password = password
server.KeepAlive = false
/*
switch authenticationType {
case 1:
server.Authentication = mail.AuthPlain
case 2:
server.Authentication = mail.AuthLogin
case 3:
server.Authentication = mail.AuthCRAMMD5
}
*/
switch encryptionType {
case 1:
server.Encryption = mail.EncryptionNone
case 2:
server.Encryption = mail.EncryptionSSL
case 3:
server.Encryption = mail.EncryptionTLS
}
smtpClient, err := server.Connect()
if err != nil {
log.Fatal(err)
return err
}
email := mail.NewMSG()
email.SetFrom(from)
email.AddTo(to)
email.SetSubject(subject)
email.SetBody(mail.TextHTML, body)
err = email.Send(smtpClient)
if err != nil {
log.Println(err)
return err
}
log.Println("Email Sent")
return nil
}