/
email.go
45 lines (39 loc) · 1.17 KB
/
email.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
package internal
import (
"fmt"
"net/http"
"github.com/rs/zerolog"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
func (a *Application) SendEmail(log zerolog.Logger, subject string, to *mail.Email, plainTextContent, htmlContent string) error {
log = log.With().
Str("component", "send_email").
Interface("to", to).
Str("subject", subject).
Logger()
if a.Config.DevMode {
fmt.Printf("=== EMAIL ===\nTo: %s\nSubject: %s\n\n%s\n\n", to, subject, plainTextContent)
return nil
}
from := mail.NewEmail("HSOreCTF Support", "support@hsorectf.tech")
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent)
message.ReplyTo = from
resp, err := a.SendGridClient.Send(message)
if err != nil {
log.Err(err).Msg("failed to send email")
return err
} else if resp.StatusCode != http.StatusAccepted {
log.Error().
Int("status_code", resp.StatusCode).
Any("to", to).
Str("response_body", resp.Body).
Msg("error sending email")
return fmt.Errorf("error sending email (error code %d)", resp.StatusCode)
} else {
log.Info().
Int("status_code", resp.StatusCode).
Any("to", to).
Msg("successfully sent email")
return nil
}
}