diff --git a/config/config.js b/config/config.js index a42fed1..8a178b5 100644 --- a/config/config.js +++ b/config/config.js @@ -3,11 +3,14 @@ module.exports = { // APP port: process.env.PORT || 3001, environment: process.env.NODE_ENV || 'development', + // DATABASE dbURL: process.env.DATABASE_URL, + // JWT jwtSecret: process.env.JWT_SECRET, saltingRounds: 10, + // NODEMAILER smtpHost: process.env.SMTP_HOST, smtpPort: process.env.SMTP_PORT, diff --git a/helpers/logSmtpStatus.js b/helpers/logSmtpStatus.js index 18ba2a9..a65dd02 100644 --- a/helpers/logSmtpStatus.js +++ b/helpers/logSmtpStatus.js @@ -1,4 +1,4 @@ -const transporter = require('./smtpConfig'); +const transporter = require('../services/email/transporter'); function logSmtpStatus() { transporter.verify(function (error, success) { diff --git a/helpers/smtpConfig.js b/helpers/smtpConfig.js deleted file mode 100644 index cbd20df..0000000 --- a/helpers/smtpConfig.js +++ /dev/null @@ -1,25 +0,0 @@ -const nodemailer = require('nodemailer'); - -const { - smtpPool, - smtpHost, - smtpPort, - smtpUsername, - smtpPassword, - smtpSecure, - smtpRequireTLS, -} = require('../config/config'); - -const smtpTransporter = nodemailer.createTransport({ - pool: smtpPool, - host: smtpHost, - port: smtpPort, - requireTLS: smtpRequireTLS, - secure: smtpSecure, - auth: { - user: smtpUsername, - pass: smtpPassword, - }, -}); - -module.exports = smtpTransporter; diff --git a/services/email/sendEmail.js b/services/email/sendEmail.js new file mode 100644 index 0000000..8b59149 --- /dev/null +++ b/services/email/sendEmail.js @@ -0,0 +1,15 @@ +const transporter = require('./transporter'); + +transporter.sendMail(message, (error, info) => { + if (error) { + console.log('Error occurred'); + console.log(error.message); + return process.exit(1); + } + + console.log('Message sent successfully!'); + console.log(nodemailer.getTestMessageUrl(info)); + + // only needed when using pooled connections + transporter.close(); +}); diff --git a/services/email/emailConfigs.js b/services/email/tempConfig.js similarity index 86% rename from services/email/emailConfigs.js rename to services/email/tempConfig.js index 68a78fe..ee7b5a7 100644 --- a/services/email/emailConfigs.js +++ b/services/email/tempConfig.js @@ -2,7 +2,7 @@ module.exports = { appURL: 'https://meetingplanner.com', sender: 'no-reply@ruihildt.xyz', - receiver: 'ruihildt@armada.digital', + receiver: 'rui@armada.digital', senderUsername: 'Rui', meetingTitle: 'Simple Title meeting', diff --git a/services/email/templates/meetingInvite.js b/services/email/templates/meetingInvite.js index d0bbc22..1364fd9 100644 --- a/services/email/templates/meetingInvite.js +++ b/services/email/templates/meetingInvite.js @@ -7,7 +7,7 @@ const { meetingID, signature, appURL, -} = require('../emailConfigs'); +} = require('../tempConfig'); let meetingInvite = { from: sender, diff --git a/services/email/transporter.js b/services/email/transporter.js new file mode 100644 index 0000000..fc0a8f0 --- /dev/null +++ b/services/email/transporter.js @@ -0,0 +1,41 @@ +const nodemailer = require('nodemailer'); + +const { + smtpPool, + smtpHost, + smtpPort, + smtpUsername, + smtpPassword, + smtpSecure, + smtpRequireTLS, +} = require('../../config/config'); + +let mailConfig; + +if (process.env.NODE_ENV === 'production') { + // all emails are delivered to destination + mailConfig = { + pool: smtpPool, + host: smtpHost, + port: smtpPort, + requireTLS: smtpRequireTLS, + secure: smtpSecure, + auth: { + user: smtpUsername, + pass: smtpPassword, + }, + }; +} else { + // all emails are catched by ethereal.email + // Check test emails: https://ethereal.email/login + mailConfig = { + host: 'smtp.ethereal.email', + port: 587, + auth: { + user: 'lemuel.wunsch91@ethereal.email', + pass: 'Y9Vs51v27Q9X9feJgN', + }, + }; +} + +module.exports = nodemailer.createTransport(mailConfig);