Generate RFC822 formatted e-mail messages that can be streamed to SMTP or file.
mailcomposer is exposed as a submodule of Nodemailer
npm install nodemailer --save
const MailComposer = require("nodemailer/lib/mail-composer");
var mail = new MailComposer(mailOptions);
Where mailOptions
is an object that defines the components of the message, see below
To create a stream that outputs a raw rfc822 message from the defined input, use createReadStream()
var mail = new MailComposer({from: '...', ...});
var stream = mail.compile().createReadStream();
stream.pipe(process.stdout);
To generate the message and return it with a callback use build()
var mail = new MailComposer({from: '...', ...});
mail.compile().build(function(err, message){
process.stdout.write(message);
});
The following are the possible fields of an e-mail message:
'sender@server.com'
or formatted 'Sender Name <sender@server.com>'
, see here for detailstext
and html
. Latest watches have no problems rendering text/html content so watchHtml is most probably never seen by the recipienttext
and html
. Make sure it is a full and valid AMP4EMAIL document, otherwise the displaying email client falls back to html
and ignores the amp
part. See this blogpost for sending and renderingtext
and html
. Event method
attribute defaults to ‘PUBLISH’ or define it yourself: {method: 'REQUEST', content: iCalString}
. This value is added as an additional alternative to html or text. Only utf-8 content is allowed\r\n
for windows/network newlines, \n
for unix newlines or do not set to keep as isAll text fields (e-mail addresses, plaintext body, html body) use UTF-8 as the encoding. Attachments are streamed as binary.
Attachment object consists of the following properties:
false
, otherwise a filename is generated automaticallycid
sets the default contentDisposition
to 'inline'
and moves the attachment into a multipart/related mime node, so use it only if you actually want to use this attachment as an embedded imagecontent
is string, then encodes the content to a Buffer using the specified encoding. Example values: base64
, hex
, binary
etc. Useful if you want to use binary attachments in a JSON formatted e-mail objectfilename
propertycontentType
property. Example values: quoted-printable
, base64
{'X-My-Header': 'value'}
path
or content
)Attachments can be added as many as you want.
var mailOptions = {
...
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
},
{ // binary buffer as an attachment
filename: 'text2.txt',
content: new Buffer('hello world!','utf-8')
},
{ // file on disk as an attachment
filename: 'text3.txt',
path: '/path/to/file.txt' // stream this file
},
{ // filename and content type is derived from path
path: '/path/to/file.txt'
},
{ // stream as an attachment
filename: 'text4.txt',
content: fs.createReadStream('file.txt')
},
{ // define custom content type for the attachment
filename: 'text.bin',
content: 'hello world!',
contentType: 'text/plain'
},
{ // use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE'
},
{ // encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},
{ // data uri as an attachment
path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
}
]
}
In addition to text and HTML, any kind of data can be inserted as an alternative content of the main body - for example a word processing document with the same text as in the HTML field. It is the job of the e-mail client to select and show the best fitting alternative to the reader. Usually this field is used for calendar events and such.
Alternative objects use the same options as attachment objects. The difference between an attachment and an alternative is the fact that attachments are placed into multipart/mixed or multipart/related parts of the message white alternatives are placed into multipart/alternative part.
Usage example:
var mailOptions = {
...
html: '<b>Hello world!</b>',
alternatives: [
{
contentType: 'text/x-web-markdown',
content: '**Hello world!**'
}
]
}
Alternatives can be added as many as you want.
All the e-mail addresses can be plain e-mail addresses
foobar@example.com
or with formatted name (includes unicode support)
"Ноде Майлер" <foobar@example.com>
Notice that all address fields (even
from
) are comma separated lists, so if you want to use a comma in the name part, make sure you enclose the name in double quotes:"Майлер, Ноде" <foobar@example.com>
or as an address object (in this case you do not need to worry about the formatting, no need to use quotes etc.)
{
name: 'Майлер, Ноде',
address: 'foobar@example.com'
}
All address fields accept comma separated list of e-mails or an array of e-mails or an array of comma separated list of e-mails or address objects - use it as you like. Formatting can be mixed.
...,
to: 'foobar@example.com, "Ноде Майлер" <bar@example.com>, "Name, User" <baz@example.com>',
cc: ['foobar@example.com', '"Ноде Майлер" <bar@example.com>, "Name, User" <baz@example.com>'],
bcc: ['foobar@example.com', {name: 'Майлер, Ноде', address: 'foobar@example.com'}]
...
You can even use unicode domains, these are automatically converted to punycode
'"Unicode Domain" <info@müriaad-polüteism.info>'
SMTP envelope is usually auto generated from from
, to
, cc
and bcc
fields but
if for some reason you want to specify it yourself, you can do it with envelope
property.
envelope
is an object with the following params: from
, to
, cc
and bcc
just like
with regular mail options. You can also use the regular address format, unicode domains etc.
mailOptions = {
...,
from: 'mailer@kreata.ee',
to: 'daemon@kreata.ee',
envelope: {
from: 'Daemon <deamon@kreata.ee>',
to: 'mailer@kreata.ee, Mailer <mailer2@kreata.ee>'
}
}
Not all transports can use the
envelope
object, for example SES ignores it and uses the data from the From:, To: etc. headers.
Attachments can be used as embedded images in the HTML body. To use this feature, you need to set additional property of the attachment - cid
(unique identifier of the file) which is a reference to the attachment file. The same cid
value must be used as the image URL in HTML (using cid:
as the URL protocol, see example below).
NB! the cid value should be as unique as possible!
var mailOptions = {
...
html: 'Embedded image: <img src="cid:unique@kreata.ee"/>',
attachments: [{
filename: 'image.png',
path: '/path/to/file',
cid: 'unique@kreata.ee' //same cid value as in the html img src
}]
}
By default, MailComposer drops the BCC header when built. If you need to BCC header to be shown when built, you’ll need to include the keepBcc
option like so.
var mailOptions = {
...
bcc: 'bcc@example.com'
}
var mail = new MailComposer(mailOptions).compile()
mail.keepBcc = true
mail.build(function(err, message){
process.stdout.write(message);
});
MIT