Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
Frontend email options, support SMTP
Browse files Browse the repository at this point in the history
Closes #16.
  • Loading branch information
gaborcsardi committed Oct 18, 2018
1 parent c83a2b6 commit e711391
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 28 deletions.
30 changes: 27 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@ LOGDB_URL=http://logdb:5984/logs
JENKINS_USER=admin
JENKINS_PASSWORD=44186ecbfec14
JENKINS_ROOT_URL=http://localhost:8080
MAILGUN_DOMAIN=put-mailgun-domain-here
MAILGUN_API_KEY=put-mailgun-api-key-here
RHUB_CRAN_MIRROR=https://cloud.r-project.org
RHUB_EMAIL_FROM="r-hub builder" <support@rhub.io>
RHUB_BUILDER_EXTERNAL_URL=http://127.0.0.1
RHUB_ARTIFACTS=local
RHUB_ARTIFACTS_URL=http://127.0.0.1/artifacts/

#####################################################################
# Sending emails

RHUB_EMAIL_FROM="r-hub builder" <support@rhub.io>

# This must be smtp or mailgun
RHUB_EMAIL_MODE=smtp

# Send via mailgun HTTP API
MAILGUN_DOMAIN=<-- put-mailgun-domain-here -->
MAILGUN_API_KEY=<-- put-mailgun-api-key-here -->

# Send via a generic SMTP server
# Here is an example that works with mailgun:
# RHUB_EMAIL_MODE=smtp
# RHUB_SMTP_SERVER=smtp.mailgun.org
# RHUB_SMTP_USERNAME=postmaster@rhub.io
# RHUB_SMTP_PASSWORD=<-- smtp-password -->
# RHUB_SMTP_PORT=465
# RHUB_SMTP_TLS_REQUIRED=true

RHUB_SMTP_SERVER=<-- smtp-server -->
RHUB_SMTP_USERNAME=<-- smtp-username -->
RHUB_SMTP_PASSWORD=<-- smtp-password -->
RHUB_SMTP_PORT=25
RHUB_SMTP_TLS_REQUIRED=true
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ most of R-hub's services.
For testing and development you can simply deploy the project on your
computer, using Docker Compose. Try this first.

1. To make email notifications work, you'll have to set the `MAILGUN_DOMAIN`
and `MAILGUN_API_KEY` variables in the [.env](.env) file. You can create a
mailgun account at <https://mailgun.com>. See <https://mailgun.com/pricing>
for prices. Currently each month the first 10,000 emails are free and
after that you pay at most $0.50 for 1,000 emails. Other email setups will
be implemented later, see issue
[#16](https://github.com/gaborcsardi/rhub-server/issues/16).
1. To make email notifications work, you'll have to configure R-hub's SMTP
settings. You can connect to an SMTP server, or use mailgun. For mailgun,
you need to set the `RHUB_EMAIL_MODE` variable to `mailgun`, and also set
the `MAILGUN_DOMAIN` and `MAILGUN_API_KEY` variables in the
[.env](.env) file. You can create a mailgun account at
<https://mailgun.com>. See <https://mailgun.com/pricing> for prices.
Currently each month the first 10,000 emails are free and
after that you pay at most $0.50 for 1,000 emails. To use an SMTP server,
see the [.env](.env) file. You need to set `RHUB_EMAIL_MODE` to `smtp` and
also set the `RHUB_SMTP_SERVER` variable. If you need need authentication
set `RHUB_SMTP_USERNAME` and `RHUB_SMTP_PASSWORD`.

1. By default the R-hub frontend will run on port 80 of 127.0.0.1. Make
sure this port is available on your machine. Note that by default
Expand Down
10 changes: 8 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ services:
- REDIS_EMAIL_URL=${REDIS_EMAIL_URL}
- GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}
- GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET}
- MAILGUN_DOMAIN=${MAILGUN_DOMAIN}
- MAILGUN_API_KEY=${MAILGUN_API_KEY}
- JENKINS_URL=http://${JENKINS_USER}:${JENKINS_PASSWORD}@jenkins:8080
- RHUB_BUILDER_URL=http://frontend:3000
- RHUB_BUILDER_EXTERNAL_URL=${RHUB_BUILDER_EXTERNAL_URL}
- RHUB_ARTIFACTS_URL=${RHUB_ARTIFACTS_URL}
- RHUB_EMAIL_FROM=${RHUB_EMAIL_FROM}
- RHUB_EMAIL_MODE=${RHUB_EMAIL_MODE}
- MAILGUN_DOMAIN=${MAILGUN_DOMAIN}
- MAILGUN_API_KEY=${MAILGUN_API_KEY}
- RHUB_SMTP_SERVER=${RHUB_SMTP_SERVER}
- RHUB_SMTP_USERNAME=${RHUB_SMTP_USERNAME}
- RHUB_SMTP_PASSWORD=${RHUB_SMTP_PASSWORD}
- RHUB_SMTP_PORT=${RHUB_SMTP_PORT}
- RHUB_SMTP_TLS_REQUIRED=${RHUB_SMTP_TLS_REQUIRED}
volumes:
- uploads-data:/usr/src/app/uploads
depends_on:
Expand Down
9 changes: 2 additions & 7 deletions frontend/lib/email-notification.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@

