Using Mailgun with Electric Imp

Another example in my “Using something with Electric Imp” series. ๐Ÿ™‚

This time it is time for sending good old E-Mails.

The Electric Imp system does not include any way to send mails, but this isn’t a big problem. Over atย mailgun.com they provide a free (up to 10.000 mails per month).

All you need to do is to register a user on their site, find your api key and domain that got assigned to you, and put them into the function posted below.

Agent

function mailgun(subject, message)
{
  local from = "imp@no-reply.com";
  local to   = "your@address.com"

  local apikey = "key-something--something";
  local domain = "sandbox34252.mailgun.org";

  local request = http.post("https://api:" + apikey + "@api.mailgun.net/v2/" + domain + "/messages", {"Content-Type": "application/x-www-form-urlencoded"}, "from=" + from + "&to=" + to + "&subject=" + subject + "&text=" + message);

  local response = request.sendsync();
  server.log("Mailgun response: " + response.body);
}
mailgun("Electric Imp", "Just saying hi! :)");

This should give a response looking something like this

2014-01-07 21:59:52 UTC+1: [Agent] Mailgun response: { "message": "Queued. Thank you.", "id": "<20140107205952.31868.40941@sandbox73816.mailgun.org>" }

And in your mailbox you should have a mail with the subject “Electric Imp” and the test saying “Just saying hi! :)” in it.

mailgun

5 comments

Leave a Reply