Skip to content

Commit 6ef3bf6

Browse files
authored
Merge pull request #18 from ainsleyclark/headers
Headers
2 parents 847df1d + 5d1900e commit 6ef3bf6

27 files changed

+707
-39
lines changed

.github/workflows/email.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ jobs:
6060
SPARKPOST_FROM_NAME: ${{ secrets.SPARKPOST_FROM_NAME }}
6161
run: |
6262
# Make file runnable, might not be necessary
63-
chmod +x "${GITHUB_WORKSPACE}/bin/tests.sh"
63+
chmod +x ./bin/tests.sh
6464
# Run tests
6565
# Ignore Postal, no server active.
66-
"${GITHUB_WORKSPACE}/bin/tests.sh mailgun"
67-
"${GITHUB_WORKSPACE}/bin/tests.sh postmark"
68-
"${GITHUB_WORKSPACE}/bin/tests.sh sendgrid"
69-
"${GITHUB_WORKSPACE}/bin/tests.sh smtp"
70-
"${GITHUB_WORKSPACE}/bin/tests.sh sparkpost"
66+
./bin/tests.sh mailgun
67+
./bin/tests.sh postmark
68+
./bin/tests.sh sendgrid
69+
./bin/tests.sh smtp
70+
./bin/tests.sh sparkpost
7171

README.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ cfg := mail.Config{
8989
APIKey: "my-key",
9090
FromAddress: "hello@gophers.com",
9191
FromName: "Gopher",
92+
Client: http.DefaultClient, // Client is optional
9293
}
9394

9495
mailer, err := drivers.NewSparkpost(cfg)
@@ -110,6 +111,9 @@ tx := &mail.Transmission{
110111
Subject: "My email",
111112
HTML: "<h1>Hello from Go Mail!</h1>",
112113
PlainText: "Hello from Go Mail!",
114+
Headers: map[string]string{
115+
"X-Go-Mail": "Test",
116+
},
113117
}
114118

115119
result, err := mailer.Send(tx)
@@ -425,18 +429,10 @@ The driver flag can be one of the following:
425429
- `smtp`
426430
- `sparkpost`
427431

428-
## TODO
429-
430-
- Add headers to transmission
431-
- Add templates to providers - Sparkpost et al.
432-
433432
## Contributing
434433

435434
We welcome contributors, but please read the [contributing document](CONTRIBUTING.md) before making a pull request.
436435

437436
## Licence
438437

439438
Code Copyright 2021 Go Mail. Code released under the [MIT Licence](LICENCE).
440-
441-
<a href="https://www.producthunt.com/posts/go-mail?utm_source=badge-review&utm_medium=badge&utm_souce=badge-go-mail#discussion-body" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/review.svg?post_id=324954&theme=light" alt="Go Mail - 📧 A cross platform mail driver for GoLang. | Product Hunt" width="250" height="54" /></a>
442-
<a href="https://www.producthunt.com/posts/go-mail?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-go-mail" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=324954&theme=light" alt="Go Mail - 📧 A cross platform mail driver for GoLang. | Product Hunt" width="250" height="54" /></a>

drivers/drivers_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ var (
5959
Subject: "Subject",
6060
HTML: "<h1>HTML</h1>",
6161
PlainText: "PlainText",
62+
Headers: map[string]string{
63+
"X-Go-Mail": "Test",
64+
},
6265
}
6366
// Trans is the transmission with an
6467
// attachment used for testing.

drivers/mailgun.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewMailgun(cfg mail.Config) (mail.Mailer, error) {
5353
}
5454
return &mailGun{
5555
cfg: cfg,
56-
client: client.New(),
56+
client: client.New(cfg.Client),
5757
}, nil
5858
}
5959

@@ -132,6 +132,10 @@ func (m *mailGun) Send(t *mail.Transmission) (mail.Response, error) {
132132
}
133133
}
134134

135+
for k, v := range t.Headers {
136+
f.AddValue("h:"+k, v)
137+
}
138+
135139
url := fmt.Sprintf("%s/%s", m.cfg.URL, strings.TrimPrefix(fmt.Sprintf(mailgunEndpoint, m.cfg.Domain), "/"))
136140
req := httputil.NewHTTPRequest(http.MethodPost, url)
137141
req.SetBasicAuth("api", m.cfg.APIKey)

drivers/postal.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewPostal(cfg mail.Config) (mail.Mailer, error) {
5252
}
5353
return &postal{
5454
cfg: cfg,
55-
client: client.New(),
55+
client: client.New(cfg.Client),
5656
}, nil
5757
}
5858

