imicros-mails
v0.0.4
Published
Moleculer service for sending mails via smtp
Downloads
3
Readme
imicros-mails
Moleculer service for sending mails via smtp
Requires additional running services: imicros-minio and imicros-keys
Installation
$ npm install imicros-mails --save
Usage
Setup service
"use strict";
const { Mails } = require("imicros-mails");
const { AclMixin } = require("imicros-acl");
const { MinioMixin, SecretsMixin } = require("imicros-minio");
module.exports = {
name: "mails",
mixins: [Mails, MinioMixin({ service: "minio" }), AclMixin, SecretsMixin({ service: "keys" })],
/**
* Service settings
*/
settings: {},
/**
* Service dependencies
*/
dependencies: ["minio","keys"],
/**
* Service created lifecycle event handler
*/
created() {},
/**
* Service started lifecycle event handler
*/
started() {},
/**
* Service stopped lifecycle event handler
*/
stopped() {}
};
Usage service
- smtp { account, settings } => account
- verify { account } => { test, err}
- send { account, message } => result
Verify connection
let params = {
account: {
smtp: { // connection settings: refer to nodemailer
host: "mail.myhost.com",
port: "465",
secure: true,
requireTLS: true
},
auth: {
user: "[email protected]",
pass: {
_encrypt: {
value: account.pass
}
}
}
}
};
await broker.call("mails.verify", params, opts).then(res => {
console.log(res.account); // "test"
});
Save connection
let params = {
account: "test",
settings: {
// connection settings: refer to nodemailer documentation
smtp: {
host: "mail.myhost.com",
port: "465",
secure: true,
requireTLS: true
},
auth: {
user: "[email protected]",
pass: {
_encrypt: {
value: account.pass
}
}
}
}
};
await broker.call("mails.smtp", params, opts).then(res => {
console.log(res.account); // "test"
});
Send mail
const fs = require("fs");
let params = {
account: "test",
// message object: refer to nodemailer documentation
message: {
// Comma separated list of recipients
to: "Max Mustermann <[email protected]>",
// Subject of the message
subject: "Nodemailer is unicode friendly ✔",
// plaintext body
text: "Hello to myself!",
// HTML body
html:
"<p><b>Hello</b> to myself <img src=\"cid:[email protected]\"/></p>" +
"<p>Here's the imicros logo for you as an embedded attachment:<br/><img src=\"cid:[email protected]\"/></p>",
// An array of attachments
attachments: [
// String attachment
{
filename: "notes.txt",
content: "Some notes about this e-mail",
contentType: "text/plain" // optional, would be detected from the filename
},
// Binary Buffer attachment
{
filename: "image.png",
content: Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/" +
"//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U" +
"g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC",
"base64"
),
cid: "[email protected]" // should be as unique as possible
},
// File Stream attachment
{
filename: "logo imicros ✔.png",
content: fs.createReadStream("assets/imicros.png"),
cid: "[email protected]" // should be as unique as possible
}
]
}
};
let res = await broker.call("mails.send", params, opts);
console.log(res);
/*
{ accepted: [ '[email protected]' ],
rejected: [],
envelopeTime: 60,
messageTime: 355,
messageSize: 17051,
response:
'250 Accepted [STATUS=new MSGID=XSGpENwpOWUvmNbGXSGpEdEoY2VsWvv3AAAAAS1yzYsg0f4g5G02QvJAu0A]',
envelope:
{ from: '[email protected]',
to: [ '[email protected]' ] },
messageId: '<[email protected]>' }
*/