2018-01-25 15:03:03 -06:00
/* eslint-disable no-console */
2017-03-24 10:35:19 +09:00
import VulcanEmail from '../namespace.js' ;
2016-05-20 09:59:01 +09:00
import Juice from 'juice' ;
import htmlToText from 'html-to-text' ;
import Handlebars from 'handlebars' ;
2018-10-20 16:41:02 +09:00
import { Utils , getSetting , registerSetting , runQuery , Strings , getString } from 'meteor/vulcan:lib' ; // import from vulcan:lib because vulcan:core is not loaded yet
/ *
Get intl string . Usage : { { _ _ "posts.create" } }
* /
Handlebars . registerHelper ( '__' , function ( id , context ) {
const s = getString ( { id , locale : context . data . root . locale } ) ;
return new Handlebars . SafeString ( s ) ;
} ) ;
/ *
Get intl string , accepts a second variables argument . Usage : { { _ _ "posts.create" postVariables } }
* /
Handlebars . registerHelper ( '___' , function ( id , variables , context ) {
const s = getString ( { id , variables , locale : context . data . root . locale } ) ;
return new Handlebars . SafeString ( s ) ;
} ) ;
2017-09-22 12:24:15 +02:00
registerSetting ( 'secondaryColor' , '#444444' ) ;
registerSetting ( 'accentColor' , '#DD3416' ) ;
registerSetting ( 'title' , 'My App' ) ;
registerSetting ( 'tagline' ) ;
registerSetting ( 'emailFooter' ) ;
registerSetting ( 'logoUrl' ) ;
registerSetting ( 'logoHeight' ) ;
registerSetting ( 'logoWidth' ) ;
registerSetting ( 'defaultEmail' , 'noreply@example.com' ) ;
registerSetting ( 'title' , 'Vulcan' ) ;
registerSetting ( 'enableDevelopmentEmails' , false ) ;
2016-05-20 09:59:01 +09:00
2017-03-24 10:35:19 +09:00
VulcanEmail . templates = { } ;
2015-12-14 12:43:45 +09:00
2017-03-24 10:35:19 +09:00
VulcanEmail . addTemplates = templates => {
_ . extend ( VulcanEmail . templates , templates ) ;
2015-12-14 12:43:45 +09:00
} ;
2018-09-29 17:12:58 +09:00
VulcanEmail . getTemplate = templateName => {
if ( ! VulcanEmail . templates [ templateName ] ) {
throw new Error ( ` Couldn't find email template named “ ${ templateName } ” ` ) ;
}
return Handlebars . compile ( VulcanEmail . templates [ templateName ] , { noEscape : true , strict : true } ) ;
}
2015-08-03 15:42:28 +09:00
2018-06-11 10:53:39 +09:00
VulcanEmail . buildTemplate = ( htmlContent , data = { } , locale ) => {
2017-02-16 10:04:00 +01:00
const emailProperties = {
2016-12-12 15:00:56 +09:00
secondaryColor : getSetting ( 'secondaryColor' , '#444444' ) ,
accentColor : getSetting ( 'accentColor' , '#DD3416' ) ,
2017-03-24 10:35:19 +09:00
siteName : getSetting ( 'title' , 'My App' ) ,
2016-12-12 15:00:56 +09:00
tagline : getSetting ( 'tagline' ) ,
2016-12-12 11:34:28 +09:00
siteUrl : Utils . getSiteUrl ( ) ,
2015-04-22 07:50:11 +09:00
body : htmlContent ,
unsubscribe : '' ,
2018-09-16 11:48:38 +09:00
accountLink : Utils . getSiteUrl ( ) + 'account' ,
2016-12-12 15:00:56 +09:00
footer : getSetting ( 'emailFooter' ) ,
logoUrl : getSetting ( 'logoUrl' ) ,
logoHeight : getSetting ( 'logoHeight' ) ,
logoWidth : getSetting ( 'logoWidth' ) ,
2018-06-11 10:53:39 +09:00
... data ,
_ _ : Strings [ locale ] ,
2015-05-01 18:22:00 +02:00
} ;
2015-04-22 07:50:11 +09:00
2018-09-12 11:59:00 +09:00
const emailHTML = VulcanEmail . getTemplate ( 'wrapper' ) ( emailProperties ) ;
2018-09-16 11:48:38 +09:00
const inlinedHTML = Juice ( emailHTML , { preserveMediaQueries : true } ) ;
const doctype =
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' ;
2015-04-22 07:50:11 +09:00
2018-09-16 11:48:38 +09:00
return doctype + inlinedHTML ;
2015-05-01 18:22:00 +02:00
} ;
2015-04-22 07:50:11 +09:00
2017-08-28 22:27:07 +09:00
VulcanEmail . generateTextVersion = html => {
return htmlToText . fromString ( html , {
2018-09-16 11:48:38 +09:00
wordwrap : 130 ,
2017-08-28 22:27:07 +09:00
} ) ;
2018-09-16 11:48:38 +09:00
} ;
2017-08-28 22:27:07 +09:00
2018-09-26 12:24:48 -07:00
VulcanEmail . send = ( to , subject , html , text , throwErrors , cc , bcc , replyTo , headers ) => {
2015-04-22 07:50:11 +09:00
// TODO: limit who can send emails
// TODO: fix this error: Error: getaddrinfo ENOTFOUND
2018-09-16 11:48:38 +09:00
if ( typeof to === 'object' ) {
// eslint-disable-next-line no-redeclare
2018-09-26 12:24:48 -07:00
var { to , cc , bcc , replyTo , subject , html , text , throwErrors , headers } = to ;
2018-09-14 12:36:19 +02:00
}
2015-04-22 07:50:11 +09:00
2017-02-16 10:04:00 +01:00
const from = getSetting ( 'defaultEmail' , 'noreply@example.com' ) ;
2017-05-23 09:44:22 +09:00
const siteName = getSetting ( 'title' , 'Vulcan' ) ;
2018-09-27 03:24:11 -07:00
subject = subject || '[' + siteName + ']' ;
2015-04-22 07:50:11 +09:00
2018-09-16 11:48:38 +09:00
if ( typeof text === 'undefined' ) {
2015-04-22 07:50:11 +09:00
// Auto-generate text version if it doesn't exist. Has bugs, but should be good enough.
2017-08-28 22:27:07 +09:00
text = VulcanEmail . generateTextVersion ( html ) ;
2015-04-22 07:50:11 +09:00
}
2017-02-16 10:04:00 +01:00
const email = {
2015-04-22 07:50:11 +09:00
from : from ,
to : to ,
2018-09-10 21:13:50 +02:00
cc : cc ,
bcc : bcc ,
replyTo : replyTo ,
2015-04-22 07:50:11 +09:00
subject : subject ,
2018-09-26 12:24:48 -07:00
headers : headers ,
2015-04-22 07:50:11 +09:00
text : text ,
2018-09-16 11:48:38 +09:00
html : html ,
2015-05-01 18:22:00 +02:00
} ;
2017-05-23 09:44:22 +09:00
2018-09-26 12:41:02 -07:00
const shouldSendEmail = process . env . NODE _ENV === 'production' || getSetting ( 'enableDevelopmentEmails' , false )
2017-05-23 09:44:22 +09:00
2018-09-26 12:41:02 -07:00
console . log ( ` //////// sending email ${ shouldSendEmail ? '' : ' (simulation)' } … ` ) ; // eslint-disable-line
console . log ( 'from: ' + from ) ; // eslint-disable-line
2018-10-14 18:10:32 +09:00
console . log ( 'to: ' + to ) ; // eslint-disable-line
2018-09-26 12:41:02 -07:00
console . log ( 'cc: ' + cc ) ; // eslint-disable-line
console . log ( 'bcc: ' + bcc ) ; // eslint-disable-line
console . log ( 'replyTo: ' + replyTo ) ; // eslint-disable-line
console . log ( 'headers: ' + JSON . stringify ( headers ) ) ; // eslint-disable-line
if ( shouldSendEmail ) {
2017-05-23 09:44:22 +09:00
try {
Email . send ( email ) ;
} catch ( error ) {
2018-09-16 11:48:38 +09:00
console . log ( '// error while sending email:' ) ; // eslint-disable-line
2017-05-23 09:44:22 +09:00
console . log ( error ) ; // eslint-disable-line
2018-02-16 01:00:33 -05:00
if ( throwErrors ) throw error ;
2017-05-23 09:44:22 +09:00
}
2016-08-26 10:16:01 +09:00
}
2015-04-22 07:50:11 +09:00
return email ;
} ;
2018-05-10 09:22:24 +09:00
VulcanEmail . build = async ( { emailName , variables , locale } ) => {
2017-08-20 17:15:40 +09:00
// execute email's GraphQL query
2017-08-24 13:16:50 +09:00
const email = VulcanEmail . emails [ emailName ] ;
2018-09-16 11:48:38 +09:00
const result = email . query ? await runQuery ( email . query , variables , { locale } ) : { data : { } } ;
2017-08-20 17:15:40 +09:00
// if email has a data() function, merge its return value with results from the query
2018-10-20 16:41:02 +09:00
const data = email . data ? { ... result . data , ... email . data ( { data : result . data , variables , locale } ) } : result . data ;
2017-08-28 22:27:07 +09:00
2018-10-20 16:41:02 +09:00
const subject = typeof email . subject === 'function' ? email . subject ( { data , variables , locale } ) : email . subject ;
2018-05-10 09:22:24 +09:00
2018-06-11 10:53:39 +09:00
data . _ _ = Strings [ locale ] ;
2018-10-20 16:41:02 +09:00
data . locale = locale ;
2018-05-10 09:22:24 +09:00
const html = VulcanEmail . buildTemplate ( VulcanEmail . getTemplate ( email . template ) ( data ) , data , locale ) ;
2017-08-28 22:27:07 +09:00
return { data , subject , html } ;
2018-09-16 11:48:38 +09:00
} ;
2017-08-19 16:13:59 +09:00
2018-09-26 12:24:48 -07:00
VulcanEmail . buildAndSend = async ( { to , cc , bcc , replyTo , emailName , variables , locale = getSetting ( 'locale' ) , headers } ) => {
2018-05-10 09:22:24 +09:00
const email = await VulcanEmail . build ( { to , emailName , variables , locale } ) ;
2018-09-26 12:24:48 -07:00
return VulcanEmail . send ( { to , cc , bcc , replyTo , subject : email . subject , html : email . html , headers } ) ;
2015-05-01 18:22:00 +02:00
} ;
2015-04-22 07:50:11 +09:00
2018-09-13 16:41:47 +02:00
VulcanEmail . buildAndSendHTML = ( to , subject , html ) => VulcanEmail . send ( to , subject , VulcanEmail . buildTemplate ( html ) ) ;