Skip to content

Commit

Permalink
add supoprt for amp-html param
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen committed Jan 23, 2020
1 parent 02c5416 commit 3979dd4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
21 changes: 19 additions & 2 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type plainMessage struct {
subject string
text string
html string
ampHtml string
template string
}

Expand All @@ -131,15 +132,16 @@ type sendMessageResponse struct {
}

// features abstracts the common characteristics between regular and MIME messages.
// addCC, addBCC, recipientCount, and setHTML are invoked via the package-global AddCC, AddBCC,
// RecipientCount, and SetHtml calls, as these functions are ignored for MIME messages.
// addCC, addBCC, recipientCount, setHtml and setAMPHtml are invoked via the package-global AddCC, AddBCC,
// RecipientCount, SetHtml and SetAMPHtml calls, as these functions are ignored for MIME messages.
// Send() invokes addValues to add message-type-specific MIME headers for the API call
// to Mailgun. isValid yeilds true if and only if the message is valid enough for sending
// through the API. Finally, endpoint() tells Send() which endpoint to use to submit the API call.
type features interface {
addCC(string)
addBCC(string)
setHtml(string)
setAMPHtml(string)
addValues(*formDataPayload)
isValid() bool
endpoint() string
Expand Down Expand Up @@ -336,6 +338,18 @@ func (pm *plainMessage) setHtml(h string) {

func (mm *mimeMessage) setHtml(_ string) {}

// SetAMP is a helper. If you're sending a message that isn't already MIME encoded, SetAMP() will arrange to bundle
// an AMP-For-Email representation of your message in addition to your html & plain-text content.
func (m *Message) SetAMPHtml(html string) {
m.specific.setAMPHtml(html)
}

func (pm *plainMessage) setAMPHtml(h string) {
pm.ampHtml = h
}

func (mm *mimeMessage) setAMPHtml(_ string) {}

// AddTag attaches tags to the message. Tags are useful for metrics gathering and event tracking purposes.
// Refer to the Mailgun documentation for further details.
func (m *Message) AddTag(tag ...string) error {
Expand Down Expand Up @@ -622,6 +636,9 @@ func (pm *plainMessage) addValues(p *formDataPayload) {
if pm.template != "" {
p.addValue("template", pm.template)
}
if pm.ampHtml != "" {
p.addValue("amp-html", pm.ampHtml)
}
}

func (mm *mimeMessage) addValues(p *formDataPayload) {
Expand Down
24 changes: 22 additions & 2 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const (
exampleSubject = "Mailgun-go Example Subject"
exampleText = "Testing some Mailgun awesomeness!"
exampleHtml = "<html><head /><body><p>Testing some <a href=\"http://google.com?q=abc&r=def&s=ghi\">Mailgun HTML awesomeness!</a> at [email protected]</p></body></html>"

exampleMime = `Content-Type: text/plain; charset="ascii"
exampleAMPHtml = `<!doctype html><html ⚡4email><head><meta charset="utf-8"><script async src="https://cdn.ampproject.org/v0.js"></script><style amp4email-boilerplate>body{visibility:hidden}</style><style amp-custom>h1{margin: 1rem;}</style></head><body><h1>Hello, I am an AMP EMAIL!</h1></body></html>`
exampleMime = `Content-Type: text/plain; charset="ascii"
Subject: Joe's Example Subject
From: Joe Example <[email protected]>
To: BARGLEGARF <[email protected]>
Expand Down Expand Up @@ -109,6 +109,26 @@ func TestSendMGHtml(t *testing.T) {
})
}

func TestSendMGAMPHtml(t *testing.T) {
if reason := SkipNetworkTest(); reason != "" {
t.Skip(reason)
}

spendMoney(t, func() {
toUser := os.Getenv("MG_EMAIL_TO")
mg, err := NewMailgunFromEnv()
ensure.Nil(t, err)

ctx := context.Background()
m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser)
m.SetHtml(exampleHtml)
m.SetAMPHtml(exampleAMPHtml)
msg, id, err := mg.Send(ctx, m)
ensure.Nil(t, err)
t.Log("TestSendHtml:MSG(" + msg + "),ID(" + id + ")")
})
}

func TestSendMGTracking(t *testing.T) {
if reason := SkipNetworkTest(); reason != "" {
t.Skip(reason)
Expand Down

0 comments on commit 3979dd4

Please sign in to comment.