-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailer.go
More file actions
151 lines (140 loc) · 3.27 KB
/
emailer.go
File metadata and controls
151 lines (140 loc) · 3.27 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package emailer
import (
"bytes"
"crypto/tls"
"fmt"
"log"
"net/smtp"
"strings"
"sync"
)
// A Client represent a connection to smtp server with identity
type Client struct {
// Name of the client
name string
username string
password string
// smtp sever without port
host string
// smtp sever address with port
addr string
from string
sender string
smtp *smtp.Client
mu *sync.Mutex
}
type Options struct {
Host string // smtp host
Port string // smtp port
User string // smtp username
Pass string // smtp password
Name string // sender name for the client
From string // sender email for the client
}
// NewClient create a emailer client for sending emails using provided
// smtp sevice provider
func NewClient(o Options) (*Client, error) {
var mu sync.Mutex
c := &Client{
name: o.Name,
username: o.User,
password: o.Pass,
host: o.Host,
addr: fmt.Sprintf("%s:%s", o.Host, o.Port),
from: o.From,
sender: fmt.Sprintf("%s <%s>", o.Name, o.From),
mu: &mu,
}
err := c.connnect()
if err != nil {
return nil, err
}
return c, nil
}
func (mail *Mail) SendWith(c *Client) {
c.sendMail(mail)
}
// SendMail sends the mail
func (c *Client) SendMail(mail *Mail) error {
return c.sendMail(mail)
}
type Mail struct {
sender string
To []string
Cc []string
Bcc []string
Subject string
Body bytes.Buffer
}
func (mail *Mail) BuildMail() string {
msg := strings.Builder{}
msg.WriteString("MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n")
msg.WriteString(fmt.Sprintf("From: %s\r\n", mail.sender))
msg.WriteString(fmt.Sprintf("To: %s\r\n", strings.Join(mail.To, ";")))
msg.WriteString(fmt.Sprintf("Cc: %s\r\n", strings.Join(mail.Cc, ";")))
msg.WriteString(fmt.Sprintf("Bcc: %s\r\n", strings.Join(mail.Bcc, ";")))
msg.WriteString(fmt.Sprintf("Subject: %s\r\n", mail.Subject))
msg.WriteString(fmt.Sprintf("\r\n%s\r\n", mail.Body.String()))
return msg.String()
}
func (c *Client) sendMail(mail *Mail) error {
c.mu.Lock()
defer c.mu.Unlock()
mail.sender = c.sender
err := c.smtp.Noop()
if err != nil {
err = c.connnect()
if err != nil {
log.Fatal("Noop Reconnectiong Failed", err)
return err
}
}
err = c.smtp.Mail(c.from)
if err != nil {
log.Fatal("Error creating mail from", err)
}
for _, v := range mail.To {
c.smtp.Rcpt(v)
}
for _, v := range mail.Cc {
c.smtp.Rcpt(v)
}
msg := mail.BuildMail()
w, err := c.smtp.Data()
if err != nil {
log.Printf("[Failed] \"%s\" to %s\n. %s\n", mail.Subject, mail.To, err.Error())
c.smtp.Reset()
return err
}
_, err = w.Write([]byte(msg))
if err != nil {
log.Println("Error writing mail bytes", err)
return err
}
err = w.Close()
if err != nil {
log.Println("Error closing mail writer", err)
return err
}
fmt.Printf("[SENT] \"%s\" to %s\n", mail.Subject, mail.To)
return nil
}
func (c *Client) connnect() (err error) {
if c.smtp != nil {
c.smtp.Close()
}
c.smtp, err = smtp.Dial(c.addr)
if err != nil {
return err
}
var auth smtp.Auth
err = c.smtp.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err == nil {
auth = smtp.PlainAuth("", c.username, c.password, c.host)
} else {
// server doesn't support tls, use cram-mds
auth = smtp.CRAMMD5Auth(c.username, c.password)
}
err = c.smtp.Auth(auth)
return
}