Mailgun is a transactional email service that allows developers to send, receive and track emails from web-based applications.

With Mailgun, the first 10,000 emails you send every month are free of charge!

If you wish to send an email when a specific event in your mobile application occurs, you can write a KScript method to do this, that your mobile application can then call.


This recipe assumes that you have KScripts enabled for your app and that you have your own Mailgun account


Click the 'Create Some KScript' button and include the following JavaScript into your method:

var client = K.http.createClient(
    'https://api:[email protected]');

var headers = {
    'Content-type': 'multipart/form-data'

var params = {
    to: '[email protected]',
    from: '[email protected]',
    subject: 'Some subject',
    text: 'Some content'
};'/v3/', params, headers);

Note that while your domain in your Mailgun account is in sandbox mode, you can only send emails to upto five authorized recipients

Your mobile application can then call your new KScript method when required or, if you wish to send a recurring email (for example a digest of recent activity), your KScript method can be scheduled to run once per hour or day.

If your application needs to send multiple emails for different events (i.e. from different KScript methods), then we would recommend encapsulating the mailgun integration in a helper method (for example '_mailer_') that takes in generic email parameters (to, from, subject, html, text etc). This helper method can then be included in your KScript methods with an include( _mailer_ ) call. This will prevent duplicating the same code in multiple methods and make it easier to switch email provider in the future.

Further reading

A full reference guide to the Mailgun API service can be found on the Mailgun website


We would like to thank Edson Iván Mendieta Moreno from Via Professional Services for contributing to this article.