DEV Community

Cover image for Send mail with Netlify and Mailgun
Himanshu Mishra
Himanshu Mishra

Posted on

Send mail with Netlify and Mailgun

For a side project I wanted to send activation emails with license keys. My project was a chrome extension and didn't have much of backend, So I hosted it on recently discovered Netlify and ended up using Netlify functions for little bit backend that I had.

Prerequisites

We will be writing functions in nodejs so some knowledge of javascript will be required.

Get Mailgun api key

To get your private api key, go to Settings > Api Keys.

Api keys

Setup Netlify Functions

We will use netlify-lambda for building and locally running netlify functions. To install netlify-lambda -

npm install netlify-lambda

It is suggested that you don't install it as dev dependency.

We also need to create netlify.toml file to define where functions will be build and served from.

[build]
    command = "npm run build"
    functions = "functions/build" # folder where build functions exist
    publish = "public" # folder where functions will be served from

Create scripts in package.json

netlify-lambda exposes two commands which will help us to build and serve our files

  • netlify-lambda build <folder> - build functions from to destination folder defined in netilfy.toml. It our case it was 'functions/build'.
  • netlify-lambda serve <folder> - serves functions present in locally.

With this information, we can create two scripts in our package.json

{
  ...
  "scripts": {
    "build": "netlify-lambda build functions",
    "serve": "netlify-lambda serve functions"
  }
  ...
}

Create function for sending mail

With everything setup we can finally write function which will send mail. Mailgun's npm library makes super easy. But first let's install it-

npm install mailgun-js

Now create a file in functions folder sendmail.js. Name of the file is very important as it will part of function url (in this case it will be something like https://blahblah.blah/.netlify/functions/sendmail).

Every function have following general systax which exports a handler-

exports.handler = function(event, context, callback) 
{
    // your server-side functionality
}

Let's do some mailgun magic-

const mailgun = require('mailgun-js');

exports.handler = function(event, context, callback) 
{
    const mg = mailgun({
        apiKey: "YOUR_PRIVATE_KEY", 
        domain: "YOUR_DOMAIN"
    });

    const data = {
        from: 'Name <something@YOUR_DOMAIN>',
    to: 'elon@musk.com',
    subject: 'SUBJECT',
    text: 'TEXT',
    html: 'HTML'
    };

   mg.messages().send(data, (error, body) => 
   {
        if (error)
        {
            return console.log(error);
        }

        callback(null, {
            statusCode: 200,
            body: "Mail sent"
        });
   });
}

Test & Deploy

We can locally test our function by running npm run serve in terminal.
To deploy we have to just push commit to github like any other netlify site. Please don't forget to set build command in your deploy settings.

Build settings

And it's done
Mailgun and Netlify make it really simple and easy to do something like this, that too for free. Netlify functions are really powerful and gives you more freedom than something like firebase function for free.

Top comments (0)