Repository: mailgun/mailgun-go
Pull request #210 (2020-01-23 15:45:23)

Add support for setting AMP content in messages
Adds a new `SetAMPHtml` method on the `Message` struct to allow configuring and sending an `amp-html` param to the send API, which enables sending of https://amp.dev email content, as described in https://documentation.mailgun.com/en/latest/api-sending.html#sending

Includes test coverage, however due to the strict requirements of AMP, inspecting the original SMTP body is the only way to confirm it was added to the MIME body (since the default sender is `joe@example.com` which won't pass the SPF requirements unless you happen to own the example.com domain). Example AMP email taken from the boilerplate example on the AMP site.

Follows same convention as with `SetHtml` which is a no-op when used against a `mimeMessage` type message


Patch:
diff --git a/messages.go b/messages.go
index bc6012a..105c98e 100644
--- a/messages.go
+++ b/messages.go
@@ -117,6 +117,7 @@ type plainMessage struct {
 	subject  string
 	text     string
 	html     string
+	ampHtml  string
 	template string
 }
 
@@ -131,8 +132,8 @@ 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.
@@ -140,6 +141,7 @@ type features interface {
 	addCC(string)
 	addBCC(string)
 	setHtml(string)
+	setAMPHtml(string)
 	addValues(*formDataPayload)
 	isValid() bool
 	endpoint() string
@@ -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 {
@@ -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) {


Test patch:
diff --git a/messages_test.go b/messages_test.go
index e1af5f5..96acf29 100644
--- a/messages_test.go
+++ b/messages_test.go
@@ -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 www.kc5tja@yahoo.com</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 <joe@example.com>
 To: BARGLEGARF <sam.falvo@rackspace.com>
@@ -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)