Class MailComposer
- Defined in: mailcomposer.js
| Constructor Attributes | Constructor Name and Description |
|---|---|
|
MailComposer(options)
Costructs a MailComposer object. |
Method Summary
Class Detail
Costructs a MailComposer object. This is a Stream instance so you could pipe the output to a file or send it to network.
Possible options properties are:
- escapeSMTP - convert dots in the beginning of line to double dots
- encoding - forced transport encoding (quoted-printable, base64, 7bit or 8bit)
- keepBcc - include Bcc: field in the message headers (default is false)
Events
- 'envelope' - emits an envelope object with
fromandto(array) addresses. - 'data' - emits a chunk of data
- 'end' - composing the message has ended
- Parameters:
- {Object} options Optional
- Optional options object
Field Detail
Contains a list of attachments
Contains e-mail addresses for the SMTP
Counter for generating unique mime boundaries etc.
Contains all header values
Contains message related values
Method Detail
-
<private> _buildMessageHeaders()
Uses data from the
this._messageobject to build headers -
<private> _composeBody()
Composes the e-mail body based on the previously generated mime tree
Assumes that the linebreak separating headers and contents is already sent
Emits 'data' events
-
<private> _composeEnvelope()
Composes and emits an envelope from the
this._envelopeobject. Needed for the SMTP clientEmitted envelope is int hte following structure:
{ to: "address", from: ["list", "of", "addresses"] }Both properties (
fromandto) are optional and may not exist -
<private> _composeHeader()
Composes a header for the message and emits it with a
'data'eventAlso checks and build a structure for the message (is it a multipart message and does it need a boundary etc.)
By default the message is not a multipart. If the message containes both plaintext and html contents, an alternative block is used. it it containes attachments, a mixed block is used. If both alternative and mixed exist, then alternative resides inside mixed.
-
<private> _composeMessage()
Generate an e-mail from the described info
-
<private> {String} _convertAddresses(value)
Handles a list of parsed e-mail addresses, checks encoding etc.
- Parameters:
- {Array} value
- A list or single e-mail address
{address:'...', name:'...'}
- Returns:
- {String} Comma separated and encoded list of addresses
-
<private> {Object} _createAttachmentComponent(attachment)
Creates a mime tree node for a text component (plaintext, HTML)
- Parameters:
- {Object} attachment
- Attachment info for the component
- Returns:
- {Object} Mime tree node
-
<private> {Object} _createMimeNode()
Creates an empty mime tree node
- Returns:
- {Object} Mime tree node
-
<private> {Object} _createTextComponent(text, contentType)
Creates a mime tree node for a text component (plaintext, HTML)
- Parameters:
- {String} text
- Text contents for the component
- {String} contentType Optional, Default: "text/plain"
- Content type for the text component
- Returns:
- {Object} Mime tree node
-
<private> _emitDataElement(element, callback)
Emits a data event for a text or html body and attachments. If it is a file, stream it
If
this.options.escapeSMTPis true, replace dots in the beginning of a line with double dots - only valid for QP encoding- Parameters:
- {Object} element
- Data element descriptor
- {Function} callback
- Callback function to run when completed
-
<private> {String} _encodeMimeWord(str, encoding, maxlen)
Converts a string to mime word format. If the length is longer than
maxlen, split itIf the string doesn't have any unicode characters return the original string instead
- Parameters:
- {String} str
- String to be encoded
- {String} encoding
- Either Q for Quoted-Printable or B for Base64
- {Number} maxlen Optional
- Optional length of the resulting string, whitespace will be inserted if needed
- Returns:
- {String} Mime-word encoded string (if needed)
-
<private> _flattenMimeTree()
Converts a structured mimetree into an one dimensional array of components. This includes headers and multipart boundaries as strings, textual and attachment contents are.
-
<private> _generateBodyStructure()
Generates the structure (mime tree) of the body. This sets up multipart structure, individual part headers, boundaries etc.
The headers of the root element will be appended to the message headers
-
<private> {String} _generateBoundary()
Generates a boundary for multipart bodies
- Returns:
- {String} Boundary String
-
<private> {String|Array} _getHeader(key)
Gets a header field
- Parameters:
- {String} key
- Key name
- Returns:
- {String|Array} Header field - if several values, then it's an array
-
<private> {String} _getMimeType(filename)
Resolves a mime type for a filename
- Parameters:
- {String} filename
- Filename to check
- Returns:
- {String} Corresponding mime type
-
<private> {String} _handleValue(key, value)
Handles a message object value, converts addresses etc.
- Parameters:
- {String} key
- Message options key
- {String} value
- Message options value
- Returns:
- {String} converted value
-
<private> {Boolean} _hasUTFChars(str)
Tests if a string has high bit (UTF-8) symbols
- Parameters:
- {String} str
- String to be tested for high bit symbols
- Returns:
- {Boolean} true if high bit symbols were found
-
<private> _init()
Resets and initializes MailComposer
-
<private> {String} _normalizeKey(key)
Normalizes a key name by cpitalizing first chars of words
x-mailerwill becomeX-MailerNeeded to avoid duplicate header keys
- Parameters:
- {String} key
- Key name
- Returns:
- {String} First chars uppercased
-
<private> _serveFile(filePath, callback)
Pipes a file to the e-mail stream
This function opens a file and starts sending 76 bytes long base64 encoded lines. To achieve this, the incoming stream is divded into chunks of 57 bytes (57/3*4=76) to achieve exactly 76 byte long base64
- Parameters:
- {String} filePath
- Path to the file
- {Function} callback
- Callback function to run after completion
-
addAttachment(attachment)
Adds an attachment to the list
Following options are allowed:
- fileName - filename for the attachment
- contentType - content type for the attachmetn (default will be derived from the filename)
- cid - Content ID value for inline images
- contents - String or Buffer attachment contents
- filePath - Path to a file for streaming
One of
contentsorfilePathmust be specified, otherwise the attachment is not included- Parameters:
- {Object} attachment
- Attachment info
-
addHeader(key, value)
Adds a header field to the headers object
- Parameters:
- {String} key
- Key name
- {String} value
- Header value
-
{Array} compileHeaders(headers)
Compiles headers object into an array of header lines. If needed, the lines are folded
- Parameters:
- {Object|Array} headers
- An object with headers in the form of
{key:value}or[[key, value]] or [{key:key, value: value}]
- Returns:
- {Array} A list of header lines. Can be joined with \r\n
-
setMessageOption(options)
Resets and initializes MailComposer
Setting an option overwrites an earlier setup for the same keys
Possible options:
- from - The e-mail address of the sender. All e-mail addresses can be plain
sender@server.comor formattedSender Name <sender@server.com> - to - Comma separated list of recipients e-mail addresses that will appear on the
To:field - cc - Comma separated list of recipients e-mail addresses that will appear on the
Cc:field - bcc - Comma separated list of recipients e-mail addresses that will appear on the
Bcc:field - replyTo - An e-mail address that will appear on the
Reply-To:field - subject - The subject of the e-mail
- body - The plaintext version of the message
- html - The HTML version of the message
- Parameters:
- {Object} options
- Message related options
- from - The e-mail address of the sender. All e-mail addresses can be plain
-
streamMessage()
Starts streaming the message