Powered By GitBook
send-email-with-sendgrid-and-nodejs-on-google-app-engine
Learn how to send email with SendGrid 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 SendGrid account.
    2.
    Create a new API key.
    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 sendgrid
    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 SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
    8
    const SENDGRID_SENDER = process.env.SENDGRID_SENDER;
    9
    const Sendgrid = require('sendgrid')(SENDGRID_API_KEY);
    10
    ​
    11
    const app = express();
    12
    ​
    13
    // Setup view engine
    14
    app.set('views', path.join(__dirname, 'views'));
    15
    app.set('view engine', 'pug');
    16
    ​
    17
    // Parse form data
    18
    app.use(bodyParser.json());
    19
    app.use(bodyParser.urlencoded({ extended: false }));
    20
    ​
    21
    app.get('/', (req, res) => res.render('index'));
    22
    ​
    23
    app.post('/hello', (req, res, next) => {
    24
    const sgReq = Sendgrid.emptyRequest({
    25
    method: 'POST',
    26
    path: '/v3/mail/send',
    27
    body: {
    28
    personalizations: [{
    29
    to: [{ email: req.body.email }],
    30
    subject: 'Hello World!'
    31
    }],
    32
    from: { email: SENDGRID_SENDER },
    33
    content: [{
    34
    type: 'text/plain',
    35
    value: 'SendGrid on App Engine with Node.js.'
    36
    }]
    37
    }
    38
    });
    39
    ​
    40
    Sendgrid.API(sgReq, (err) => {
    41
    if (err) {
    42
    next(err);
    43
    return;
    44
    }
    45
    // Render the index route on success
    46
    res.render('index', {
    47
    sent: true
    48
    });
    49
    return;
    50
    });
    51
    });
    52
    ​
    53
    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 + SendGrid 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
    SENDGRID_SENDER=your-sendgrid-sender-email SENDGRID_API_KEY=your-sendgrid-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
    SENDGRID_SENDER: your-sendgrid-sender-email
    5
    SENDGRID_API_KEY: your-sendgrid-api-key
    Copied!
    The app.yaml makes the app deployable to 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