-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSender.go
50 lines (47 loc) · 1.06 KB
/
Sender.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
package gosmtp
import (
"errors"
"fmt"
)
// Sender - smtp client structure
type Sender struct {
Login, Email, Password, ServerSMTP string
}
// SendMessage - отправить письмо
func (s *Sender) SendMessage(message *Message) error {
if message == nil {
return errors.New("message is nil")
}
client, err := s.connect()
if err != nil {
return err
}
defer client.Quit()
message.SetFrom(s.Email)
if err := client.Mail(message.from); err != nil {
return fmt.Errorf("%s %q", err, message.from)
}
for i := range message.to {
if err := client.Rcpt(message.to[i]); err != nil {
return fmt.Errorf("%s %q", err, message.to[i])
}
}
for i := range message.cc {
if err := client.Rcpt(message.cc[i]); err != nil {
return fmt.Errorf("%s %q", err, message.cc[i])
}
}
for i := range message.bcc {
if err := client.Rcpt(message.bcc[i]); err != nil {
return fmt.Errorf("%s %q", err, message.bcc[i])
}
}
w, err := client.Data()
if err != nil {
return err
}
if _, err := message.WriteTo(w); err != nil {
return err
}
return w.Close()
}