Powered By GitBook
send-email-with-mailgun-and-nodejs-on-google-app-engine
Learn how to send email with Mailgun from a Node.js app to App Engine flexible environment.
Contributed by Google employees.
You can check out Node.js and Google Cloud to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud.

Prerequisites

    1.
    Create a project in the Cloud Console.
    2.
    Enable billing for your project.
    3.
    Install the Cloud SDK.

Prepare

    1.
    Sign up for a Mailgun account.
    2.
    Add a new domain. Find your API key in your new domain's settings.
    3.
    Initialize a package.json file with the following command:
    1
    npm init
    Copied!
    4.
    Install some dependencies:
    1
    npm install --save express body-parser pug mailgun
    Copied!

Create

    1.
    Create a server.js file with the following contents:
    1
    'use strict';
    2
    ​
    3
    const express = require('express');
    4
    const path = require('path');
    5
    const bodyParser = require('body-parser');
    6
    ​
    7
    const Mailgun = require('mailgun').Mailgun;
    8
    const mg = new Mailgun(process.env.MAILGUN_API_KEY);
    9
    ​
    10
    const app = express();
    11
    ​
    12
    // Setup view engine
    13
    app.set('views', path.join(__dirname, 'views'));
    14
    app.set('view engine', 'pug');
    15
    ​
    16
    // Parse form data
    17
    app.use(bodyParser.json());
    18
    app.use(bodyParser.urlencoded({ extended: false }));
    19
    ​
    20
    app.get('/', (req, res) => res.render('index'));
    21
    ​
    22
    app.post('/hello', (req, res, next) => {
    23
    const servername = '';
    24
    const options = {};
    25
    ​
    26
    mg.sendText(
    27
    // From
    29
    // To
    30
    req.body.email,
    31
    // Subject
    32
    'Hello World!',
    33
    // Body
    34
    'Mailgun on App Engine with Node.js',
    35
    servername,
    36
    options,
    37
    (err) => {
    38
    if (err) {
    39
    next(err);
    40
    return;
    41
    }
    42
    // Render the index route on success
    43
    res.render('index', {
    44
    sent: true
    45
    });
    46
    }
    47
    );
    48
    });
    49
    ​
    50
    app.listen(process.env.PORT || 8080);
    Copied!
    2.
    Create a directory named views:
    1
    mkdir views
    Copied!
    3.
    Create a file named index.pug inside the views directory with the following contents:
    1
    doctype html
    2
    html
    3
    head
    4
    title= title
    5
    body
    6
    h1 Hello World!
    7
    p Express.js + Mailgun on App Engine.
    8
    hr
    9
    if sent
    10
    p Email sent!
    11
    else
    12
    form(name="hello", action="/hello", method="post")
    13
    input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;")
    14
    input(type="submit", value="Send")
    Copied!

Run

    1.
    Run the app with the following command:
    1
    MAILGUN_API_KEY=your-mailgun-api-key npm start
    Copied!
    2.
    Visit http://localhost:8080 to try sending an email.

Deploy

    1.
    Create an app.yaml file with the following contents:
    1
    runtime: nodejs
    2
    env: flex
    3
    env_variables:
    4
    MAILGUN_API_KEY: your-mailgun-api-key
    Copied!
    The app.yaml makes the app deployable to Google App Engine Managed VMs.
    2.
    Run the following command to deploy your app:
    1
    gcloud app deploy
    Copied!
    3.
    Visit http://YOUR_PROJECT_ID.appspot.com to try sending an email.
Last modified 7mo ago