Просмотр исходного кода

make smtp more configurable (#200)

pull/203/head
Cian Butler 4 лет назад
committed by GitHub
Родитель
Сommit
470c5339db
6 измененных файлов: 59 добавлений и 51 удалений
  1. +0
    -6
      .env.example
  2. +6
    -4
      _config.yml
  3. +16
    -11
      lib/emailNewPosts.js
  4. +13
    -10
      lib/smtpTransport.js
  5. +23
    -19
      routes/contact.js
  6. +1
    -1
      themes/redbrick-theme

+ 0
- 6
.env.example Просмотреть файл

@@ -1,9 +1,3 @@
SECRET_API_TOKEN=somethingThatMustChange
RECAPTCHA_SITE_KEY=6LdCuwgUAAAAAKB3yf7AcOpyHxIFKJNaH3cNhmEC
RECAPTCHA_SECRET_KEY=
LOG_ROTATE=24h
SMTP_HOST=mailhost.redbrick.dcu.ie
SMTP_PORT=25
SMTP_SECURE=false
SMTP_USER=
SMTP_PASS=

+ 6
- 4
_config.yml Просмотреть файл

@@ -225,11 +225,13 @@ theme: redbrick-theme

# Server Config
server:
useSendMail: false
email:
host: smtp.redbrick.dcu.ie
port: '465'
secure: 'true'
authMethod: 'plain'
host: mailhost.redbrick.dcu.ie
port: 25
secure: false
authMethod: plain
disableAuth: false
auth:
user: ''
pass: ''


+ 16
- 11
lib/emailNewPosts.js Просмотреть файл

@@ -16,6 +16,7 @@ const writeArrayToFile = utils.writeArrayToFile;

const configFile = fs.readFileSync('_config.yml', 'utf8');
const config = yaml.safeLoad(configFile).server;
const sendMailDefault = true;

const postsDirectory = path.join(process.cwd(), 'source/_posts');
const emailLogFilename = path.join(process.cwd(), 'logs/email_update_log');
@@ -28,17 +29,21 @@ function sendEmail ({ senderName, subject, body }, address, callback) {
subject: `[${config.mailsubject}] ${subject}`,
text : body,
};
smtpTransport.sendMail(mailOptions, err => {
if (err) {
logger.error(`smtp failed: ${err}\nTrying again with sendmail`);
sendMailTransport.sendMail(mailOptions, err => {
if (err) {
logger.error(`sendMail failed: ${err}\nUnable to send email "${subject}" to recipient: ${address}`);
}
});
}
callback(err);
});
if (!config.useSendMail || sendMailDefault) {
smtpTransport.sendMail(mailOptions, err => {
if (err) {
logger.error(`SMTP failed: ${err}\nUnable to send email "${subject}" to recipient: ${address}`);
}
callback(err);
});
} else {
sendMailTransport.sendMail(mailOptions, err => {
if (err) {
logger.error(`sendMail failed: ${err}\nUnable to send email "${subject}" to recipient: ${address}`);
}
callback(err);
});
}
}

function sendEmails (addresses, emailData, callback) {


+ 13
- 10
lib/smtpTransport.js Просмотреть файл

@@ -1,18 +1,21 @@
const nodemailer = require('nodemailer');

const fs = require('fs');
const yaml = require('js-yaml');
const fs = require('fs');
const yaml = require('js-yaml');

const configFile = fs.readFileSync('./_config.yml', 'utf8');
const config = yaml.safeLoad(configFile).server;
const config = yaml.safeLoad(configFile).server;

const smtp = config.email;
smtp.host = process.env.SMTP_HOST || config.email.host;
smtp.port = process.env.SMTP_PORT || config.email.port;
const smtp = config.email;
smtp.host = process.env.SMTP_HOST || config.email.host;
smtp.port = process.env.SMTP_PORT || config.email.port;
smtp.secure = process.env.SMTP_SECURE || config.email.secure;
smtp.authMethod = process.env.SMTP_AUTH_METHOD || config.email.authMethod;
smtp.user = process.env.SMTP_USER || config.email.user;
smtp.pass = process.env.SMTP_PASS || config.email.pass;
if (process.env.SMTP_DISABLE_AUTH || config.email.disableAuth) {
smtp.auth = null;
} else {
smtp.authMethod = process.env.SMTP_AUTH_METHOD || config.email.authMethod;
smtp.auth.user = process.env.SMTP_USER || config.email.auth.user;
smtp.auth.pass = process.env.SMTP_PASS || config.email.auth.pass;
}

const smtpTransport = nodemailer.createTransport(smtp);



+ 23
- 19
routes/contact.js Просмотреть файл

@@ -9,6 +9,7 @@ const sendMailTransport = require('../lib/sendMailTransport');

const configFile = fs.readFileSync('./_config.yml', 'utf8');
const config = yaml.safeLoad(configFile).server;
const sendMailDefault = true;
const recaptcha = new ReCAPTCHA({
siteKey : process.env.RECAPTCHA_SITE_KEY || config.recaptcha.siteKey,
secretKey: process.env.RECAPTCHA_SECRET_KEY || config.recaptcha.secretKey,
@@ -28,28 +29,31 @@ router.get('/contact', ({ query }, res) => {
text : query.text,
replyTo: query.email,
};
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
logger.error(error);
sendMailTransport.sendMail(mailOptions, (error, info) => {
if (error) {
logger.error(error);
res.json({ formSubmit: true, errors: 'error in SMTP Server' });
} else {
logger.info(info);
res.json({ formSubmit: true, errors: null });
}
});
} else {
logger.info(info);
res.json({ formSubmit: true, errors: null });
}
});
if (!config.useSendMail || sendMailDefault) {
smtpTransport.sendMail(mailOptions, (error, info) => {
if (error) {
logger.error(`error in smtp server ${error}`);
res.json({ formSubmit: true, errors: 'error in SMTP Server' });
} else {
logger.info(info);
res.json({ formSubmit: true, errors: null });
}
});
} else {
sendMailTransport.sendMail(mailOptions, (error, info) => {
if (error) {
logger.error(`error in SendMail ${error}`);
res.json({ formSubmit: true, errors: 'error in SendMail' });
} else {
logger.info(info);
res.json({ formSubmit: true, errors: null });
}
});
}
})
.catch(errorCodes => {
// invalid
console.log('error in captcha', errorCodes);
logger.error(errorCodes);
logger.error(`error in captcha ${errorCodes}`);
res.json({ formSubmit: false, errors: recaptcha.translateErrors(errorCodes) });
});
});


+ 1
- 1
themes/redbrick-theme

@@ -1 +1 @@
Subproject commit b6381b1759ed6ed62b54314e031df1ab23507c44
Subproject commit 304da269629eb5facf3d9b7e0e55fcb010846c91

Загрузка…
Отмена
Сохранить