Skip to content

Commit 4f14285

Browse files
committed
Added requested Mailjet samples.
1 parent 7a246e4 commit 4f14285

File tree

11 files changed

+284
-2
lines changed

11 files changed

+284
-2
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cache:
3636
- appengine/logging/node_modules/
3737
- appengine/loopback/node_modules/
3838
- appengine/mailgun/node_modules/
39+
- appengine/mailjet/node_modules/
3940
- appengine/memcached/node_modules/
4041
- appengine/mongodb/node_modules/
4142
- appengine/parse-server/node_modules/
@@ -49,7 +50,8 @@ cache:
4950
- appengine/twilio/node_modules/
5051
- appengine/webpack/node_modules/
5152
- appengine/websockets/node_modules/
52-
- computeengine/sendgrid/node_modules/
53+
- bigquery/node_modules/
54+
- computeengine/node_modules/
5355
- datastore/node_modules/
5456
- functions/uuid/node_modules/
5557
- logging/node_modules/

appengine/mailjet/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Express.js + Mailjet on Google App Engine
2+
3+
> [Mailjet][1] is an all-in-one email service provider.
4+
>
5+
> www.mailjet.com
6+
7+
This sample application demonstrates how to use [Express.js][2] and
8+
[node-mailjet][3] to send transactional email on [Google App Engine][4].
9+
10+
You can also read the [Mailjet documentation][5].
11+
12+
[1]: https://www.mailjet.com/
13+
[2]: http://expressjs.com
14+
[3]: https://github.com/mailjet/mailjet-apiv3-nodejs
15+
[4]: https://cloud.google.com/appengine
16+
[5]: https://dev.mailjet.com/

appengine/mailjet/app.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var path = require('path');
18+
var bodyParser = require('body-parser');
19+
20+
// [START setup]
21+
var Mailjet = require('node-mailjet').connect(
22+
process.env.MJ_APIKEY_PUBLIC,
23+
process.env.MJ_APIKEY_PRIVATE
24+
);
25+
// [END setup]
26+
27+
var app = express();
28+
29+
// Setup view engine
30+
app.set('views', path.join(__dirname, 'views'));
31+
app.set('view engine', 'jade');
32+
33+
// Parse form data
34+
app.use(bodyParser.json());
35+
app.use(bodyParser.urlencoded({ extended: false }));
36+
37+
// [START index]
38+
app.get('/', function(req, res) {
39+
res.render('index');
40+
});
41+
// [END index]
42+
43+
// [START hello]
44+
app.post('/hello', function(req, res, next) {
45+
var options = {
46+
// From
47+
FromEmail: 'no-reply@appengine-mailjet-demo.com',
48+
FromName: 'Mailjet Demo',
49+
// To
50+
Recipients: [{ Email: req.body.email }],
51+
// Subject
52+
Subject: 'Hello World!',
53+
// Body
54+
'Text-part': 'Mailjet on Google App Engine with Node.js',
55+
'Html-part': '<h3>Mailjet on Google App Engine with Node.js</h3>'
56+
};
57+
58+
var request = Mailjet.post('send').request(options);
59+
60+
request
61+
.on('success', function (response, body) {
62+
console.log(response.statusCode, body);
63+
// Render the index route on success
64+
return res.render('index', {
65+
sent: true
66+
});
67+
})
68+
.on('error', function (err) {
69+
return next(err);
70+
});
71+
});
72+
// [END hello]
73+
74+
// [START server]
75+
var server = app.listen(
76+
process.env.PORT || 8080,
77+
'0.0.0.0',
78+
function () {
79+
var address = server.address().address;
80+
var port = server.address().port;
81+
console.log('App listening at http://%s:%s', address, port);
82+
console.log('Press Ctrl+C to quit.');
83+
}
84+
);
85+
// [END server]
86+

appengine/mailjet/app.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
env_variables:
18+
MJ_APIKEY_PUBLIC: <your-mj-apikey-public>
19+
MJ_APIKEY_PRIVATE: <your-mj-apikey-private>
20+
21+
skip_files:
22+
- ^(.*/)?.*/node_modules/.*$
23+
# [END app_yaml]

appengine/mailjet/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "appengine-mailjet",
3+
"description": "An example of using Mailjet in Node.js on Google App Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": "~4.2"
10+
},
11+
"scripts": {
12+
"start": "node app.js",
13+
"deploy": "gcloud preview app deploy"
14+
},
15+
"dependencies": {
16+
"body-parser": "^1.14.2",
17+
"express": "^4.13.4",
18+
"jade": "^1.11.0",
19+
"node-mailjet": "^1.1.0"
20+
}
21+
}

appengine/mailjet/views/index.jade

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
doctype html
15+
html
16+
head
17+
title= title
18+
body
19+
h1 Hello World!
20+
p Express.js + Mailjet on Google App Engine.
21+
hr
22+
if sent
23+
p Email sent!
24+
else
25+
form(name="hello", action="/hello", method="post")
26+
input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;")
27+
input(type="submit", value="Send")

computeengine/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@ Install dependencies:
1414

1515
### sendgrid.js
1616

17-
Also required a `SENDGRID_API_KEY` environment variable to be set.
17+
Requires a `SENDGRID_API_KEY` environment variable to be set.
1818

1919
npm run sendgrid
2020

21+
### mailjet.js
22+
23+
Requires `MAILJET_API_KEY` and `MAILJET_API_SECRET` environment variables to be
24+
set.
25+
26+
npm run mailjet
27+
2128
### vms.js
2229

2330
npm run vms

computeengine/mailjet.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START send]
17+
var mailer = require('nodemailer');
18+
var smtp = require('nodemailer-smtp-transport');
19+
20+
var transport = mailer.createTransport(smtp({
21+
service: 'Mailjet',
22+
auth: {
23+
user: process.env.MAILJET_API_KEY || '<your-mailjet-api-key',
24+
pass: process.env.MAILJET_API_SECRET || '<your-mailjet-api-secret>'
25+
}
26+
}));
27+
28+
transport.sendMail({
29+
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address
30+
to: 'EMAIL@EXAMPLE.COM', // To address
31+
subject: 'test email from Node.js on Google Cloud Platform', // Subject
32+
text: 'Hello!\n\nThis a test email from Node.js.' // Content
33+
}, function (err, json) {
34+
if (err) {
35+
console.log(err);
36+
} else {
37+
console.log(json);
38+
}
39+
});
40+
// [END send]

computeengine/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
},
1111
"scripts": {
1212
"sendgrid": "node sendgrid.js",
13+
"mailjet": "node mailjet.js",
1314
"vms": "node vms.js",
1415
"vms_api": "node vms_api.js"
1516
},
1617
"dependencies": {
1718
"gcloud": "^0.30.3",
1819
"googleapis": "^4.0.0",
20+
"nodemailer": "^2.3.2",
21+
"nodemailer-smtp-transport": "^2.4.2",
1922
"sendgrid": "^2.0.0"
2023
}
2124
}

test/appengine/all.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ var sampleTests = [
135135
args: ['app.js'],
136136
msg: 'Express.js + Mailgun on Google App Engine.'
137137
},
138+
{
139+
dir: 'appengine/mailjet',
140+
cmd: 'node',
141+
args: ['app.js'],
142+
msg: 'Express.js + Mailjet on Google App Engine.'
143+
},
138144
{
139145
dir: 'appengine/memcached',
140146
cmd: 'node',

0 commit comments

Comments
 (0)