Sending Messages

Sending Messages

There are two ways to send emails using Mailgun API:

  • You can pass the components of the messages such as To, From, Subject, HTML and text parts, attachments, etc. Mailgun will build a MIME representation of the message and send it. This is the preferred method.
  • You can also build a MIME string yourself using a MIME library for your programming language and submit it to Mailgun.

Note

You can also use good old SMTP to send messages. But you will have to specify all advanced sending options via MIME headers

POST /<domain>/messages

Sends a message by assembling it from the components. Note that you can specify most parameters multiple times, HTTP supports this out of the box. This makes sense for parameters like cc, to or attachment.

Parameter Description
from Email address for From header
to Email address of the recipient(s). Example: "Bob <bob@host.com>". You can use commas to separate multiple recipients.
cc Same as To but for Cc
bcc Same as To but for Bcc
subject Message subject
text Body of the message. (text version)
html Body of the message. (HTML version)
attachment File attachment. You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments.
o:tag Tag string. See Tagging for more information.
o:campaign Id of the campaign the message belongs to. See Campaign Analytics for details.
o:dkim Enables/disables DKIM signatures on per-message basis. Pass yes or no
o:deliverytime Desired time of delivery. See Date Format.
o:testmode Enables sending in test mode. Pass yes if needed. See Sending in Test Mode
o:tracking Toggles tracking on a per-message basis, see Tracking Messages for details. Pass yes or no.
o:tracking-clicks Toggles clicks tracking on a per-message basis. Has higher priority than domain-level setting. Pass yes, no or htmlonly.
o:tracking-opens Toggles opens tracking on a per-message basis. Has higher priority than domain-level setting. Pass yes or no.
h:X-My-Header h: prefix followed by an arbitrary value allows to append a custom MIME header to the message (X-My-Header in this case).
v:my-var v: prefix followed by an arbitrary name allows to attach a custom JSON data to the message. See Attaching Data to Messages for more information.
POST /<domain>/messages.mime

Posts a message in MIME format. Note: you will need to build a MIME string yourself. Use a MIME library for your programming language to do this. Pass the resulting MIME string as message parameter.

Note

You must use multipart/form-data encoding.

Parameter Description
to Email address of the recipient(s). Example: "Bob <bob@host.com>". You can use commas to separate multiple recipients.
message MIME string of the message. Make sure to use multipart/form-data to send this as a file upload.
o:tag Tag string. See Tagging for more information.
o:campaign Id of the campaign the message belongs to. See Campaign Analytics for details.
o:deliverytime Desired time of delivery. See Date Format.
o:dkim Enables/disabled DKIM signatures on per-message basis. Pass yes or no
o:testmode Enables sending in test mode. Pass yes if needed. See Sending in Test Mode
o:tracking Toggles tracking on a per-message basis, see Tracking Messages for details. Pass yes or no.
o:tracking-clicks Toggles clicks tracking on a per-message basis. Has higher priority than domain-level setting. Pass yes, no or htmlonly.
o:tracking-opens Toggles opens tracking on a per-message basis. Has higher priority than domain-level setting. Pass yes or no.
h:X-My-Header h: prefix followed by an arbitrary value allows to append a custom MIME header to the message (X-My-Header in this case).
v:my-var v: prefix followed by an arbitrary name allows to attach a custom JSON data to the message. See Attaching Data to Messages for more information.

Examples

Warning

Some samples are using curl utility for API examples. UNIX shells require that some characters must be escaped, for example $ becomes \$.

If your API key contains unescaped characters you may receive HTTP error 401 (Unauthorized).

Sending a plain text message:

def post_message
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages",
  :from => "Excited User <me@samples.mailgun.org>",
  :to => "sergeyo@profista.com, serobnic@mail.ru",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!"
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170704.20654.39224@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

def post_text_and_html_and_files
  data = Multimap.new
  data[:from] = "Excited User <me@samples.mailgun.org>"
  data[:to] = "sergeyo@profista.com, serobnic@mail.ru"
  data[:subject] = "Hello"
  data[:text] = "Testing some Mailgun awesomness!"
  data[:html] = "<html>HTML version of the body</html>"
  data[:attachment] = File.new(File.join("files", "test.jpg"))
  data[:attachment] = File.new(File.join("files", "test.txt"))
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages", data
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170705.20654.81656@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