@@ -68,6 +68,7 @@ type (
6868
HTML string `json:"html_body"`
6969
PlainText string `json:"plain_body"`
7070
Attachments []postalAttachment `json:"attachments"`
71+
Headers map[string]string `json:"headers"`
7172
}
7273
// postalAttachment defines a singular Postal mail attachment.
7374
postalAttachment struct {
@@ -154,6 +155,8 @@ func (d *postal) Send(t *mail.Transmission) (mail.Response, error) {
154155
}
155156
}
156157

158+
tx.Headers = t.Headers
159+
157160
pl, err := newJSONData(tx)
158161
if err != nil {
159162
return mail.Response{}, err

drivers/postmark.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,23 @@ func NewPostmark(cfg mail.Config) (mail.Mailer, error) {
5151
}
5252
return &postmark{
5353
cfg: cfg,
54-
client: client.New(),
54+
client: client.New(cfg.Client),
5555
}, nil
5656
}
5757

5858
type (
5959
// postmarkTransmission defines the data to be sent to the Postmark API.
6060
postmarkTransmission struct {
61-
From string `json:"From"`
62-
To string `json:"To"`
63-
CC string `json:"Cc"`
64-
BCC string `json:"Bcc"`
65-
Subject string `json:"Subject"`
66-
Tag string `json:"Tag"`
67-
HTML string `json:"HtmlBody"`
68-
PlainText string `json:"TextBody"`
69-
ReplyTo string `json:"ReplyTo"`
70-
Headers []struct {
71-
Name string `json:"Name"`
72-
Value string `json:"Value"`
73-
} `json:"headers"`
61+
From string `json:"From"`
62+
To string `json:"To"`
63+
CC string `json:"Cc"`
64+
BCC string `json:"Bcc"`
65+
Subject string `json:"Subject"`
66+
Tag string `json:"Tag"`
67+
HTML string `json:"HtmlBody"`
68+
PlainText string `json:"TextBody"`
69+
ReplyTo string `json:"ReplyTo"`
70+
Headers []postmarkHeader `json:"headers"`
7471
TrackOpens bool `json:"TrackOpens"`
7572
TrackLinks string `json:"TrackLinks"`
7673
Attachments []postmarkAttachment `json:"Attachments"`
@@ -80,6 +77,12 @@ type (
8077
} `json:"Metadata"`
8178
MessageStream string `json:"MessageStream"`
8279
}
80+
// postmarkHeaders defines the key value pair of custom headers
81+
// to send with the email.
82+
postmarkHeader struct {
83+
Name string `json:"Name"`
84+
Value string `json:"Value"`
85+
}
8386
// postmarkAttachment defines a singular Postmark mail attachment.
8487
postmarkAttachment struct {
8588
Name string `json:"Name"`
@@ -156,6 +159,13 @@ func (d *postmark) Send(t *mail.Transmission) (mail.Response, error) {
156159
}
157160
}
158161

162+
for k, v := range t.Headers {
163+
tx.Headers = append(tx.Headers, postmarkHeader{
164+
Name: k,
165+
Value: v,
166+
})
167+
}
168+
159169
pl, err := newJSONData(tx)
160170
if err != nil {
161171
return mail.Response{}, err

drivers/sendgrid.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewSendGrid(cfg mail.Config) (mail.Mailer, error) {
5252
}
5353
return &sendGrid{
5454
cfg: cfg,
55-
client: client.New(),
55+
client: client.New(cfg.Client),
5656
}, nil
5757
}
5858

@@ -211,6 +211,8 @@ func (d *sendGrid) Send(t *mail.Transmission) (mail.Response, error) {
211211
}
212212
}
213213

