-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsendgrid.go
More file actions
46 lines (38 loc) · 1.03 KB
/
sendgrid.go
File metadata and controls
46 lines (38 loc) · 1.03 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
// Package sendgrid adapts sendgrid-go to our Email interface.
package sendgrid
import (
"context"
"fmt"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
auth "github.com/fmitra/authenticator"
)
type service struct {
apiKey string
fromAddr string
fromName string
}
// Email delivers an email to an email address.
func (s *service) Email(ctx context.Context, email, subject, message string) error {
from := mail.NewEmail(s.fromName, s.fromAddr)
to := mail.NewEmail("", email)
msg := mail.NewSingleEmail(from, subject, to, message, message)
client := sendgrid.NewSendClient(s.apiKey)
resp, err := client.Send(msg)
if err != nil {
return fmt.Errorf("sendgrid client failed: %w", err)
}
ok := 202
if resp.StatusCode != ok {
return fmt.Errorf("sendgrid failure received: %s", resp.Body)
}
return nil
}
// NewClient returns a new Sendgrid client
func NewClient(apiKey, fromAddr, fromName string) auth.Emailer {
return &service{
apiKey: apiKey,
fromAddr: fromAddr,
fromName: fromName,
}
}