Created
June 15, 2016 15:58
-
-
Save chodorowicz/53d87cab4ffd32ca7d0e0f42525a68fb to your computer and use it in GitHub Desktop.
send emails using email.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const email = require('emailjs'); | |
const _ = require('lodash'); | |
const smtpUser = process.env.SMTP_USER; | |
const smtpPassword = process.env.SMTP_PASSWORD; | |
const smtpHost = process.env.SMTP_HOST; | |
const smtpFrom = process.env.SMTP_FROM; | |
const smtpToSales = process.env.SMTP_TO_SALES; | |
if(!(smtpUser && smtpHost && smtpPassword && smtpFrom)) { | |
return; | |
} | |
function jsonDataToText(data) { | |
return _.reduce(data, (result, item, key) => { | |
return `${result}\n\n ${key}: ${item}`; | |
}, ''); | |
} | |
const server = email.server.connect({ | |
user: smtpUser, | |
password: smtpPassword, | |
host: smtpHost, | |
ssl: true, | |
}); | |
function send(config) { | |
server.send({ | |
text: jsonDataToText(config.data), | |
from: smtpFrom, | |
to: smtpToSales, | |
subject: config.subject | |
}, (err, message) => { console.log(err || message); }); | |
} | |
module.exports = { | |
send, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment