Skip to content

Commit afd298e

Browse files
authoredDec 10, 2020
Add support for SendGrid API (sahat#1127)
1 parent d2ba8a4 commit afd298e

File tree

6 files changed

+629
-620
lines changed

6 files changed

+629
-620
lines changed
 

‎.env.example

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ SESSION_SECRET=Your Session Secret goes here
77
MAILGUN_USER=postmaster@sandbox697fcddc09814c6b83718b9fd5d4e5dc.mailgun.org
88
MAILGUN_PASSWORD=29eldds1uri6
99

10-
SENDGRID_USER=hslogin
11-
SENDGRID_PASSWORD=hspassword00
10+
SMTP_USER=none
11+
SMTP_PASSWORD=none
12+
13+
SENDGRID_API_KEY=hdgfadsfahg---apikey---hdgfadsfahg
1214

1315
NYT_KEY=9548be6f3a64163d23e1539f067fcabd:5:68537648
1416

‎README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ credentials.
163163

164164
- Visit <a href="https://www.google.com/recaptcha/admin" target="_blank">Google reCAPTCHA Admin Console</a>
165165
- Enter your application's name as the **Label**
166-
- Chose **reCAPTCHA v2**, **"I'm not a robot" Checkbox**
166+
- Choose **reCAPTCHA v2**, **"I'm not a robot" Checkbox**
167167
- Enter *localhost* as the domain. You can have other domains added in addition to *localhost*
168168
- Accept the terms and submit the form
169169
- Copy the *Site Key* and the *Secret key* into `.env`. These keys will be accessible under Settings, reCAPTCHA keys drop down if you need them again later.
@@ -353,9 +353,13 @@ The same goes for other providers.
353353

354354
<img src="https://sendgrid.com/brand/sg-logo-300.png" width="200">
355355

356+
You can use SendGrid for sending emails. The developer tier allows you to send 100 free emails per day. As an Alternative to SendGrid, you may also choose to use an SMTP service provider. If using SendGrid:
356357
- Go to <a href="https://sendgrid.com/user/signup" target="_blank">https://sendgrid.com/user/signup</a>
357358
- Sign up and **confirm** your account via the *activation email*
358-
- Then enter your SendGrid *Username* and *Password* into `.env` file
359+
- Then enter your SendGrid *API Key* into `.env` file as SENDGRID_API_KEY
360+
361+
If using an SMTP service provider instead of SendGrid:
362+
- Set SMTP_USER and SMTP_PASSWORD in `.env`, and remove SENDGRID_API_KEY
359363

360364
<hr>
361365

‎controllers/contact.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const axios = require('axios');
22
const validator = require('validator');
33
const nodemailer = require('nodemailer');
4+
const nodemailerSendgrid = require('nodemailer-sendgrid');
45

56
/**
67
* GET /contact
@@ -57,13 +58,22 @@ exports.postContact = async (req, res) => {
5758
fromEmail = req.user.email;
5859
}
5960

60-
let transporter = nodemailer.createTransport({
61-
service: 'SendGrid',
62-
auth: {
63-
user: process.env.SENDGRID_USER,
64-
pass: process.env.SENDGRID_PASSWORD
65-
}
66-
});
61+
let transportConfig;
62+
if (process.env.SENDGRID_API_KEY) {
63+
transportConfig = nodemailerSendgrid({
64+
apiKey: process.env.SENDGRID_API_KEY
65+
});
66+
} else {
67+
transportConfig = {
68+
auth: {
69+
user: process.env.SMTP_USER,
70+
pass: process.env.SMTP_PASSWORD
71+
}
72+
};
73+
}
74+
75+
let transporter = nodemailer.createTransport(transportConfig);
76+
6777
const mailOptions = {
6878
to: process.env.SITE_CONTACT_EMAIL,
6979
from: `${fromName} <${fromEmail}>`,
@@ -79,16 +89,9 @@ exports.postContact = async (req, res) => {
7989
.catch((err) => {
8090
if (err.message === 'self signed certificate in certificate chain') {
8191
console.log('WARNING: Self signed certificate in certificate chain. Retrying with the self signed certificate. Use a valid certificate if in production.');
82-
transporter = nodemailer.createTransport({
83-
service: 'SendGrid',
84-
auth: {
85-
user: process.env.SENDGRID_USER,
86-
pass: process.env.SENDGRID_PASSWORD
87-
},
88-
tls: {
89-
rejectUnauthorized: false
90-
}
91-
});
92+
transportConfig.tls = transportConfig.tls || {};
93+
transportConfig.tls.rejectUnauthorized = false;
94+
transporter = nodemailer.createTransport(transportConfig);
9295
return transporter.sendMail(mailOptions);
9396
}
9497
console.log('ERROR: Could not send contact email after security downgrade.\n', err);

‎controllers/user.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { promisify } = require('util');
22
const crypto = require('crypto');
33
const nodemailer = require('nodemailer');
4+
const nodemailerSendgrid = require('nodemailer-sendgrid');
45
const passport = require('passport');
56
const _ = require('lodash');
67
const validator = require('validator');
@@ -13,14 +14,20 @@ const randomBytesAsync = promisify(crypto.randomBytes);
1314
* Helper Function to Send Mail.
1415
*/
1516
const sendMail = (settings) => {
16-
const transporterSettings = {
17-
service: 'SendGrid',
18-
auth: {
19-
user: process.env.SENDGRID_USER,
20-
pass: process.env.SENDGRID_PASSWORD
21-
}
22-
};
23-
let transporter = nodemailer.createTransport(transporterSettings);
17+
let transportConfig;
18+
if (process.env.SENDGRID_API_KEY) {
19+
transportConfig = nodemailerSendgrid({
20+
apiKey: process.env.SENDGRID_API_KEY
21+
});
22+
} else {
23+
transportConfig = {
24+
auth: {
25+
user: process.env.SMTP_USER,
26+
pass: process.env.SMTP_PASSWORD
27+
}
28+
};
29+
}
30+
let transporter = nodemailer.createTransport(transportConfig);
2431

2532
return transporter.sendMail(settings.mailOptions)
2633
.then(() => {
@@ -29,12 +36,9 @@ const sendMail = (settings) => {
2936
.catch((err) => {
3037
if (err.message === 'self signed certificate in certificate chain') {
3138
console.log('WARNING: Self signed certificate in certificate chain. Retrying with the self signed certificate. Use a valid certificate if in production.');
32-
transporter = nodemailer.createTransport({
33-
...transporterSettings,
34-
tls: {
35-
rejectUnauthorized: false
36-
}
37-
});
39+
transportConfig.tls = transportConfig.tls || {};
40+
transportConfig.tls.rejectUnauthorized = false;
41+
transporter = nodemailer.createTransport(transportConfig);
3842
return transporter.sendMail(settings.mailOptions)
3943
.then(() => {
4044
settings.req.flash(settings.successfulType, { msg: settings.successfulMsg });
@@ -519,7 +523,7 @@ exports.postForgot = (req, res, next) => {
519523
};
520524
const mailSettings = {
521525
successfulType: 'info',
522-
successfulMsg: `An e-mail has been sent to ${req.user.email} with further instructions.`,
526+
successfulMsg: `An e-mail has been sent to ${user.email} with further instructions.`,
523527
loggingError: 'ERROR: Could not send forgot password email after security downgrade.\n',
524528
errorType: 'errors',
525529
errorMsg: 'Error sending the password reset message. Please try again shortly.',

0 commit comments

Comments
 (0)
Please sign in to comment.