-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
84 lines (65 loc) · 1.71 KB
/
client.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
package mail
import (
"errors"
"fmt"
"github.com/sendgrid/rest"
sg "github.com/sendgrid/sendgrid-go"
sgMail "github.com/sendgrid/sendgrid-go/helpers/mail"
)
var sgClient *sg.Client
type MailClient struct {
Key string
client *sg.Client
VerifedReplySender string
VerifedReplySenderAddress string
}
type Mail struct {
From string
FromAddress string
To string
ToAddress string
Subject string
Message string
}
type MailReply struct {
From *sgMail.Email
To *sgMail.Email
}
type MailResponse struct {
ClientResponse *rest.Response
Mail *MailReply
}
func NewMailClient(client *MailClient) *MailClient {
sgClient = sg.NewSendClient(client.Key)
return &MailClient{
Key: client.Key,
client: sgClient,
}
}
func (mc *MailClient) NewMail(mail *Mail) (*MailResponse, error) {
from := sgMail.NewEmail(mail.From, mail.FromAddress)
to := sgMail.NewEmail(mail.To, mail.ToAddress)
message := sgMail.NewSingleEmail(from, mail.Subject, to, mail.Message, "")
resp, err := mc.client.Send(message)
fmt.Print("Sent the message")
if err != nil {
return nil, errors.New("failed to send message")
}
verifiedReplyEmail := sgMail.NewEmail(mc.VerifedReplySender, mc.VerifedReplySenderAddress)
response := &MailResponse{
ClientResponse: resp,
Mail: &MailReply{
From: verifiedReplyEmail,
To: from,
},
}
return response, nil
}
func (mr *MailResponse) Reply(subject string, replyMessage string) (*rest.Response, error) {
message := sgMail.NewSingleEmail(mr.Mail.From, subject, mr.Mail.To, replyMessage, "")
res, err := sgClient.Send(message)
if err != nil {
return nil, errors.New("failed to reply")
}
return res, nil
}