Repository: mailgun/mailgun-go
Pull request #215 (2020-02-18 01:48:07)

Added SetTemplateVersion and SetTemplateRenderText methods to Message
- Added `SetTemplateVersion` method which configure `t:version` param
- Added `SetTemplateRenderText` method which configure `t:text` param

These described in https://documentation.mailgun.com/en/latest/api-sending.html#sending

related: #208


Patch:
diff --git a/messages.go b/messages.go
index 105c98e..9c92884 100644
--- a/messages.go
+++ b/messages.go
@@ -40,6 +40,8 @@ type Message struct {
 	templateVariables  map[string]interface{}
 	recipientVariables map[string]map[string]interface{}
 	domain             string
+	templateVersionTag string
+	templateRenderText bool
 
 	dkimSet           bool
 	trackingSet       bool
@@ -438,6 +440,16 @@ func (m *Message) SetTrackingOpens(trackingOpens bool) {
 	m.trackingOpensSet = true
 }
 
+//SetTemplateVersion information is found in the Mailgun documentation.
+func (m *Message) SetTemplateVersion(tag string) {
+	m.templateVersionTag = tag
+}
+
+//SetTemplateRenderText information is found in the Mailgun documentation.
+func (m *Message) SetTemplateRenderText(render bool) {
+	m.templateRenderText = render
+}
+
 // AddHeader allows you to send custom MIME headers with the message.
 func (m *Message) AddHeader(header, value string) {
 	if m.headers == nil {
@@ -606,6 +618,14 @@ func (mg *MailgunImpl) Send(ctx context.Context, message *Message) (mes string,
 		message.domain = mg.Domain()
 	}
 
+	if message.templateVersionTag != "" {
+		payload.addValue("t:version", message.templateVersionTag)
+	}
+
+	if message.templateRenderText {
+		payload.addValue("t:text", yesNo(message.templateRenderText))
+	}
+
 	r := newHTTPRequest(generateApiUrlWithDomain(mg, message.specific.endpoint(), message.domain))
 	r.setClient(mg.Client())
 	r.setBasicAuth(basicAuthUser, mg.APIKey())


Test patch:
diff --git a/messages_test.go b/messages_test.go
index 96acf29..c6abaf0 100644
--- a/messages_test.go
+++ b/messages_test.go
@@ -537,3 +537,38 @@ func TestSendTemplate(t *testing.T) {
 	ensure.DeepEqual(t, msg, exampleMessage)
 	ensure.DeepEqual(t, id, exampleID)
 }
+
+func TestSendTemplateOptions(t *testing.T) {
+	const (
+		exampleDomain      = "testDomain"
+		exampleAPIKey      = "testAPIKey"
+		toUser             = "test@test.com"
+		exampleMessage     = "Queue. Thank you"
+		exampleID          = "<20111114174239.25659.5817@samples.mailgun.org>"
+		templateName       = "my-template"
+		templateVersionTag = "initial"
+		templateRenderText = "yes"
+	)
+	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+		ensure.DeepEqual(t, req.FormValue("template"), templateName)
+		ensure.DeepEqual(t, req.FormValue("t:version"), templateVersionTag)
+		ensure.DeepEqual(t, req.FormValue("t:text"), templateRenderText)
+		rsp := fmt.Sprintf(`{"message":"%s", "id":"%s"}`, exampleMessage, exampleID)
+		fmt.Fprint(w, rsp)
+	}))
+	defer srv.Close()
+
+	mg := NewMailgun(exampleDomain, exampleAPIKey)
+	mg.SetAPIBase(srv.URL + "/v3")
+	ctx := context.Background()
+
+	m := mg.NewMessage(fromUser, exampleSubject, "", toUser)
+	m.SetTemplate(templateName)
+	m.SetTemplateRenderText(true)
+	m.SetTemplateVersion(templateVersionTag)
+
+	msg, id, err := mg.Send(ctx, m)
+	ensure.Nil(t, err)
+	ensure.DeepEqual(t, msg, exampleMessage)
+	ensure.DeepEqual(t, id, exampleID)
+}