Skip to content

Messages Calls

Introduction

This is a key part of both APIs, since it is from here that you will be able to trigger transactional email. The key Mailjet API resources in that section are /send/message and /message.

Mailjet's Send API allows you to send transactional emails using our HTTP API, using POST requests. This solution is aimed at users needing a programmatically way to send messages.

Send API allows you to send single messages but also to mutualise the calls by leveraging templating and personalization of the content.

The API will return a simple response indicating if the message is ready to be processed by the Mailjet system. This makes the error management on your side simple and efficient.

Our Send API offers templating features, similar to the Mandrill one. Please refer to our dedicated API guide here.

Send

Info

Maps to /messages/send.json

from mailjet import Client
import os
api_key = os.environ['MJ_APIKEY_PUBLIC']
api_secret = os.environ['MJ_APIKEY_PRIVATE']
mailjet = Client(auth=(api_key, api_secret))
data = {
	'FromEmail': 'pilot@mailjet.com',
	'FromName': 'Mailjet Pilot',
	'Subject': 'Your email flight plan!',
	'Text-part': 'Dear passenger, welcome to Mailjet! May the delivery force be with you!',
	'Html-part': '<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!',
	'Recipients': [{'Email':'passenger@mailjet.com'}]
}
result = mailjet.send.create(data=data)
print result.status_code
print result.json()
{
  "Sent": [
    {
      "Email": "passenger@mailjet.com",
      "MessageID": 111111111111111
    }
  ]
}

Here are the Mailjet Send API properties definitions, along with samples in all the languages we support through our official libraries.

Property NameDescription
FromEmailMust be a valid, activated and registered sender for this account
May include the name part: john@example.com or &lt;john@example.com&gt; or "John Doe" &lt;john@example.com&gt;
MANDATORY - MAX FROM: 1
FromNameMust be a valid, activated and registered sender for this account
May include the name part: john@example.com or &lt;john@example.com&gt; or "John Doe" &lt;john@example.com&gt;
MANDATORY - MAX FROM: 1
SenderThis can be set only on given API Keys. Contact the support team if you want us to enable this setting on your account.
Must be a valid active sender for this account.
Perform a simple GET on resource /sender to view a list of allowed senders for your account, or within the Mailjet Account Settings under Sender Addresses
MAX SENDER: 1
RecipientsList of recipients, must include at least a property Email in each element
Sample: [{"Email":"passenger@mailjet.com","Name":"passenger"}]
MANDATORY
ToMay include the name part: john@example.com or &lt;john@example.com&gt; or "John Doe" &lt;john@example.com&gt;
If a recipient is specified twice (in the to, cc, or bcc), it is counted only once.
Can be a magic list @lists.mailjet.com. See the Address contactslist property.
MAX RECIPIENTS: 50
Cc, BccMay include the name part: john@example.com or &lt;john@example.com&gt; or "John Doe" &lt;john@example.com&gt;
If one recipient is specified twice, it will be counted as one only (including to, cc, bcc)
MAX RECIPIENTS: 50
Cc and Bcc can't be used in conjunction with Recipients property
SubjectAt least 1 char, maximum length is 255 chars
MANDATORY - MAX SUBJECTS: 1
Text-partProvides the Text part of the message
Mandatory if the HTML param is not specified
MANDATORY IF NO HTML - MAX PARTS: 1
Html-partProvides the HTML part of the message
Mandatory if the text param is not specified
MANDATORY IF NO TEXT - MAX PARTS: 1
Mj-TemplateIDThe Template ID or Name to be used as content for this email. Overrides the HTML/Text parts if any.
MANDATORY IF NO HTML/TEXT - MAX TEMPLATEID: 1
Mj-TemplateLanguageActivate the template language processing. By default the template language processing is desactivated. Use True to activate.
Equivalent to using the X-MJ-TemplateLanguage header through SMTP.
More information
MJ-TemplateErrorReportingEmail Address where a carbon copy with error message is sent to.
Equivalent to using the X-MJ-TemplateErrorReporting header through SMTP.
More information
MJ-TemplateErrorDeliverDefine if the message is delivered if an error is discovered in the templating language. By default the delivery is desactivated. Use deliver to let the message be delivered to the recipient, 0 to stop it.
Equivalent to using the X-MJ-TemplateErrorDeliver header through SMTP.
More information
AttachmentsAttach files automatically to this Email
Sum of all attachments, including inline may not exceed 15 MB total
Sample: [{"Content-type": "MIME TYPE", "Filename": "FILENAME.EXT", "content":"BASE64 ENCODED CONTENT"}]
Inline_attachmentsAttach a file for inline use via cid:FILENAME.EXT
Sum of all attachements, including inline may not exceed 15 MB total
Sample: [{"Content-type": "MIME TYPE", "Filename": "FILENAME.EXT", "content":"BASE64 ENCODED CONTENT"}]
Mj-prioManage message processing priority inside your account (API key) scheduling queue.
Default is 2 as in the SMTP submission.
Equivalent of using X-Mailjet-Prio header through SMTP
More information
Mj-campaignGroups multiple messages in one campaign
Equivalent of using X-Mailjet-Campaign header through SMTP.
More information
Mj-deduplicatecampaignBlock/unblock messages to be sent multiple times inside one campaign to the same contact.
0: unblocked (default behavior) - 1: blocked
Equivalent of using X-Mailjet-DeduplicateCampaign header through SMTP.
Can only be used if mj-campaign is specified.
More information
Mj-trackopenForce or disable open tracking on this message, overriding preferences.
Equivalent of using X-Mailjet-TrackOpen header through SMTP.
Can only be used with a HTML part.
More information
Mj-trackclickForce or disable click tracking on this message, overriding preferences.
Equivalent to using the X-Mailjet-TrackClick header through SMTP.
Can only be specified if the HTML part is provided.
More information
Mj-CustomIDAttach a custom ID to the message
Equivalent to using the X-MJ-CustomID header through SMTP.
More information
Mj-EventPayLoadAttach a payload to the message
Equivalent to using the X-MJ-EventPayload header through SMTP.
More information
HeadersAdd lines to the email headers
List of headers as "property":"value" pairs
Notice: overriding values of header properties (ie: subject)
Sample: {"X-My-Header":"my own value", "X-My-Header-2":"my own value 2"}
VarsGlobal variables used for personalisation
MessagesList of messages
Used for bulk emailing.
More information

