-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
61 lines (52 loc) · 1.42 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"github.com/mattevans/postmark-go"
)
func main() {
// Init client with round tripper adding auth fields.
client := postmark.NewClient(
postmark.WithClient(&http.Client{
Transport: &postmark.AuthTransport{Token: "SERVER_API_TOKEN"},
}),
)
// Build the email.
emailReq := &postmark.Email{
From: "mail@company.com",
To: "jack@sparrow.com",
Subject: "My Test Email",
HTMLBody: "<html><body><strong>Hello</strong> dear Postmark user.</body></html>",
TextBody: "Hello dear Postmark user",
Tag: "onboarding",
TrackOpens: true,
}
// Build and append our attachment(s)
emailReq.Attachments = []postmark.EmailAttachment{
buildAttachment("./attachment_example.txt"),
}
// Send it!
_, response, err := client.Email.Send(emailReq)
if err != nil {
fmt.Printf("ERR: \n%v\n%v\n", response, err)
}
}
// buildAttachment receives a given path, reads the file and returns a
// postmark.EmailAttachment
func buildAttachment(path string) postmark.EmailAttachment {
// Read our attachment
bytes, err := ioutil.ReadFile(path)
if err != nil {
fmt.Print(err)
}
// Determine the ContentType.
contentType := http.DetectContentType(bytes)
// Build and return our attachments
return postmark.EmailAttachment{
Name: fmt.Sprintf("example%s", filepath.Ext(path)),
Content: bytes,
ContentType: &contentType,
}
}