summaryrefslogtreecommitdiff
path: root/scripts/gerrit/cherry-pick_automation/emailClient.js
blob: 409ab2950b527f9a675c76710d5a7f026621c134 (plain)
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

exports.id = "emailClient";
const nodemailer = require("nodemailer");
const sesTransport = require("nodemailer-ses-transport");

const config = require("./config.json");
const Logger = require("./logger");
const logger = new Logger();

// Set default values with the config file, but prefer environment variable.
function envOrConfig(ID) {
  return process.env[ID] || config[ID];
}

let senderAddress = envOrConfig("EMAIL_SENDER");

let SESCREDENTIALS = {
  accessKeyId: envOrConfig("SES_ACCESS_KEY_ID"),
  secretAccessKey: envOrConfig("SES_SECRET_ACCESS_KEY")
};

exports.genericSendEmail = function (to, subject, htmlbody, textbody) {
  // create reusable transporter
  let transporter = nodemailer.createTransport(sesTransport({
    accessKeyId: SESCREDENTIALS.accessKeyId,
    secretAccessKey: SESCREDENTIALS.secretAccessKey,
    rateLimit: 5
  }));

  // setup email data with unicode symbols
  let mailOptions = {
    from: senderAddress, // sender address
    to: to, // list of receivers
    subject: subject, // Subject line
    text: textbody, // plain text body
    html: htmlbody // html body
  };

  // send mail with defined transport object
  transporter.sendMail(mailOptions).catch((err) => {
    logger.log(`Error sending email: ${err}`, "error", "MAILER");
  });
};