-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemailer.go
119 lines (102 loc) · 3.12 KB
/
emailer.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
119
// Copyright 2020-2024 NGR Softlab
package emailer
import (
"bytes"
"encoding/base64"
"fmt"
"mime/multipart"
"net/smtp"
"strings"
"time"
)
/////////////////////////////////////////////
// Sender struct - sender for sending smtp packs
type Sender struct {
Login string // user login
Email string // user email address
Password string // user password
ServerSMTP string // smtp server string
client *smtp.Client // smtp client pointer
message []byte // message text
to []string // receivers of email
}
// NewSender creating new *Sender obj
func NewSender(login, password, email, server string) *Sender {
auth := Sender{
Login: login,
Email: email,
Password: password,
ServerSMTP: server}
return &auth
}
/////////////////////////////////////////////
// Send send smtp pack (mail) with login auth
func (s *Sender) Send() error {
err := smtp.SendMail(s.ServerSMTP,
LoginAuth(s.Login, s.Password),
s.Email, s.to, s.message)
if err != nil {
logger.Errorf("send error: %s", err.Error())
return err
}
return nil
}
// SendWithAuth send smtp pack (mail) with custom auth
func (s *Sender) SendWithAuth(auth smtp.Auth) error {
err := smtp.SendMail(s.ServerSMTP,
auth,
s.Email, s.to, s.message)
if err != nil {
logger.Errorf("send error: %s", err.Error())
return err
}
return nil
}
/////////////////////////////////////////////
// NewMessage creating new email message
func (s *Sender) NewMessage(params *MessageParams) error {
logger.Infof("files: %d", len(params.Files))
attachments, err := attachFile(params.Files)
if err != nil {
logger.Error(err)
return err
}
withAttachments := len(attachments) > 0
var headers = make(map[string]string)
headers["From"] = s.Email
headers["To"] = strings.Join(params.Recipients, ";")
headers["Subject"] = params.Topic
headers["MIME-Version"] = "1.0"
headers["Date"] = time.Now().Format(time.RFC1123Z)
var buf = bytes.NewBuffer(nil)
writer := multipart.NewWriter(buf)
boundary := writer.Boundary()
for k, v := range headers {
buf.WriteString(fmt.Sprintf("%s: %s\r\n", k, v))
}
if withAttachments {
buf.WriteString(fmt.Sprintf(`Content-Type: %s; boundary="%s"`, MultipartMixedContentType, boundary))
buf.WriteString("\r\n\r\n")
buf.WriteString(fmt.Sprintf("--%s\r\n", boundary))
}
buf.WriteString(fmt.Sprintf("Content-Type: %s; charset=%s\r\n", params.ContentType, params.Charset))
buf.WriteString("MIME-Version: 1.0\r\n")
buf.WriteString("\r\n" + params.Body)
if withAttachments {
for k, v := range attachments {
buf.WriteString(fmt.Sprintf("\r\n--%s\r\n", boundary))
buf.WriteString(fmt.Sprintf("Content-Type: %s\r\n", VndSheetContentType))
buf.WriteString(fmt.Sprintf("Content-Transfer-Encoding: %s\r\n", Base64Charset))
buf.WriteString("MIME-Version: 1.0\r\n")
buf.WriteString(fmt.Sprintf(`Content-Disposition: attachment; filename="%s"`, k))
buf.WriteString("\r\n\r\n")
var b = make([]byte, base64.StdEncoding.EncodedLen(len(v)))
base64.StdEncoding.Encode(b, v)
buf.Write(b)
}
buf.WriteString("--")
}
s.to = params.Recipients
s.message = buf.Bytes()
return nil
}