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

var swig = require('swig');
4
//var AWS = require('aws-sdk');
mntmn's avatar
mntmn committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

module.exports = {
  sendMail: (to_email, subject, body, options) => {

    if (!options) {
      options = {};
    }

    // FIXME
    const teamname = options.teamname || "My Open Spacedeck"
    const from = teamname + ' <support@example.org>';

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

32
    //if (process.env.NODE_ENV === 'development') {
mntmn's avatar
mntmn committed
33
      console.log("Email: to " + to_email + " in production.\nreply_to: " + reply_to + "\nsubject: " + subject + "\nbody: \n" + htmlText + "\n\n plaintext:\n" + plaintext);
34
    /*} else {
mntmn's avatar
mntmn committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
      AWS.config.update({region: 'eu-west-1'});
      var ses = new AWS.SES();

      ses.sendEmail( {
        Source: from,
        Destination: { ToAddresses: [to_email] },
        ReplyToAddresses: reply_to,
        Message: {
          Subject: {
            Data: subject
          },
          Body: {
            Text: {
              Data: plaintext,
            },
            Html: {
              Data: htmlText
            }
          }
        }
      }, function(err, data) {
56
        if (err) console.error("Error sending email:", err);
mntmn's avatar
mntmn committed
57
58
        else console.log("Email sent.");
      });
59
    }*/
mntmn's avatar
mntmn committed
60
61
  }
};