What is mailcomposer?
The mailcomposer npm package is a tool for creating email messages with various features such as attachments, HTML content, and custom headers. It is useful for generating email content that can be sent using an SMTP client or other email transport methods.
What are mailcomposer's main functionalities?
Basic Email Composition
This feature allows you to compose a basic email with a sender, receiver, subject, and plain text content.
const MailComposer = require('mailcomposer');
const mail = new MailComposer({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world!'
});
mail.build((err, message) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Email message:', message.toString());
}
});
HTML Email Composition
This feature allows you to compose an email with HTML content, which can include rich text formatting and images.
const MailComposer = require('mailcomposer');
const mail = new MailComposer({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
html: '<h1>Hello world!</h1>'
});
mail.build((err, message) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Email message:', message.toString());
}
});
Attachments
This feature allows you to add attachments to your email, such as files or images.
const MailComposer = require('mailcomposer');
const mail = new MailComposer({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world!',
attachments: [
{
filename: 'text.txt',
content: 'Hello world!'
}
]
});
mail.build((err, message) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Email message:', message.toString());
}
});
Custom Headers
This feature allows you to add custom headers to your email, which can be useful for various purposes such as tracking or custom processing.
const MailComposer = require('mailcomposer');
const mail = new MailComposer({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world!',
headers: {
'X-Custom-Header': 'Custom header value'
}
});
mail.build((err, message) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Email message:', message.toString());
}
});
Other packages similar to mailcomposer
nodemailer
Nodemailer is a module for Node.js applications to allow easy email sending. It supports various transport methods, including SMTP, and provides a rich set of features for composing and sending emails. Compared to mailcomposer, Nodemailer is more comprehensive as it includes both email composition and transport functionalities.
emailjs
EmailJS is a library for sending emails from JavaScript applications. It supports SMTP and provides features for composing and sending emails. While it offers similar functionalities to mailcomposer, it also includes built-in transport methods, making it a more complete solution for email handling.
sendmail
Sendmail is a simple way to send emails from your Node.js application without needing a dedicated SMTP server. It is lightweight and easy to use, but it lacks some of the advanced composition features provided by mailcomposer.
mailcomposer
This is going to be a module for generating e-mail messages that can be
streamed to SMTP etc.
See autogenerated docs here.
Installation
Install through NPM
npm install mailcomposer
Usage
Envelope
Envelope is emitted with 'envelope' event and it has an object as a param
that includes a from address and a list of to addresses suitable for forwarding
to a SMTP server.
mailcomposer.on("envelope", function(envelope){
console.log(envelope);
// {from:"sender@example.com", to:["receiver@example.com"]}
});
Running tests
Tests are run with nodeunit
Run
npm test
or alternatively
node run_tests.js
License
MIT