/
emailer.go
45 lines (41 loc) · 1.19 KB
/
emailer.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 service
import (
"context"
"encoding/base64"
"fmt"
"github.com/DarthBenro008/pigil/internal/types"
"github.com/DarthBenro008/pigil/internal/utils"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
"net/http"
)
const mailerTag = "mailer"
func SendEmail(client *http.Client, email string, information types.CommandInformation) {
srv, err := gmail.NewService(context.Background(),
option.WithHTTPClient(client))
if err != nil {
utils.ErrorLogger(err, mailerTag)
}
emailBody, err := utils.GenerateEmailTemplate(information)
if err != nil {
utils.ErrorLogger(err, mailerTag)
}
var message gmail.Message
var status string
if information.WasSuccessful {
status = "passed"
} else {
status = "failed"
}
emailTo := fmt.Sprintf("To: %s\r\n", email)
subject := fmt.Sprintf("Subject: [Pigil Notification]: Process `%s` %s\n",
information.CommandName, status)
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8" +
"\";\n\n"
msg := []byte(emailTo + subject + mime + "\n" + emailBody)
message.Raw = base64.URLEncoding.EncodeToString(msg)
_, err = srv.Users.Messages.Send("me", &message).Do()
if err != nil {
utils.ErrorLogger(err, mailerTag)
}
}