def post_mime_message
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages.mime",
  :to => "sergeyo@profista.com",
  :message => File.new(File.join("files", "message.mime"))
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170706.26694.70704@samples.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

def post_message_tracking_off
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages",
  :from => "Excited User <me@samples.mailgun.org>",
  :to => "sergeyo@profista.com, serobnic@mail.ru",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomness!",
  "o:tracking" => false
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170707.20653.16901@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message and sets emails for CC and BCC:

def post_text_and_html_and_files_with_cc_and_bcc
  data = Multimap.new
  data[:from] = "Excited User <me@samples.mailgun.org>"
  data[:to] = "obukhov.sergey.nickolayevich@yandex.ru"
  data[:cc] = "serobnic@mail.ru"
  data[:bcc] = "sergeyo@profista.com"
  data[:subject] = "Hello"
  data[:text] = "Testing some Mailgun awesomness!"
  data[:html] = "<html>HTML version of the body</html>"
  data[:attachment] = File.new(File.join("files", "test.jpg"))
  data[:attachment] = File.new(File.join("files", "test.txt"))
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages", data
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170707.26695.20461@samples.mailgun.org>"
}

An example of how to set message delivery time using the o:deliverytime option:

def post_message_delivery_time
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages",
  :from => "Excited User <me@samples.mailgun.org>",
  :to => "sergeyo@profista.com",
  :subject => "Hello",
  :text => "Testing some Mailgun awesomeness!",
  "o:deliverytime" => "Fri, 25 Oct 2011 23:10:10 -0000"
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170708.26695.29087@samples.mailgun.org>"
}

An example of how to tag a message with the o:tag option:

def post_message_with_tag
  data = Multimap.new
  data[:from] = "Excited User <me@samples.mailgun.org>"
  data[:to] = "sergeyo@profista.com"
  data[:subject] = "Hello"
  data[:text] = "Testing some Mailgun awesomness!"
  data["o:tag"] = "September newsletter"
  data["o:tag"] = "newsletters"
  RestClient.post "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"\
  "@api.mailgun.net/v2/samples.mailgun.org/messages", data
end

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120203170709.20654.73847@samples.mailgun.org>"
}

Sending a plain text message:

public static ClientResponse PostMessage() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       MultivaluedMapImpl formData = new MultivaluedMapImpl();
       formData.add("from", "Excited User <me@samples.mailgun.org>");
       formData.add("to", "sergeyo@profista.com");
       formData.add("to", "serobnic@mail.ru");
       formData.add("subject", "Hello");
       formData.add("text", "Testing some Mailgun awesomness!");
       return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
               post(ClientResponse.class, formData);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174238.6405.69989@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

public static ClientResponse PostTextAndHtmlAndFiles() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       FormDataMultiPart form = new FormDataMultiPart();
       form.field("from", "Excited User <me@samples.mailgun.org>");
       form.field("to", "sergeyo@profista.com");
       form.field("to", "serobnic@mail.ru");
       form.field("subject", "Hello");
       form.field("text", "Testing some Mailgun awesomness!");
       String file_separator = System.getProperty("file.separator");
       File txtFile = new File("." + file_separator +
                       "files" + file_separator + "test.txt");
       form.bodyPart(new FileDataBodyPart("attachment",txtFile,
                       MediaType.TEXT_PLAIN_TYPE));
       File jpgFile = new File("." + file_separator +
                       "files" + file_separator + "test.jpg");
       form.bodyPart(new FileDataBodyPart("attachment",jpgFile,
                       MediaType.APPLICATION_OCTET_STREAM_TYPE));
       return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).
               post(ClientResponse.class, form);

}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174238.6404.26938@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

public static ClientResponse PostMimeMessage() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages.mime");
       FormDataMultiPart form = new FormDataMultiPart();
       form.field("to", "sergeyo@profista.com");
       String file_separator = System.getProperty("file.separator");
       File mimeFile = new File("." + file_separator + "files" +
                       file_separator + "message.mime");
       form.bodyPart(new FileDataBodyPart("message", mimeFile,
                       MediaType.APPLICATION_OCTET_STREAM_TYPE));
       return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).
               post(ClientResponse.class, form);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174238.25659.9938@samples.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

