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

Hirunatan's avatar
Hirunatan committed
3
4
const config = require('config');
const nodemailer = require('nodemailer');
mntmn's avatar
mntmn committed
5
6
7
8
9
10
11

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

Hirunatan's avatar
Hirunatan committed
12
13
    const teamname = options.teamname || config.get('team_name');
    const from = teamname + ' <' + config.get('contact_email') + '>';
mntmn's avatar
mntmn committed
14
15
16
17
18
19
20
21
22
23
24

    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";
    }

Hirunatan's avatar
Hirunatan committed
25
26
    if (config.get('mail_provider') === 'console') {

27
      console.log("Email: to " + to_email + " in production.\nreply_to: " + reply_to + "\nsubject: " + subject + "\nbody: \n" + plaintext + "\n\n plaintext:\n" + plaintext);
Hirunatan's avatar
Hirunatan committed
28
29

    } else if (config.get('mail_provider') === 'smtp') {
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      let transporter;
      if (config.has('mail_smtp_user')) {
        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'),
            }
        });
      } else {
        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'),
        });
      }
Hirunatan's avatar
Hirunatan committed
50
51
52
53
54
55

      transporter.sendMail({
        from: from,
        replyTo: reply_to,
        to: to_email,
        subject: subject,
56
        text: plaintext
Hirunatan's avatar
Hirunatan committed
57
58
59
60
61
62
63
64
65
      }, function(err, info) {
        if (err) {
          console.error("Error sending email:", err);
        } else {
          console.log("Email sent.");
        }
      });

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