-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
postmark.go
110 lines (89 loc) · 2.74 KB
/
postmark.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gomail
import (
"bufio"
"context"
"encoding/base64"
"fmt"
"io"
"log"
"strings"
"github.com/mrz1836/postmark"
)
// postmarkInterface is an interface for Postmark/mocking
type postmarkInterface interface {
SendEmail(ctx context.Context, email postmark.Email) (postmark.EmailResponse, error)
}
// sendViaPostmark sends an email using the Postmark service
func sendViaPostmark(ctx context.Context, client postmarkInterface, email *Email) (err error) {
// Create the email struct
postmarkEmail := postmark.Email{
From: email.FromAddress,
HTMLBody: email.HTMLContent,
ReplyTo: email.ReplyToAddress,
Subject: email.Subject,
TextBody: email.PlainTextContent,
TrackOpens: email.TrackOpens,
TrackLinks: "None",
}
// Set the link tracking
if email.TrackClicks {
postmarkEmail.TrackLinks = "HtmlAndText"
}
// Warn about features that are set but not available
if email.AutoText {
log.Printf("warning: auto text is enabled, but Postmark does not offer this feature")
}
// Set the "from" name if given
if len(email.FromName) > 0 {
postmarkEmail.From = email.FromName + " " + email.FromAddress
}
// Convert recipients to comma separated
postmarkEmail.To = strings.Join(email.Recipients, ",")
// Convert tags to comma separated
postmarkEmail.Tag = strings.Join(email.Tags, ",")
// CC addresses
if len(email.RecipientsCc) > 0 {
postmarkEmail.Cc = strings.Join(email.RecipientsCc, ",")
}
// BCC addresses
if len(email.RecipientsBcc) > 0 {
postmarkEmail.Bcc = strings.Join(email.RecipientsBcc, ",")
}
// Convert attachments to Postmark format
for _, attachment := range email.Attachments {
// Create the postmark attachment
postmarkAttachment := &postmark.Attachment{
ContentType: attachment.FileType,
Name: attachment.FileName,
}
// Read all content from the attachment
reader := bufio.NewReader(attachment.FileReader)
var content []byte
if content, err = io.ReadAll(reader); err != nil {
return
}
// Encode as base64
postmarkAttachment.Content = base64.StdEncoding.EncodeToString(content)
// Add to the email
postmarkEmail.Attachments = append(postmarkEmail.Attachments, *postmarkAttachment)
}
// Add importance
if email.Important {
postmarkEmail.Headers = append(
postmarkEmail.Headers,
postmark.Header{Name: "X-Priority", Value: "1 (Highest)"},
postmark.Header{Name: "X-MSMail-Priority", Value: "High"},
postmark.Header{Name: "Importance", Value: "High"},
)
}
// Send the email
var resp postmark.EmailResponse
if resp, err = client.SendEmail(ctx, postmarkEmail); err != nil {
return
}
// Check the response from Postmark
if resp.ErrorCode > 0 {
err = fmt.Errorf("error from postmark: %s error code: %d", resp.Message, resp.ErrorCode)
}
return
}