Powered By GitBook
send-email-with-sparkpost-and-nodejs-on-google-app-engine
Learn how to send email with SparkPost from a Node.js app to App Engine flexible environment.
Contributed by the Google Cloud community. Not official Google documentation.
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.
    Remember to take note of your project ID; you'll need it later to access your app at http://YOUR_PROJECT_ID.appspot.com​
    2.
    Enable billing for your project.
    3.
    Install the Cloud SDK.

Prepare

    1.
    Sign up for a SparkPost account.
    2.
    ​Create an API key with the "Transmissions: Read/Write" privilege.
    3.
    Initialize a package.json file with the following command:
    1
    npm init
    Copied!
    4.
    Install some dependencies, including the SparkPost Node.js client:
    1
    npm install --save express body-parser pug sparkpost
    Copied!

Create

    1.
    Create a server.js file with the following contents:
    1
    'use strict';
    2
    ​
    3
    const http = require('http');
    4
    const express = require('express');
    5
    const path = require('path');
    6
    const bodyParser = require('body-parser');
    7
    ​
    8
    const SparkPost = require('sparkpost');
    9
    const spClient = new SparkPost(process.env.SPARKPOST_API_KEY);
    10
    ​
    11
    const app = express();
    12
    const srv = http.Server(app);
    13
    ​
    14
    // Setup view engine
    15
    app.set('views', path.join(__dirname, 'views'));
    16
    app.set('view engine', 'pug');
    17
    ​
    18
    // Parse form data
    19
    app.use(bodyParser.json());
    20
    app.use(bodyParser.urlencoded({ extended: false }));
    21
    ​
    22
    app.get('/', (req, res) => res.render('index'));
    23
    ​
    24
    app.post('/hello', (req, res, next) => {
    25
    spClient.transmissions.send({
    26
    options: { sandbox: true },
    27
    content: {
    28
    29
    subject: 'Hello from Google AppEngine!',
    30
    text: 'Google AppEngine + Node.js + SparkPost = awesome!'
    31
    },
    32
    recipients: [
    33
    {address: req.body.email}
    34
    ]
    35
    }).then(result => {
    36
    res.render('index', {sent: true});
    37
    }).catch(err => {
    38
    res.render('index', {err: err});
    39
    console.error(err);
    40
    });
    41
    });
    42
    ​
    43
    srv.listen(process.env.PORT || 8080, () => {
    44
    console.log(`Listening on ${srv.address().port}`);
    45
    });
    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 + sparkpost on google app engine.
    8
    hr
    9
    if sent
    10
    p email sent!
    11
    if err
    12
    p Oh my. Something's not right:
    13
    ul
    14
    each e in err.errors
    15
    li
    16
    strong=e.message+': '
    17
    |#{e.description}
    18
    else
    19
    form(name="hello", action="/hello", method="post")
    20
    input(type="email", placeholder="enter your email to send yourself a hello world message", name="email", style="width: 50%; margin-right: 15px;")
    21
    input(type="submit", value="send")
    Copied!

Run

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

Deploy

    1.
    Create a file named app.yaml with the following contents:
    1
    runtime: nodejs
    2
    env: flex
    3
    env_variables:
    4
    SPARKPOST_API_KEY: your-sparkpost-api-key
    Copied!
    app.yaml describes how to deploy your app to App Engine. Read more about that here.
    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 email. YOUR_PROJECT_ID is the project ID you created earlier in the Cloud Console.
Last modified 7mo ago