public static ClientResponse PostMessageTrackingOff() {
       Client client = new Client();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       MultivaluedMapImpl formData = new MultivaluedMapImpl();
       formData.add("from", "Excited User <me@samples.mailgun.org>");
       formData.add("to", "sergeyo@profista.com");
       formData.add("to", "serobnic@mail.ru");
       formData.add("subject", "Hello");
       formData.add("text", "Testing some Mailgun awesomness!");
       formData.add("o:tracking", false);
       return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
               post(ClientResponse.class, formData);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174238.6404.96764@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message and sets emails for CC and BCC:

public static ClientResponse PostTextAndHtmlAndFilesWithCcAndBcc() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       FormDataMultiPart form = new FormDataMultiPart();
       form.field("from", "Excited User <me@samples.mailgun.org>");
       form.field("to", "obukhov.sergey.nickolayevich@yandex.ru");
       form.field("bcc", "sergeyo@profista.com");
       form.field("cc", "serobnic@mail.ru");
       form.field("subject", "Hello");
       form.field("text", "Testing some Mailgun awesomness!");
       String file_separator = System.getProperty("file.separator");
       File txtFile = new File("." + file_separator +
                       "files" + file_separator + "test.txt");
       form.bodyPart(new FileDataBodyPart("attachment",txtFile,
                       MediaType.TEXT_PLAIN_TYPE));
       File jpgFile = new File("." + file_separator +
                       "files" + file_separator + "test.jpg");
       form.bodyPart(new FileDataBodyPart("attachment",jpgFile,
                       MediaType.APPLICATION_OCTET_STREAM_TYPE));
       return webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).
               post(ClientResponse.class, form);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174239.25660.70079@samples.mailgun.org>"
}

An example of how to set message delivery time using the o:deliverytime option:

public static ClientResponse PostMessageDeliveryTime() {
       Client client = new Client();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       MultivaluedMapImpl formData = new MultivaluedMapImpl();
       formData.add("from", "Excited User <me@samples.mailgun.org>");
       formData.add("to", "sergeyo@profista.com");
       formData.add("subject", "Hello");
       formData.add("text", "Testing some Mailgun awesomness!");
       formData.add("o:deliverytime", "Fri, 14 Oct 2011 23:10:10 -0000");
       return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
               post(ClientResponse.class, formData);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174239.25659.5817@samples.mailgun.org>"
}

An example of how to tag a message with the o:tag option:

public static ClientResponse PostMessageWithTag() {
       Client client = new Client();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +
                               "/messages");
       MultivaluedMapImpl formData = new MultivaluedMapImpl();
       formData.add("from", "Excited User <me@samples.mailgun.org>");
       formData.add("to", "sergeyo@profista.com");
       formData.add("subject", "Hello");
       formData.add("text", "Testing some Mailgun awesomness!");
       formData.add("o:tag", "September newsletter");
       formData.add("o:tag", "newsletters");
       return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
               post(ClientResponse.class, formData);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111114174239.6405.95896@samples.mailgun.org>"
}

Sending a plain text message:

function post_message() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' =>'Excited User'.
                          '<Excited User <me@samples.mailgun.org>>',
                          'to' => 'sergeyo@profista.com, serobnic@mail.ru',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!'
                          ));
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080851.17787.43714@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

function post_text_and_html_and_files() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' => 'Excited User <me@samples.mailgun.org>',
                          'to' => 'sergeyo@profista.com',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!',
                          'html' => '<html>HTML version of the body</html>'
                          ));
  $request->addPostFile('attachment', 'files/test.txt');
  $request->addPostFile('attachment', 'files/test.jpg');
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080851.6405.60954@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

function post_mime_message() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/'.
                    'messages.mime',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' => 'Excited User <me@samples.mailgun.org>',
                          'to' => 'sergeyo@profista.com',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!',
                          ));
  $request->addPostFile('message', 'files/message.mime');
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080852.6404.7041@samples.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

function post_message_tracking_off() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' => 'Excited User <me@samples.mailgun.org>',
                          'to' => 'sergeyo@profista.com, serobnic@mail.ru',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!',
                          'o:tracking' => false
                          ));
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080852.17787.24491@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message and sets emails for CC and BCC:

function post_text_and_html_and_files_with_cc_and_bcc() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' => 'Excited User <me@samples.mailgun.org>',
                          'to' => 'obukhov.sergey.nickolayevich@yandex.ru',
                          'cc' => 'serobnic@mail.ru',
                          'bcc' => 'sergeyo@profista.com',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!',
                          'html' => '<html>HTML version of the body</html>'
                          ));
  $request->addPostFile('attachment', 'files/test.txt');
  $request->addPostFile('attachment', 'files/test.jpg');
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080852.6405.53274@samples.mailgun.org>"
}