214+
tx.Headers = t.Headers
215+
214216
pl, err := newJSONData(tx)
215217
if err != nil {
216218
return mail.Response{}, err

drivers/smtp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (m *smtpClient) getTo(t *mail.Transmission) []string {
9595
func (m *smtpClient) bytes(t *mail.Transmission) []byte {
9696
buf := bytes.NewBuffer(nil)
9797

98+
for k, v := range t.Headers {
99+
buf.WriteString(fmt.Sprintf("%s: %s\n", k, v))
100+
}
101+
98102
buf.WriteString("MIME-Version: 1.0\n")
99103
writer := multipart.NewWriter(buf)
100104
boundary := writer.Boundary()

drivers/sparkpost.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewSparkPost(cfg mail.Config) (mail.Mailer, error) {
5454
}
5555
return &sparkPost{
5656
cfg: cfg,
57-
client: client.New(),
57+
client: client.New(cfg.Client),
5858
}, nil
5959
}
6060

@@ -238,6 +238,8 @@ func (d *sparkPost) Send(t *mail.Transmission) (mail.Response, error) {
238238
}
239239
}
240240

241+
tx.Content.Headers = t.Headers
242+
241243
pl, err := newJSONData(tx)
242244
if err != nil {
243245
return mail.Response{}, err

examples/attachments.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
// http://www.apache.org/licenses/LICENSE-2.0
5+
//
6+
// Unless required by applicable law or agreed to in writing, software
7+
// distributed under the License is distributed on an "AS IS" BASIS,
8+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
// See the License for the specific language governing permissions and
10+
// limitations under the License.
11+
12+
package mail
13+
14+
import (
15+
"fmt"
16+
"github.com/ainsleyclark/go-mail/drivers"
17+
"github.com/ainsleyclark/go-mail/mail"
18+
"io/ioutil"
19+
"log"
20+
)
21+
22+
// Attachments example for Go Mail
23+
func Attachments() {
24+
cfg := mail.Config{
25+
URL: "https://api.eu.sparkpost.com",
26+
APIKey: "my-key",
27+
FromAddress: "hello@gophers.com",
28+
FromName: "Gopher",
29+
}
30+
31+
mailer, err := drivers.NewSparkPost(cfg)
32+
if err != nil {
33+
log.Fatalln(err)
34+
}
35+
36+
image, err := ioutil.ReadFile("gopher.jpg")
37+
if err != nil {
38+
log.Fatalln(err)
39+
}
40+
41+
tx := &mail.Transmission{
42+
Recipients: []string{"hello@gophers.com"},
43+
Subject: "My email",
44+
HTML: "<h1>Hello from Go Mail!</h1>",
45+
PlainText: "plain text",
46+
Attachments: []mail.Attachment{
47+
{
48+
Filename: "gopher.jpg",
49+
Bytes: image,
50+
},
51+
},
52+
}
53+
54+
result, err := mailer.Send(tx)
55+
if err != nil {
56+
log.Fatalln(err)
57+
}
58+
59+
fmt.Printf("%+v\n", result)
60+
}

examples/mailgun.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2020 The Go Mail Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package mail
15+
16+
import (
17+
"fmt"
18+
"github.com/ainsleyclark/go-mail/drivers"
19+
"github.com/ainsleyclark/go-mail/mail"
20+
"log"
21+
)
22+
23+
// MailGun example for Go Mail
24+
func MailGun() {
25+
cfg := mail.Config{
26+
URL: "my-url",
27+
APIKey: "my-key",
28+
FromAddress: "hello@gophers.com",
29+
FromName: "Gopher",
30+
Domain: "my-domain",
31+
}
32+
33+
mailer, err := drivers.NewMailGun(cfg)
34+
if err != nil {
35+
log.Fatalln(err)
36+
}
37+
38+
tx := &mail.Transmission{
39+
Recipients: []string{"hello@gophers.com"},
40+
Subject: "My email",
41+
HTML: "<h1>Hello from Go Mail!</h1>",
42+
PlainText: "plain text",
43+
}
44+
45+
result, err := mailer.Send(tx)
46+
if err != nil {
47+
log.Fatalln(err)
48+
}
49+
50+
fmt.Printf("%+v\n", result)
51+
}

examples/postal.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 The Go Mail Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package mail
15+
16+
import (
17+
"fmt"
18+
"github.com/ainsleyclark/go-mail/drivers"
19+
"github.com/ainsleyclark/go-mail/mail"
20+
"log"
21+
)
22+
23+
// Postal example for Go Mail
24+
func Postal() {
25+
cfg := mail.Config{
26+
URL: "https://postal.example.com",
27+
APIKey: "my-key",
28+
FromAddress: "hello@gophers.com",
29+
FromName: "Gopher",
30+
}
31+
32+
mailer, err := drivers.NewPostal(cfg)
33+
if err != nil {
34+
log.Fatalln(err)
35+
}
36+
37+
tx := &mail.Transmission{
38+
Recipients: []string{"hello@gophers.com"},
39+
Subject: "My email",
40+
HTML: "<h1>Hello from Go Mail!</h1>",
41+
PlainText: "plain text",
42+
}
43+
44+
result, err := mailer.Send(tx)
45+
if err != nil {
46+
log.Fatalln(err)
47+
}
48+
49+
fmt.Printf("%+v\n", result)
50+
}

0 commit comments

Comments
 (0)