cloudron-e2e-test/mailer.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-07-23 19:02:16 +00:00
'use strict';
var assert = require('assert'),
debug = require('debug')('e2e:mailer'),
fs = require('fs'),
path = require('path'),
2015-07-24 02:10:04 +00:00
postmark = require('postmark')(process.env.POSTMARK_API_KEY_TOOLS),
2015-07-23 19:02:16 +00:00
util = require('util');
exports = module.exports = {
2016-05-19 00:46:42 +00:00
sendMailToCloudronUser: sendMailToCloudronUser,
2015-07-23 19:02:16 +00:00
sendEndToEndTestResult: sendEndToEndTestResult
};
function send(options, callback) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof options.to, 'string');
assert.strictEqual(typeof options.subject, 'string');
assert.strictEqual(typeof options.text, 'string');
2016-06-24 16:43:53 +00:00
debug('Sending email from %s to %s with subject "%s".', options.from, options.to, options.subject);
2015-07-23 19:02:16 +00:00
postmark.send({
'From': options.from || 'no-reply@cloudron.io',
'To': options.to,
'Bcc': options.bcc,
'Subject': options.subject,
'TextBody': options.text,
'HtmlBody': options.html,
'Tag': 'Important',
'Attachments': options.attachments || []
2015-07-23 19:02:16 +00:00
}, function (error, success) {
if (error) {
console.error('Unable to send via postmark: ', error);
return callback(error);
}
callback();
});
}
function sendEndToEndTestResult(topic, versionInfo, stdout, stderr, attachmentFolder, callback) {
2015-07-23 19:02:16 +00:00
debug('Sending e2e test result for %s', topic);
2016-09-21 20:35:53 +00:00
debug('Creating attachements from folder ', attachmentFolder);
fs.readdir(attachmentFolder, function (error, files) {
var attachments = [];
2015-07-23 19:02:16 +00:00
if (!error) {
attachments = files.filter(function (file) {
return fs.statSync(path.join(attachmentFolder, file)).isFile();
}).map(function (file) {
var filePath = path.join(attachmentFolder, file);
return {
Content: fs.readFileSync(filePath).toString('base64'),
Name: file,
ContentType: 'text/plain'
};
});
2016-09-21 20:35:53 +00:00
} else {
debug('Error reading attachment directory: ', error);
}
var mailOptions = {
to: 'test@cloudron.io',
subject: util.format('E2E test results for %s', topic),
text: versionInfo + '\n\nstdout\n------\n' + stdout.toString('utf8') + '\n\nstderr\n------\n' + stderr.toString('utf8') + '\n\n',
attachments: attachments
};
send(mailOptions, callback);
});
2015-07-23 19:02:16 +00:00
}
2016-05-19 00:46:42 +00:00
function sendMailToCloudronUser(email, callback) {
debug('Sending test mail to %s', email);
var mailOptions = {
to: email,
2016-06-24 16:02:48 +00:00
subject: 'Hi from e2e test - ' + email.split('@')[1],
2016-06-24 16:46:08 +00:00
text: 'This release depends on you!' // match this with cloudron.sendMail
2016-05-19 00:46:42 +00:00
};
send(mailOptions, callback);
}