An example of how to set message delivery time using the o:deliverytime option:

function post_message_delivery_time() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $request
    ->setPostFields(array('from' => 'Excited User <me@samples.mailgun.org>',
                          'to' => 'sergeyo@profista.com, serobnic@mail.ru',
                          'subject' => 'Hello',
                          'text' => 'Testing some Mailgun awesomness!',
                          'o:deliverytime' => 'Fri, 25 Oct 2011 23:10:10 -0000'
                          ));
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080852.6404.39610@samples.mailgun.org>"
}

An example of how to tag a message with the o:tag option:

function post_message_with_tags() {
  $request =
    new HttpRequest('https://api.mailgun.net/v2/samples.mailgun.org/messages',
                    HttpRequest::METH_POST);
  $auth = base64_encode('api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
  $request->setHeaders(array('Authorization' => 'Basic '.$auth));
  $data = array('from' => 'Excited User <me@samples.mailgun.org>',
                'to' => array('sergeyo@profista.com', 'serobnic@mail.ru'),
                'subject' => 'Hello',
                'text' => 'Testing some Mailgun awesomness!',
                'o:tag' => array('September newsletter', 'newsletters')
                );
  $dataString = '';
  foreach ($data as $key => $val) {
    if (is_array($val)) {
      foreach ($val as $v) {
        $dataString .= urlencode($key).'='.urlencode($v).'&';
      }
    } else {
      $dataString .= urlencode($key).'='.urlencode($val).'&';
    }
  }
  $request->setRawPostData(substr($dataString, 0, -1));
  $request->send();
  return $request;
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115080853.17788.513@samples.mailgun.org>"
}

Sending a plain text message:

def post_message():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             data={
                 "from": "Excited User <me@samples.mailgun.org>",
                 "to": ["sergeyo@profista.com",
                        "serobnic@mail.ru"],
                 "subject": "Hello",
                 "text": "Testing some Mailgun awesomness!"
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071511.31733.9015@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

def post_text_and_html_and_files():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             files=MultiDict([
                 ("attachment", open(os.path.join("files",
                                                  "test.jpg"))),
                 ("attachment", open(os.path.join("files",
                                                  "test.txt"))),
                 ]),
             data={
                 "from": "Excited User <me@samples.mailgun.org>",
                 "to": ("sergeyo@profista.com,"
                        "serobnic@mail.ru"),
                 "subject": "Hello",
                 "text": "Testing some Mailgun awesomness!",
                 "html": "<html>HTML version of the body</html>"
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071512.31733.84355@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

def post_mime_message():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages.mime"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             data={
                 "to": "sergeyo@profista.com",
                 },
             files={
                 "message": open(os.path.join("files",
                                              "message.mime"))
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071513.30916.43157@samples.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

def post_message_tracking_off():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             data={
                 "from": "Excited User <me@samples.mailgun.org>",
                 "to": ["sergeyo@profista.com",
                        "serobnic@mail.ru"],
                 "subject": "Hello",
                 "text": "Testing some Mailgun awesomness!",
                 "o:tracking": False
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071513.31733.11043@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message and sets emails for CC and BCC:

def post_text_and_html_and_files_with_cc_and_bcc():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             files=MultiDict([
                 ("attachment", open(os.path.join("files",
                                                  "test.jpg"))),
                 ("attachment", open(os.path.join("files",
                                                  "test.txt"))),
                 ]),
             data={
                 "from": "Excited User <me@samples.mailgun.org>",
                 "to": "obukhov.sergey.nickolayevich@yandex.ru",
                 "cc": "serobnic@mail.ru",
                 "bcc": "sergeyo@profista.com",
                 "subject": "Hello",
                 "text": "Testing some Mailgun awesomness!",
                 "html": "<html>HTML version of the body</html>"
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071514.26600.87528@samples.mailgun.org>"
}

An example of how to set message delivery time using the o:deliverytime option:

def post_message_delivery_time():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             data={
                 "from": "Excited User <me@samples.mailgun.org>",
                 "to": "sergeyo@profista.com",
                 "subject": "Hello",
                 "text": "Testing some Mailgun awesomness!",
                 "o:deliverytime": ("Fri, 25 Oct 2011 "
                                    "23:10:10 -0000"),
                 }
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071515.31734.65362@samples.mailgun.org>"
}

An example of how to tag a message with the o:tag option:

def post_message_with_tag():
    r = requests.\
        post(("https://api.mailgun.net/v2/samples.mailgun.org/"
              "messages"),
             auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
             data=MultiDict([
                 ("from", "Excited User <me@samples.mailgun.org>"),
                 ("to", "sergeyo@profista.com"),
                 ("subject", "Hello"),
                 ("text", "Testing some Mailgun awesomness!"),
                 ("o:tag", "September newsletter"),
                 ("o:tag", "newsletters")
                 ])
             )
    return r

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120207071516.30917.4734@samples.mailgun.org>"
}

Sending a plain text message:

public static RestResponse PostMessage() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddParameter("to", "serobnic@mail.ru");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104146.6404.55913@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

public static RestResponse PostTextAndHtmlAndFiles() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddParameter("to", "serobnic@mail.ru");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("html", "<html>HTML version of the body</html>");
       request.AddFile("attachment", Path.Combine("files", "test.jpg"));
       request.AddFile("attachment", Path.Combine("files","test.txt"));
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104146.17788.96325@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

public static RestResponse PostMimeMessage() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages.mime";
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddFile("message", Path.Combine("files", "message.mime"));
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104147.17787.30322@samples.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

public static RestResponse PostMessageTrackingOff() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddParameter("to", "serobnic@mail.ru");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("o:tracking", false);
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104147.17788.42361@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message and sets emails for CC and BCC:

public static RestResponse PostTextAndHtmlAndFilesWithCcAndBcc() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "obukhov.sergey.nickolayevich@yandex.ru");
       request.AddParameter("cc", "serobnic@mail.ru");
       request.AddParameter("bcc", "sergeyo@profista.com");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("html", "<html>HTML version of the body</html>");
       request.AddFile("attachment", Path.Combine("files", "test.jpg"));
       request.AddFile("attachment", Path.Combine("files","test.txt"));
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104147.17787.15582@samples.mailgun.org>"
}

