forked from tj/go-mail
/
mail.go
87 lines (71 loc) · 1.68 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
// Package mail provides a small wrapper around AWS SES for sending emails.
package mail
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/ses/sesiface"
)
// client is the default client.
var client = New(ses.New(session.New(aws.NewConfig())))
// Email options.
type Email struct {
// From is the source email.
From string
// To is a set of destination emails.
To []string
// ReplyTo is a set of reply to emails.
ReplyTo []string
// Subject is the email subject text.
Subject string
// Text is the plain text representation of the body.
Text string
// HTML is the HTML representation of the body.
HTML string
}
// Client is the mail client.
type Client struct {
Service sesiface.SESAPI
}
// New returns a new mail client with the given SES service implementation.
func New(s sesiface.SESAPI) *Client {
return &Client{
Service: s,
}
}
// Send an email.
func (c *Client) Send(e Email) error {
if e.HTML == "" {
e.HTML = e.Text
}
msg := &ses.Message{
Subject: &ses.Content{
Charset: aws.String("utf-8"),
Data: &e.Subject,
},
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String("utf-8"),
Data: &e.HTML,
},
Text: &ses.Content{
Charset: aws.String("utf-8"),
Data: &e.Text,
},
},
}
dest := &ses.Destination{
ToAddresses: aws.StringSlice(e.To),
}
_, err := c.Service.SendEmail(&ses.SendEmailInput{
Source: &e.From,
Destination: dest,
Message: msg,
ReplyToAddresses: aws.StringSlice(e.ReplyTo),
})
return err
}
// Send an email.
func Send(e Email) error {
return client.Send(e)
}