Sample Code

We get many requests for sample code, so here, we've compiled a couple of the most requested snippets.

I've listed these by language for easy retrieval. All examples use the Mad Mimi API client Libraries.

PHP

List Recent Promotions

  <?php
require('MadMimi.class.php');
$mimi = new MadMimi('email', 'api-key');
$promotions = new SimpleXMLElement($mimi->Promotions());
foreach ($promotions as $promotion) {
  echo '<a href="http://mim.io/' . $promotion['mimio'] . '">' . $promotion['name'] . '</a>';
}
?>

Send a Transactional E-Mail

  <?php
require('MadMimi.class.php');
$mimi = new MadMimi('email', 'api-key');
$options = array(
       'promotion_name' => 'Test Promotion',
       'recipients' => 'Nicholas Young <test@madmimi.com>',
       'subject' => 'Testing the Mailer API',
       'from' => 'Mad Mimi <no-reply@madmimi.com>'
);
$html_body = "<html><head><title>My title</title></head>
<body>Body content[[tracking_beacon]]</body></html>";
$mimi->SendHTML($options, $html_body);

Send a Raw HTML Transactional E-Mail

  <?php
require('MadMimi.class.php');
$mimi = new MadMimi('email', 'api-key');
$options = array(
       'promotion_name' => 'Test Promotion',
       'recipients' => 'Nicholas Young <test@madmimi.com>',
       'subject' => 'Testing the Mailer API',
       'from' => 'Mad Mimi <no-reply@madmimi.com>'
);
$html_body = "<html><head><title>My title</title></head>
<body>Body content[[tracking_beacon]]</body></html>";
$mimi->SendHTML($options, $html_body);

Send a Plain Text Transactional E-Mail

  <?php
require('MadMimi.class.php');
$mimi = new MadMimi('email', 'api-key');
$options = array(
       'promotion_name' => 'Test Promotion',
       'recipients' => 'Nicholas Young <test@madmimi.com>',
       'subject' => 'Testing the Mailer API',
       'from' => 'Mad Mimi <no-reply@madmimi.com>'
);
$plaintext = "This is my plain text. Like it?";
$mimi->SendPlainText($options, $plaintext);

When executed, any of these methods will return the transaction id, if successful. If you desire to send to a list rather than to a specific recipient, just subsitute the recipients key in the array for list_name => 'Your list name', and include the [[unsubscribe]] macro in your body. Easy.

Ruby

List Recent Promotions

    require 'rubygems'
require 'madmimi'
mimi = MadMimi.new('email address', 'api-key')
mimi.promotions.each do |promotion|
   // Do something here
end
  

Java

Send Transactional Email

    HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(madMimiUrl);
method.addParameter("username", "yourUsername");
method.addParameter("api_key", "yourApiKey");
method.addParameter("promotion_name", "yourPromotionName");
method.addParameter("recipient", "yourRecipientEmailAddress");
method.addParameter("subject", "yourSubject");
method.addParameter("from", "yourFromAddress");
String body = "Your message body here";
method.addParameter("body", body);
int statusCode = httpClient.executeMethod( method );
if (statusCode == 200) {
....