Skip to content

Commit 1d3a3a4

Browse files
committed
Update MailTransporter.js
- Support for using mail relay on JSC
1 parent 24e2ad9 commit 1d3a3a4

1 file changed

Lines changed: 40 additions & 28 deletions

File tree

server/mail-setup/MailTransporter.js

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
// This script creates a transport object which can be used to send emails
22

33
// This script requires the following environment variables to be set:
4-
// - RELAY_HOST (production)
4+
// - DEPLOYMENT_LOCATION : The location where the app is deployed, either local, CSCS or JSC
5+
// - EMAIL_HOSTNAME : The hostname of the smtp server, e.g. smtp.gmail.com, a relay server or an ip address.
6+
// - EMAIL_ADDRESS_SENDER : The email address of the sender
7+
// - EMAIL_PASSWORD : The password of the sender email address (optional, depending on the smtp server configuration)
58

6-
// In order to use this script locally, you need to create a file called
7-
// mail_credentials.js in the same folder as this script. Use the following
8-
// template for the file: mail_credentials_template.js
9-
// Make sure to add the file to .gitignore so that you don't commit your
10-
// credentials to the repository.
119

12-
const nodemailer = require('nodemailer'); // Nodemailer is used for sending emails
10+
const nodemailer = require('nodemailer'); // Nodemailer is used for sending emails
1311

14-
// Define the smtp server to use and the user credentials
15-
16-
// For local development (get credentials from a config file):
17-
// Todo: Add a check for the environment variable NODE_ENV instead of the user
18-
19-
// if process.env.NODE_ENV === 'development'
20-
if (process.env.USER==='eivinhen' || process.env.USER==='Eivind') {
21-
const creds = require('./mail_credentials');
12+
// Email configuration for testing via local development:
13+
if (process.env.DEPLOYMENT_LOCATION==='local') {
2214
var transportConfiguration = {
23-
host: 'smtp.gmail.com', // e.g. smtp.gmail.com
15+
host: process.env.EMAIL_HOSTNAME,
2416
auth: {
25-
user: creds.USER,
26-
pass: creds.PASS
17+
user: process.env.EMAIL_ADDRESS_SENDER,
18+
pass: process.env.EMAIL_PASSWORD
2719
}
28-
}
29-
}
20+
}
3021

31-
// For production (use the mail relay) // if process.env.NODE_ENV === 'production'
32-
else {
33-
var transportConfiguration = {
34-
host: process.env.RELAY_HOST,
35-
port: 25,
36-
secure: false, // Don't use TLS
37-
tls: {rejectUnauthorized: false}
38-
};
22+
} // Configuration for open shift on CSCS
23+
else if (process.env.DEPLOYMENT_LOCATION==='CSCS') {
24+
var transportConfiguration = {
25+
host: process.env.EMAIL_HOSTNAME,
26+
port: 25,
27+
secure: false, // Don't use TLS (default is false, but make it explicit)
28+
tls: {rejectUnauthorized: false}
29+
};
30+
31+
// Configuration for kubernetes on JSC
32+
} else if (process.env.DEPLOYMENT_LOCATION==='JSC') {
33+
var transportConfiguration = {
34+
host: process.env.EMAIL_HOSTNAME,
35+
port: 587,
36+
auth: {
37+
user: process.env.EMAIL_ADDRESS_SENDER,
38+
pass: process.env.EMAIL_PASSWORD
39+
},
40+
tls: {
41+
rejectUnauthorized: false
42+
},
43+
logger: true,
44+
debug: true
45+
}
46+
47+
} else {
48+
console.log('No mail configuration found for deployment location ' + process.env.DEPLOYMENT_LOCATION);
49+
console.log('Please set the environment variable DEPLOYMENT_LOCATION to either local, CSCS or JSC');
50+
var transportConfiguration = {}
3951
}
4052

4153
var mailTransporter = nodemailer.createTransport(transportConfiguration)

0 commit comments

Comments
 (0)