-
Notifications
You must be signed in to change notification settings - Fork 1
/
mail.go
64 lines (55 loc) · 1.49 KB
/
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
package main
import (
"bytes"
"net/smtp"
)
// basicMailSend will connect to a remote mail server without authentication and send a message.
func basicMailSend(host string, to string, from string, subject string, body string) error {
var (
message string
buf *bytes.Buffer
uuid = getUUID()
)
// connect to the remote server
client, err := smtp.Dial(host)
if err != nil {
return err
}
defer client.Close()
// set sender and and recipient
client.Mail(from)
client.Rcpt(to)
// send the body
mailContent, err := client.Data()
if err != nil {
return err
}
// close connection once function is complete
defer mailContent.Close()
message = "From: " + from + "\n"
message += "To: " + to + "\n"
message += "Subject: " + subject + "\n"
message += "X-Mailer: GoLang net/smtp\n"
message += "MIME-Version: 1.0\n"
message += "Content-Type: multipart/alternative; boundary=\"" + uuid + "\"\n"
message += "--" + uuid + "\n"
message += "Content-Type: text/plain; charset=\"UTF-8\"\n"
message += "\n"
message += "Please view this message in a client capable of displaying HTML\n"
message += "encoded messages.\n"
message += "\n"
message += "--" + uuid + "\n"
message += "Content-Type: text/html; charset=\"UTF-8\"\n"
message += "\n"
message += body + "\n"
message += "\n"
message += "--" + uuid + "--\n"
message += "\n"
// set up connection
buf = bytes.NewBufferString(message)
// send message
if _, err = buf.WriteTo(mailContent); err != nil {
return err
}
return nil
}