Transactional template example doesn't compile
dgellow opened this issue · comments
Sam El-Borai commented
Hi,
The Go version of the integration example available after creating a template isn't correct and doesn't compile.
Where to find it
The snippet is the one displayed at https://app.mailjet.com/template/TEMPLATE_ID/send (with a correct template ID).
The example (directly copy pasted)
/*
This call sends a message to the given recipient with vars and custom vars.
*/
package main
import (
"fmt"
"log"
"os"
mailjet "github.com/mailjet/mailjet-apiv3-go"
)
func main () {
mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
messagesInfo := []mailjet.InfoMessagesV31 {
mailjet.InfoMessagesV31{
From: &mailjet.RecipientV31{
Email: "example@example.org",
Name: "My Awesome Company",
},
To: &mailjet.RecipientsV31{
mailjet.RecipientV31 {
Email: "passenger1@mailjet.com",
Name: "passenger 1",
},
},
TemplateID: 1234,
TemplateLanguage: true,
Subject: "My Awesome Company: Activate your account",
Variables: map[string]interface{}{
"verification_code": ""
},
},
messages := mailjet.MessagesV31{Info: messagesInfo }
res, err := m.SendMailV31(&messages)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Data: %+v
", res)
}
What is wrong
package main
import (
Newline missing between package
and import
(Go convention).
mailjet "github.com/mailjet/mailjet-apiv3-go"
As far as I can tell, the aliasing is redundant.
mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
Should be mailjet.NewMailjetClient
.
mailjetClient := NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
// [...]
res, err := m.SendMailV31(&messages)
The client is first called mailjetClient
then just m
.
messagesInfo := []mailjet.InfoMessagesV31{
mailjet.InfoMessagesV31{
// [...]
Variables: map[string]interface{}{
"verification_code": ""
},
},
Two issues here:
- missing
,
at the end of"verification_code": ""
. - an extra
}
is missing after the last},
fmt.Printf("Data: %+v
", res)
Newline in the middle of the string.
Corrected version
/*
This call sends a message to the given recipient with vars and custom vars.
*/
package main
import (
"fmt"
mailjet "github.com/mailjet/mailjet-apiv3-go"
"log"
"os"
)
func main() {
m := mailjet.NewMailjetClient(os.Getenv("MJ_APIKEY_PUBLIC"), os.Getenv("MJ_APIKEY_PRIVATE"))
messagesInfo := []mailjet.InfoMessagesV31{
mailjet.InfoMessagesV31{
From: &mailjet.RecipientV31{
Email: "example@example.org",
Name: "My Awesome Company",
},
To: &mailjet.RecipientsV31{
mailjet.RecipientV31{
Email: "passenger1@mailjet.com",
Name: "passenger 1",
},
},
TemplateID: 1234,
TemplateLanguage: true,
Subject: "My Awesome Company: Activate your account",
Variables: map[string]interface{}{
"verification_code": "",
},
},
}
messages := mailjet.MessagesV31{Info: messagesInfo}
res, err := m.SendMailV31(&messages)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Data: %+v", res)
}
Hope that helps.
Lyubomir Atanasov commented
Hi @dgellow thank you for spotting the issue, we will have it fixed.