-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
89 lines (71 loc) · 2.04 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
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
package mail
import (
"crypto/rand"
"errors"
"fmt"
"net/smtp"
"os"
)
type Options struct {
Host string
Port string
Username string
Password string
}
var ErrEmptyHost = errors.New("host is empty")
var ErrEmptyPort = errors.New("port is empty")
var ErrEmptyUsername = errors.New("username is empty")
var ErrEmptyPassword = errors.New("password is empty")
// Easiest way to get authentication for smtp server
// See: https://golang.org/pkg/net/smtp/#PlainAuth
func (m *Options) plainAuth() (smtp.Auth, error) {
if m.Username == "" {
return nil, fmt.Errorf("mail options: %w", ErrEmptyUsername)
}
if m.Password == "" {
return nil, fmt.Errorf("mail options: %w", ErrEmptyPassword)
}
if m.Host == "" {
return nil, fmt.Errorf("mail options: %w", ErrEmptyHost)
}
auth := smtp.PlainAuth("", m.Username, m.Password, m.Host)
return auth, nil
}
// New is used to create new instance of Message
func New(opt Options) *Message {
return &Message{
Options: opt,
}
}
// Message-ID in header email is consist of uuid and hostname
// Example: <uuid@hostname>
// This header is important for email server to identify email
// Also this header is used to prevent email from being detected as spam
// See: https://tools.ietf.org/html/rfc5322#section-3.6.4
func generateMessageID() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
uuid, err := generateUUID()
if err != nil {
fmt.Println("Error when generate uuid: ", err.Error())
}
messageId := fmt.Sprintf("<%s@%s>", uuid, hostname)
return messageId
}
// This func is generate uuid v4 that compliant with RFC 4122
// See: https://www.rfc-editor.org/rfc/rfc4122
func generateUUID() (string, error) {
uuid := make([]byte, 16)
_, err := rand.Read(uuid)
if err != nil {
return "", err
}
// version 4 (pseudo-random); see section 4.4
uuid[6] = (uuid[6] & 0x0f) | 0x40
// variant bits; see section 4.1.1
uuid[8] = (uuid[8] & 0xbf) | 0x80
result := fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:])
return result, nil
}