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 = "[email protected]";
  local to   = "[email protected]"

  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": "<[email protected]>" }

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

mailgun

11 thoughts on “Using Mailgun with Electric Imp”

  1. thanks! this turns out to be the easiest way (with mailgun) to get notification for a small, personal, one-off thing (alerting me to water my plants based on moisture sensor output)

    Reply
  2. I keep getting the fallowing error when using it.

    ERROR: the index ‘http’ does not exist

    Any idea what I did wrong?

    Reply
    • I have no idea, I just tested it again by just copy/pasting the code, and put my api key and such in. Worked fine, are you putting it into the Agent code?

      Reply
  3. I keep getting the fallowing error when using it.

    ERROR: the index ‘http’ does not exist

    Any idea what I did wrong?

    Reply
    • I have no idea, I just tested it again by just copy/pasting the code, and put my api key and such in. Worked fine, are you putting it into the Agent code?

      Reply

Leave a Comment