An example of how to set message delivery time using the o:deliverytime option:

public static RestResponse PostMessageDeliveryTime() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("o:deliverytime", "Fri, 14 Oct 2011 23:10:10 -0000");
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104147.6405.67461@samples.mailgun.org>"
}

An example of how to tag a message with the o:tag option:

public static RestResponse PostMessageWithTag() {

       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnjp29jd6fds4gc373sgvjxteol0");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "sergeyo@profista.com");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("o:tag", "September newsletter");
       request.AddParameter("o:tag", "newsletters");
       request.Method = Method.POST;
       return client.Execute(request);
}

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20111115104147.17788.366@samples.mailgun.org>"
}

Sending a plain text message:

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
    https://api.mailgun.net/v2/samples.mailgun.org/messages \
    -F from='Excited User <me@samples.mailgun.org>' \
    -F to='Sasha Klizhentas <alex@mailgun.net>'\
    -F to=ev@mailgun.net \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!'

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120215125214.26235.78102@samples.mailgun.org>"
}

Sending a message with HTML and text parts. This example also attaches two files to the message:

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
    https://api.mailgun.net/v2/samples.mailgun.org/messages \
    -F from='Excited User <me@samples.mailgun.org>' \
    -F to='Sasha Klizhentas <alex@mailgun.net>'\
    -F to=ev@mailgun.net \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!' \
    -F html='\<html\>HTML version of the body\<\html>' \
    -F attachment=@files/cartman.jpg\
    -F attachment=@files/cartman.png

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120215125215.4813.74280@samples.mailgun.org>"
}

Sending a MIME message which you pre-build yourself using a MIME library of your choice:

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
    https://api.mailgun.net/v2/samples.mailgun.org/messages.mime \
    -F to='Ev <ev@mailgun.net>' \
    -F message=@files/message.mime

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20110701233001.E54C4F01F6B@mxa.mailgun.org>"
}

An example of how to toggle tracking on a per-message basis. Note the o:tracking option. This will disable link rewriting for this message:

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
    https://api.mailgun.net/v2/samples.mailgun.org/messages \
    -F from='Sender Bob <sbob@samples.mailgun.org>' \
    -F to='Sasha Klizhentas <alex@mailgun.net>' \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!' \
    -F o:tracking=False

Response:

{
  "message": "Queued. Thank you.",
  "id": "<20120215125217.26234.47234@samples.mailgun.org>"
}