<reponame>massa/eric-api
const pug = require('pug')
const emailData = require('../config/template').email
const email = require('emailjs/email')

const server = email.server.connect({
  user: process.env.MAIL_USER,
  password: <PASSWORD>,
  host: process.env.MAIL_HOST,
  port: process.env.MAIL_PORT,
  ssl: true
})

const sendMail = (options) => {
  const data = Object.assign({_email: emailData}, options.template.data)
  const mailPath = `${process.cwd()}/templates/mails/${options.template.path}.pug`
  const htmlstream = pug.renderFile(mailPath, data)
  const message = {
    from: '<NAME> ⚜️ 🔺 <<EMAIL>>',
    to: (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') ? '<EMAIL>' : (options.to),
    bcc: '<EMAIL>',
    subject: options.subject,
    attachment:
    [
      {data: htmlstream, alternative: true}
    ]
  }

  server.send(message, (error, info) => {
    if (error) {
      console.log(JSON.stringify(error))
      return {success: false, error: error}
    }
    return {success: true, info: info}
  })
}

module.exports = {
  sendMail
}