var fs = require('fs');
var mustache = require('mustache');
var urls = require('../lib/urls');
var pretty_ms = require('pretty-ms');
var mailgun = require('mailgun-js')(
{ apiKey: urls.mailgun_api_key, domain: urls.mailgun_domain });

var SMTP_URL = urls.smtp_url;
var SMTP_SERVER = urls.smtp_server;
var send_email = require('../lib/send-email');

var RHUB_BUILDER_URL = process.env.RHUB_BUILDER_URL ||
'http://127.0.0.1:3000';
Expand Down Expand Up @@ -97,7 +92,7 @@ function email_with_template(build, text_body, html_body, callback) {
html: mustache.render(html_body, dict)
};

mailgun.messages().send(mail, function(error, info) {
send_email(mail, function(error, info) {
if (error) {
console.log(error);
return callback(error);
Expand Down
8 changes: 3 additions & 5 deletions frontend/lib/mail-token.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

var multiline = require('multiline');
var urls = require('../lib/urls');
var mailgun = require('mailgun-js')(
{ apiKey: urls.mailgun_api_key, domain: urls.mailgun_domain });
var send_email = require('../lib/send-email');

var text_body = multiline(function() { /*
Dear R package developer!
Expand Down Expand Up @@ -30,8 +28,8 @@ function mail_token(email, token, callback) {
text: text_body.replace("${code}", token)
};

mailgun.messages().send(mail, function(error, info) {
if (error) {
send_email(mail, function(error, info) {
if (error) {
console.log(error);
return callback(error);
}
Expand Down
5 changes: 2 additions & 3 deletions frontend/lib/mail-verification-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
var multiline = require('multiline');
var uuid = require('uuid');
var urls = require('../lib/urls');
var mailgun = require('mailgun-js')(
{ apiKey: urls.mailgun_api_key, domain: urls.mailgun_domain });
var send_email = require('../lib/send-email');

var redis = require('redis');
var cli_client = null;
Expand Down Expand Up @@ -47,7 +46,7 @@ function mail_verification_code(req, callback) {
text: text_body.replace("${code}", code)
};

mailgun.messages().send(mail, function(error, info) {
send_email(mail, function(error, info) {
if (error) {
console.log(error);
return callback(error);
Expand Down
10 changes: 10 additions & 0 deletions frontend/lib/send-email-mailgun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

var urls = require('../lib/urls');

function send_email_mailgun(mail, callback) {
var mailgun = require('mailgun-js')(
{ apiKey: urls.mailgun_api_key, domain: urls.mailgun_domain });
mailgun.messages().send(mail, callback);
}

module.exports = send_email_mailgun;
24 changes: 24 additions & 0 deletions frontend/lib/send-email-smtp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

var urls = require('../lib/urls');
var nodemailer = require('nodemailer');

function send_email_smtp(mail, callback) {
if (! urls.smtp_server) { return callback("No mail server"); }

var config = {
host: urls.smtp_server,
port: urls.smtp_port,
secure: urls.smtp_tls_required
}

if (!! urls.smtp_username || !! urls.smtp_password) {
config.auth = {
user: urls.smtp_username || '',
pass: urls.smtp_password || '' };
}

var transporter = nodemailer.createTransport(config);
transporter.sendMail(mail, callback);
}

module.exports = send_email_smtp;
14 changes: 14 additions & 0 deletions frontend/lib/send-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

var urls = require('../lib/urls');

function send_email(mail, callback) {
if (urls.email_mode === 'mailgun') {
var send_email_mailgun = require('../lib/send-email-mailgun');
send_email_mailgun(mail, callback);
} else {
var send_email_smtp = require('../lib/send-email-smtp');
send_email_smtp(mail, callback);
}
}

module.exports = send_email;
11 changes: 10 additions & 1 deletion frontend/lib/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ var urls = {
process.env.DOKKU_REDIS_PURPLE_URL ||
process.env.REDIS_EMAIL_URL,
'logdb': process.env.LOGDB_URL || 'http://logdb.url',

// Email settings
'email_mode': process.env.RHUB_EMAIL_MODE || 'mailgun',
'mailgun_api_key': process.env.MAILGUN_API_KEY || 'key',
'mailgun_domain': process.env.MAILGUN_DOMAIN || 'rhub.io'
'mailgun_domain': process.env.MAILGUN_DOMAIN || 'rhub.io',
'smtp_server': process.env.RHUB_SMTP_SERVER,
'smtp_username': process.env.RHUB_SMTP_USERNAME,
'smtp_password': process.env.RHUB_SMTP_PASSWORD,
'smtp_port': process.env.RHUB_SMTP_PORT || 25,
// TLS required by default, set to 'false' (w/o quotes) to disable
'smtp_tls_required': ! (process.env.RHUB_SMTP_TLS_REQUIRED === 'false')
};

module.exports = urls;
5 changes: 5 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"multiline": "^1.0.2",
"mustache": "^2.2.1",
"nano": "^6.2.0",
"nodemailer": "^4.6.8",
"passport": "^0.3.2",
"passport-github": "^1.1.0",
"pretty-ms": "^2.1.0",
Expand Down

0 comments on commit e711391

Please sign in to comment.