-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_notifier.go
More file actions
42 lines (32 loc) · 1.02 KB
/
email_notifier.go
File metadata and controls
42 lines (32 loc) · 1.02 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
package services
import (
"fmt"
"github.com/Jetlum/WalletAlertService/models"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
type EmailNotifier interface {
Send(event *models.Event, userPref *models.UserPreference) error
}
type EmailNotification struct {
APIKey string
client *sendgrid.Client
}
func (en *EmailNotification) Send(event *models.Event, userPref *models.UserPreference) error {
if userPref.UserID == "" {
return fmt.Errorf("invalid user email")
}
from := mail.NewEmail("Wallet Alert Service", "alerts@walletalert.service")
to := mail.NewEmail("", userPref.UserID)
subject := fmt.Sprintf("Alert: %s Event Detected", event.EventType)
content := formatEventMessage(event)
message := mail.NewSingleEmail(from, subject, to, content, content)
response, err := en.client.Send(message)
if err != nil {
return fmt.Errorf("failed to send email: %w", err)
}
if response.StatusCode >= 400 {
return fmt.Errorf("email API error: status %d", response.StatusCode)
}
return nil
}