mailer.js 1.77 KB
Newer Older
mntmn's avatar
mntmn committed
1
2
'use strict';

Hirunatan's avatar
Hirunatan committed
3
4
5
const config = require('config');
const nodemailer = require('nodemailer');
const swig = require('swig');
6
//var AWS = require('aws-sdk');
mntmn's avatar
mntmn committed
7
8
9
10
11
12
13

module.exports = {
  sendMail: (to_email, subject, body, options) => {
    if (!options) {
      options = {};
    }

Hirunatan's avatar
Hirunatan committed
14
15
    const teamname = options.teamname || config.get('team_name');
    const from = teamname + ' <' + config.get('contact_email') + '>';
mntmn's avatar
mntmn committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

    let reply_to = [from];
    if (options.reply_to) {
      reply_to = [options.reply_to];
    }

    let plaintext = body;
    if (options.action && options.action.link) {
      plaintext+="\n"+options.action.link+"\n\n";
    }

    const htmlText = swig.renderFile('./views/emails/action.html', {
      text: body.replace(/(?:\n)/g, '<br />'),
      options: options
    });

Hirunatan's avatar
Hirunatan committed
32
33
    if (config.get('mail_provider') === 'console') {

mntmn's avatar
mntmn committed
34
      console.log("Email: to " + to_email + " in production.\nreply_to: " + reply_to + "\nsubject: " + subject + "\nbody: \n" + htmlText + "\n\n plaintext:\n" + plaintext);
Hirunatan's avatar
Hirunatan committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

    } else if (config.get('mail_provider') === 'smtp') {

      const transporter = nodemailer.createTransport({
        host: config.get('mail_smtp_host'),
        port: config.get('mail_smtp_port'),
        secure: config.get('mail_smtp_secure'),
        requireTLS: config.get('mail_smtp_require_tls'),
        auth: {
          user: config.get('mail_smtp_user'),
          pass: config.get('mail_smtp_pass'),
        }
      });

      transporter.sendMail({
        from: from,
        replyTo: reply_to,
        to: to_email,
        subject: subject,
        text: plaintext,
        html: htmlText,
      }, function(err, info) {
        if (err) {
          console.error("Error sending email:", err);
        } else {
          console.log("Email sent.");
        }
      });

    }
mntmn's avatar
mntmn committed
65
66
  }
};