Commit d874b5b8 authored by Jarrod's avatar Jarrod 💬

Postal for email (started)

parent 38676030
const render = require('./render-template')
if (typeof sails === 'undefined') {
sails = { config: { environment: 'production', custom: { site: { name: 'test cmd', emailFrom: 'root@zhdict.net' } } } }
}
module.exports = async function sendWithPostal(template, to, args) {
const config = sails.config.custom
const axios = require('axios')
let html = template ? await render(`${template}`, args) : ''
let text = args.text
var data = {
to: [to],
from: args.from || config.site.emailFrom,
subject: args.subject || `Notification from ${config.site.name}`,
plain_body: text,
html_body: html ? html : text ? text.replace(/\n/g, '<br/>') : ''
};
if (sails.config.environment !== 'production') {
console.log(`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`)
console.log(`Not sending email as not in production mode`)
console.log(`I would have sent the following message:`)
console.log(` To: ${data.to}`)
console.log(` Subject: ${data.subject}`)
console.log(` Message:\n${data.html}`)
return true
}
// Send mail
config.postal = {
apiHost: 'https://postal.chenggong.carrotlabs.net',
apiKey: 'aBZuUSZ7qeElyHJOOJ5PH1Qs'
}
try {
const res = await axios.post(
config.postal.apiHost + '/api/v1/send/message',
data,
{
headers: {
'X-Server-API-Key': config.postal.apiKey
}
}
)
console.log('Sent email: ', res.status)
console.log('Sent email: ', res.statusCode)
console.log('Sent email: ', res.data)
return true
} catch (err) {
console.error('Failed to send email', err.stack)
return false
}
}
\ No newline at end of file
module.exports = async function renderEmailTemplate(template, args = { }) {
const config = sails.config.custom
// Determine appropriate email layout and template to use.
const path = require('path')
const emailTemplatePath = path.join('email/', template);
let layout = path.relative(path.dirname(emailTemplatePath), path.resolve('layouts/', args.layout || 'layout'));
// Compile HTML template.
// > Note that we set the layout, provide access to core `url` package (for
// > building links and image srcs, etc.), and also provide access to core
// > `util` package (for dumping debug data in internal emails).
var htmlEmailContents = await sails.renderView(
emailTemplatePath,
_.extend({ layout }, { data: args, site: config.site, title: args.subject || '' }, args.templateData)
)
.intercept((err)=>{
err.message =
'Could not compile view template.\n'+
'(Usually, this means the provided data is invalid, or missing a piece.)\n'+
'Details:\n'+
err.message;
return err;
});
return htmlEmailContents
}
\ No newline at end of file
const render = async function(template, args = { }) {
const config = sails.config.custom
// Determine appropriate email layout and template to use.
const path = require('path')
const emailTemplatePath = path.join('email/', template);
let layout = path.relative(path.dirname(emailTemplatePath), path.resolve('layouts/', args.layout || 'layout'));
// Compile HTML template.
// > Note that we set the layout, provide access to core `url` package (for
// > building links and image srcs, etc.), and also provide access to core
// > `util` package (for dumping debug data in internal emails).
var htmlEmailContents = await sails.renderView(
emailTemplatePath,
_.extend({ layout }, { data: args, site: config.site, title: args.subject || '' }, args.templateData)
)
.intercept((err)=>{
err.message =
'Could not compile view template.\n'+
'(Usually, this means the provided data is invalid, or missing a piece.)\n'+
'Details:\n'+
err.message;
return err;
});
return htmlEmailContents
async function send (template, to, args) {
// return require('./lib/postal')(template, to, args)
return sendWithMailgun(template, to, args)
}
async function send (template, to, args) {
async function sendWithMailgun (template, to, args) {
const config = sails.config.custom
const render = require('./lib/render-template')
const mailgun = require('mailgun-js')( config.mailgun )
return new Promise(async (resolve, reject) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment