-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathmailgun.go
51 lines (41 loc) · 1.4 KB
/
mailgun.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
package mailgun
import (
"context"
"fmt"
"github.com/mailgun/mailgun-go/v4"
)
// Mailgun struct holds necessary data to communicate with the Mailgun API.
type Mailgun struct {
client mailgun.Mailgun
senderAddress string
receiverAddresses []string
}
// New returns a new instance of a Mailgun notification service.
// You will need a Mailgun API key and domain name.
// See https://documentation.mailgun.com/en/latest/
func New(domain, apiKey, senderAddress string, opts ...Option) *Mailgun {
m := &Mailgun{
client: mailgun.NewMailgun(domain, apiKey),
senderAddress: senderAddress,
receiverAddresses: []string{},
}
for _, opt := range opts {
opt(m)
}
return m
}
// AddReceivers takes email addresses and adds them to the internal address list. The Send method will send
// a given message to all those addresses.
func (m *Mailgun) AddReceivers(addresses ...string) {
m.receiverAddresses = append(m.receiverAddresses, addresses...)
}
// Send takes a message subject and a message body and sends them to all previously set chats. Message body supports
// html as markup language.
func (m Mailgun) Send(ctx context.Context, subject, message string) error {
mailMessage := mailgun.NewMessage(m.senderAddress, subject, message, m.receiverAddresses...)
_, _, err := m.client.Send(ctx, mailMessage)
if err != nil {
return fmt.Errorf("send message: %w", err)
}
return nil
}