-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.go
55 lines (50 loc) · 1.27 KB
/
util.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
package sendmail
import (
"bytes"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"log"
"net/mail"
"strings"
)
func generateMessageID(domain string) string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
log.Fatal(err)
}
msgID := fmt.Sprintf("<%s@%s>", strings.TrimRight(base64.URLEncoding.EncodeToString(b), "="), domain)
return msgID
}
// GetDumbMessage create simple mail.Message from raw data
func GetDumbMessage(sender string, recipients []string, body []byte) (*mail.Message, error) {
if len(recipients) == 0 {
return nil, errors.New("empty recipients list")
}
buf := bytes.NewBuffer(nil)
if sender != "" {
buf.WriteString("From: " + sender + "\r\n")
}
buf.WriteString("To: " + strings.Join(recipients, ",") + "\r\n")
buf.WriteString("\r\n")
buf.Write(body)
buf.WriteString("\r\n")
return mail.ReadMessage(buf)
}
// AddressListToSlice convert mail.Address list to slice of strings
func AddressListToSlice(list []*mail.Address) (slice []string) {
for _, rcpt := range list {
slice = append(slice, rcpt.Address)
}
return
}
// GetDomainFromAddress extract domain from email address
func GetDomainFromAddress(address string) string {
components := strings.Split(address, "@")
if len(components) == 2 {
return components[1]
}
return ""
}