Mails into the Web

Requirement: "Given this Mailchimp newsletter, embed the latest issue in that Jekyll static website".

Mailchimp offers many APIs, but this approach requires too many complications. Instead my solution relies on RSS: given the RSS feed of your newsletter (you can obtain it with the "RSS" link on the top-right corner while visualizing an issue on the Mailchimp website) you can retrieve the full HTML of the latest campaigns and embed it directly.

First problem: Mailchimp doesn't have CORS headers. So you have to use a proxy.
Second problem: the HTML may include a <style> node with custom global CSS, which will mess up the web page used to embed the message. So you have to strip it out.

The code:

$.ajax({
  url: 'https://cors-anywhere.herokuapp.com/https://us5.campaign-archive.com/feed?u=YOUR_NEWSLETTER_IDENTIFIER',
  method: 'GET',
  beforeSend: function(xhr) {
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  },
  success: function(data) {
    var contents = $(data).find('item').first().find('description').html();
    contents = contents.replace(/<style type="text\/css">[^<]*<\/style>/m, '');
    $('#latest_mail').empty().append(contents);
  }
});
Show Comments