Send-Template

Info

Maps to /messages/send-template.json

package MyClass;
import com.mailjet.client.errors.MailjetException;
import com.mailjet.client.errors.MailjetClient;
import com.mailjet.client.errors.MailjetRequest;
import com.mailjet.client.errors.MailjetResponse;
import com.mailjet.client.resource.Email;
public class MyClass {
    /**
     * This calls sends an email to one recipient.
     */
    public static void main(String[] args) throws MailjetException {
      MailjetClient client;
      MailjetRequest request;
      MailjetResponse response;
      client = new MailjetClient("api key", "api secret");
      request = new MailjetRequest(Email.resource)
						.property(Email.FROMEMAIL, "pilot@mailjet.com")
						.property(Email.FROMNAME, "Mailjet Pilot")
						.property(Email.MJTEMPLATEID, 1)
						.property(Email.MJTEMPLATELANGUAGE, true)
						.property(Email.SUBJECT, "Your email flight plan!")
						.property(Email.RECIPIENTS, new JSONArray()
                .put(new JSONObject()
                    .put("Email", "passenger@mailjet.com")))
						.property(Email.MJEVENTPAYLOAD, "Eticket,1234,row,15,seat,B");
      response = client.post(request);
      System.out.println(response.getStatus());
      System.out.println(response.getData());
    }
}
{
  "Sent": [
    {
      "Email": "passenger@mailjet.com",
      "MessageID": 111111111111111
    }
  ]
}

First, please visit our Templates dedicated section to learn how to create and store templates with the Mailjet API.

Use the Mj-TemplateID property in your Send API payload to specify the ID of the the template you created.

You must set the Mj-TemplateLanguage property in the payload at true to have the templating language interpreted.

Mailjet offers a templating language to customize the content of your messages, based on conditions and loops. Please visit our dedicated API guide section to learn more.

Info

Maps to /messages/search.json

The response payload of a Send API call will provide you with the MessageID of your messages. You can use this MessageID to access information and statistics about the message.

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Message, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "ArrivedAt": "2015-07-06T07:10:24Z",
      "AttachmentCount": "0",
      "AttemptCount": "0",
      "CampaignID": "51",
      "ContactID": "45",
      "Delay": "0",
      "DestinationID": "14",
      "FilterTime": "61",
      "FromID": "1",
      "ID": "16888509234525280",
      "IsClickTracked": "false",
      "IsHTMLPartIncluded": "false",
      "IsOpenTracked": "false",
      "IsTextPartIncluded": "false",
      "IsUnsubTracked": "false",
      "MessageSize": "20248",
      "SpamassassinScore": "0",
      "SpamassRules": "",
      "StateID": "0",
      "StatePermanent": "false",
      "Status": "sent"
    }
  ],
  "Total": 1
}

Perform a GET on [/message](/docs/api-reference/email-api/categories/messages/) to get basic information about a message, such as the contact it was sent to, who it was sent by, if there were any attachments and how large the message was.

The StateID property shows the current status the messages are in.

Search-Time-Series

Info

Maps to /messages/search-time-series.json

Filter by time range

Leveraging the FromTs and ToTs query filters of the message API resource, you can find messages matching the period. These filters expect UNIX epoch timestamp.

Filter by tag (custom ID)

You can find all the messages associated to a given custom ID (limited to 1 per message at the moment) through the CustomID query filter of the messagesentstatistics resource.

Filter by sender domain

You can filter all the messages sent from a given domain using the FromID query filter of the messagesentstatistics resource. The ID refers to a valid sender resource

Info

Info

Maps to /messages/info.json

<?php
require 'vendor/autoload.php';
use \Mailjet\Resources;
$mj = new \Mailjet\Client(getenv('MJ_APIKEY_PUBLIC'), getenv('MJ_APIKEY_PRIVATE'));
$response = $mj->get(Resources::$Message, ['id' => $id]);
$response->success() && var_dump($response->getData());
?>
{
  "Count": 1,
  "Data": [
    {
      "ArrivedAt": "2015-07-06T07:10:24Z",
      "AttachmentCount": "0",
      "AttemptCount": "0",
      "CampaignID": "51",
      "ContactID": "45",
      "Delay": "0",
      "DestinationID": "14",
      "FilterTime": "61",
      "FromID": "1",
      "ID": "16888509234525280",
      "IsClickTracked": "false",
      "IsHTMLPartIncluded": "false",
      "IsOpenTracked": "false",
      "IsTextPartIncluded": "false",
      "IsUnsubTracked": "false",
      "MessageSize": "20248",
      "SpamassassinScore": "0",
      "SpamassRules": "",
      "StateID": "0",
      "StatePermanent": "false",
      "Status": "sent"
    }
  ],
  "Total": 1
}

Issue a GET request to message containing the ID which was returned by our Send API