If you are sending messages in bulk, often times some property values within your payload will be the same across multiple messages. To avoid repetition and simplify the structure of your payload, you can use Globals to specify specific properties. Those will then be applied to all message objects.
# This call sends a message to one recipient.
curl -s \
-X POST \
--user "$MJ_APIKEY_PUBLIC:$MJ_APIKEY_PRIVATE" \
https://api.mailjet.com/v3.1/send \
-H 'Content-Type: application/json' \
-d '{
"Globals": {
"From": {
"Email": "pilot@mailjet.com",
"Name": "Mailjet Pilot"
},
"Subject": "Your email flight plan!"
},
"Messages":[
{
"To": [
{
"Email": "passenger1@mailjet.com",
"Name": "passenger 1"
}
],
"TextPart": "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!",
"HTMLPart": "<h3>Dear passenger 1, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
},
{
"To": [
{
"Email": "passenger2@mailjet.com",
"Name": "passenger 2"
}
],
"TextPart": "Dear passenger 2, welcome to Mailjet! ",
"HTMLPart": "<h3>Dear passenger 2, welcome to <a href=\"https://www.mailjet.com/\">Mailjet</a>!</h3><br />May the delivery force be with you!"
}
]
}'In Globals you can specify values for all message properties, except To.
Whenever a certain property is specified in both Globals and a message object, the way they interact depends on the property type:
- String, integer and boolean properties will be overwritten by the message properties of the same name.
Globals: "Subject": "Your email flight plan!"
Message: "Subject": "Your promo code!"
Final result: "Subject": "Your promo code!"
- Object type properties (From, Sender, ReplyTo, TemplateErrorReporting, Headers, Variables) will be merged with the message property, overwriting any concurrent property.
Globals: "variables" : {"var1":"value1","var2":"value2"}
Message: "variables" : {"var1":"value1_bis","var3":"value3"}
Final result: "variables" : {"var1":"value1_bis","var2":"value2","var3":"value3"}
Array type properties (Cc, Bcc, Attachments, InlineAttachments) will be appended.
Globals: "Cc" : [{"Email":"passenger@mailjet.com","Name":"passenger"}]
Message: "Cc" : [{"Email":"passenger2@mailjet.com","Name":"passenger2"}]
Final result: "Cc" : [{"Email":"passenger@mailjet.com","Name":"passenger"}, {"Email":"passenger2@mailjet.com","Name":"passenger2"}]