Skip to content

infinity.mail

Provides functionality for creating, as well as reading or writing standard conform email data from or to files and streams.

Module: infinity.mail

Example:

infinity.loadModule('infinity.mail');
infinity.loadModule('infinity.fileSystem');

let myMail = new infinity.mail();


myMail.replyTo = 'support@site.com';
myMail.from = 'mailer@site.com';
myMail.sender = 'sender@site.com';
myMail.returnPath = 'mailFailed@site.com';
myMail.charset = 'utf-8';
myMail.organization = 'Corporation Institution';
myMail.userAgent = 'INFINITY.JS';

myMail.recipients = ['recipient@site.com', 'secondRecipient@site.com'];
myMail.cc = ['copyRecipient@site.com'];
myMail.bcc = ['blindCopyRecipient@site.com'];

myMail.subject = 'Mailsubject';

myMail.contents = [{
  contentType: 'text/html', 
  contentTransferEncoding: infinity.mail.transferEncoding.quotedPrintable, 
  charset: 'utf-8', 
  text: 'Hello, this is my mailtext. And here is a picture: <img src="cid:att01.bmp">', 
  items: []
}];

// myMail.attachments is not a real array. It can only be assigned, but not modified
myMail.attachments = [{
  name: 'image.bmp',
  size: 1254,
  contentType: 'image/bmp',
  contentTransferEncoding: infinity.mail.transferEncoding.base64,
  contentId: 'att01.bmp',
  contentDisposition: 'inline',
  storedFilePath: infinity.current.root + '../image.bmp'
}];

try {
  myMail.saveToFile(infinity.current.root + '../mail.eml');
} finally {
  myMail.free();
}

Resulting mail.eml:

Return-Path: <mailFailed@site.com>
From: <mailer@site.com>
Sender: <sender@site.com>
Reply-To: <support@site.com>
To: <recipient@site.com>,
    <secondRecipient@site.com>
CC: <copyRecipient@site.com>
Organization: Corporation Institution
X-Mailer: INFINITY.JS
X-Priority: 3 (Normal)
Subject: Mailsubject
Date: Thu, 8 Apr 2021 16:24:33 +0000
Content-Transfer-Encoding: 7bit
Content-Type: multipart/related;
    boundary="_NextPart_f40c92e644d38073e5521df63cb73a57"
MIME-Version: 1.0

This is a multi-part message in MIME format

--_NextPart_f40c92e644d38073e5521df63cb73a57
Content-Type: text/html;
    charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Hello, this is my mailtext. And here is a picture: <img src=3D"cid:att01.bm=
p">

--_NextPart_f40c92e644d38073e5521df63cb73a57
Content-Type: image/bmp;
    name="image.bmp"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
    filename="image.bmp"
Content-ID: att01.bmp

Qk3mBAAAAAAAADYAAAAoAAAAFAAAABQAAAABABgAAAAAAAAAAAATCwAAEwsAAAAAAAAAAAAACvDN
CvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNqdUZCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN
CvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNLOMZCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN
CvDN2t4fCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNtvAZCvDNCvDNCvDNCvDNCvDNCvDNCvDN
CvDNCvDNCvDNeOwfCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNQ/4ZCvDNCvDNCvDNCvDNCvDNCvDN
CvDNCvDNeOwfCvDNCvDN5vkfhx0hCvDNCvDNCvDNCvDNCvDNCvDNwygbCvDNCvDNCvDNCvDNCvDN
CvDNhx0h5vkfCvDNCvDNCvDNCvDNCvDN8TwhCvDNCvDNCvDNCvDNCvDNVkUbCvDNCvDNCvDNCvDN
CvDN8TwhCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNlVUhCvDNCvDNCvDNCvDNz1wbCvDNCvDNCvDN
CvDNlVUhCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN/Goho30hCvDNCvDNaHEbCvDNCvDN
o30h/GohCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNHZAhCvDN2oMbCvDN
HZAhCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNsKAheJUb
sKAhCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN2t4feOwf5vkfhx0h8TwhlVUh/Goho30hHZAhvMAh
OLEhvMAhTc8hydwhX+sh1fghcBkj4Dkjf1Mj62gjCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNTc8h
CvDNh7UbCvDNTc8hCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNydwh
CvDNCvDN8cQbCvDNCvDNydwhCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN1fghX+sh
CvDNCvDNCvDNldMbCvDNCvDNCvDNX+sh1fghCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNcBkjCvDN
CvDNCvDNCvDNCvDN/OEbCvDNCvDNCvDNCvDNCvDNcBkjCvDNCvDNCvDNCvDNCvDNCvDN4DkjCvDN
CvDNCvDNCvDNCvDNCvDNo+8bCvDNCvDNCvDNCvDNCvDNCvDN4DkjCvDNCvDNCvDN62gjf1MjCvDN
CvDNCvDNCvDNCvDNCvDNCvDNHfwbCvDNCvDNCvDNCvDNCvDNCvDNCvDNf1Mj62gjjnwjCvDNCvDN
CvDNCvDNCvDNCvDNCvDNCvDNCvDNsCUdCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN
CvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNOEMdCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN
CvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNvFkdCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDNCvDN

--_NextPart_f40c92e644d38073e5521df63cb73a57--

Example for working with received emails:

infinity.loadModule('infinity.mail');
infinity.loadModule('infinity.fileSystem');

let myMail = new infinity.mail();

myMail.loadFromFile('C:/tmp/email.eml');

infinity.file.writeString('c:/tmp/mailContent.txt', myMail.getTextContent());

Classes

mail

The class for operations with email files.

Example:

infinity.loadModule('infinity.mail');
let myMail = new infinity.mail();

Properties


attachments

Type: infinity.mail.attachmentArray

Gets or sets the attachments for the current email. The data must be passed and will be retrieved inside an infinity.mail.attachmentArray. See the example on top of this page for more information. Note that this property is not a real array and it cannot be modified, so myMail.attachments.push() will not work. You need to assign an array with attachment items to this property.

When a message is loaded, its attachments are only read if the mail object was created with parseAttachments set to true (see the constructor). They are stored as temporary files, referenced by storedFilePath.

Important: the temporary files of loaded attachments are deleted when the mail object is freed or cleared and when another message is loaded, so copy them before if you want to keep them. If you assign loaded attachments to the attachments property again, their temporary files stay with the mail object. Files of attachments that you assign yourself are never deleted by the mail object (before 3.11 they were deleted like the temporary files).


bcc

Type: infinity.mail.stringArray

Gets or sets the blind carbon copy recipients for the current email. The data must be passed and will be retrieved inside an infinity.mail.stringArray. See the example on top of this page for more information. An error is thrown if one of the addresses is invalid.

The BCC addresses are not written to the message header; infinity.smtp.client sends the message to them, too.


boundary

Type: string

Gets or sets the MIME boundary that separates the parts of a multipart email. A random boundary (_NextPart_ followed by random characters) is generated for new mail objects.


cc

Type: infinity.mail.stringArray

Gets or sets the CC header for the current email. The data must be passed and will be retrieved inside an infinity.mail.stringArray. See the example on top of this page for more information. Invalid addresses are skipped.


charset

Type: string

Gets or sets the character encoding for the current email. It is used for the header fields and for the body of a single-part email (multipart emails use the charset of each content item). Default: 'utf-8' (before 3.11: 'windows-1252').

When writing, the text is converted for the charsets utf-8, utf-7, iso-8859-1, iso-8859-15 and windows-1252 (characters that the charset can't represent are replaced by a similar character or ?); with any other charset, the text is not converted. When a message is loaded, this property contains the charset the text was decoded with: the declared charset, utf-8 if the text is valid UTF-8 although a single-byte charset (US-ASCII, ISO-8859-1/-15, Windows-1252) or none is declared, otherwise windows-1252 if none is declared. The same detection is used for each content part; header text outside of encoded words is decoded as UTF-8 if valid, otherwise as Windows-1252.


contentTransferEncoding

Type: infinity.mail.transferEncoding

Gets or sets the Content-Transfer-Encoding header for the current email according to the infinity.mail.transferEncoding enum. When the email is saved, the value is determined automatically from the contents and attachments (quotedPrintable for a single-part email, bit7 for multipart emails), so a value you set is overwritten.


contentType

Type: string

Gets or sets the MIME Content-Type header for the current email (without parameters, e.g. text/plain or multipart/mixed). When the email is saved, the value is determined automatically from the contents and attachments, so a value you set is overwritten: text/plain or text/html for a single content without attachments, multipart/alternative for a text and an HTML content without attachments or with attachments that all have a contentId, multipart/related for a single HTML content with attachments that all have a contentId, otherwise (attachments without contentId, or no HTML content) multipart/mixed.


contents

Type: infinity.mail.contentArray

Gets or sets the contents for the current email. The data must be passed and will be retrieved inside an infinity.mail.contentArray. See the example on top of this page for more information. Note that this property is not a real array and it cannot be modified, so myMail.contents.push() will not work. You need to assign an array with content items to this property.

To save an email, the contents may contain at most one text/plain and one text/html item; other combinations cause an error when saving. Content items of multipart emails must use the transfer encoding bit7, bit8 or quotedPrintable.


date

Type: number

Gets or sets the Date header for the current email, in milliseconds since 1970-01-01 (UTC). If no date is set, the current time is written when the email is saved. When a message is loaded, a date that can't be parsed or lies in the future is replaced by the current time.


deliveryNotification

Type: string

Gets or sets the address that a read receipt is requested for (headers Disposition-Notification-To and X-Confirm-Reading-To). An empty string means no receipt is requested.


envelopeTo

Type: string

Gets or sets the envelope recipient of the current email. When a message is loaded, it is read from the Envelope-To, X-Envelope-To or X-Originally-To header. It is not written when the email is saved.


extendedHeaders

Type: infinity.mail.headerArray

Allows for additional header fields to be added when the email is saved. Assign an array of {name, value} objects; a name that occurs twice keeps the last value, and fields with an empty value are not written. Non-ASCII values are encoded with the email's charset.

This property is only used for writing: when a message is loaded, header fields are not put into extendedHeaders, so it is empty afterwards.


from

Type: string

Gets or sets the From header for the current email.


organization

Type: string

Gets or sets the Organization header for the current email.


priority

Type: infinity.mail.priority

Gets or sets the X-Priority header for the current email according to the infinity.mail.priority enum. Default: normal. Values outside the enum are limited to 0...4.


recipients

Type: infinity.mail.stringArray

Gets or sets the To header for the current email. The data must be passed and will be retrieved inside an infinity.mail.stringArray. See the example on top of this page for more information. Invalid addresses are skipped.


replyTo

Type: string

Gets or sets the Reply-To header for the current email.


returnPath

Type: string

Gets or sets the Return-Path header for the current email.


sender

Type: string

Gets or sets the Sender header for the current email.


subject

Type: string

Gets or sets the subject for the current email. Non-ASCII characters are encoded with the email's charset when saving.


userAgent

Type: string

Gets or sets the X-Mailer header for the current email.


inReplyTo

Type: string

Gets the data out of the In-Reply-To header of a loaded email. It is written as References and In-Reply-To header when the email is saved.


messageId

Type: string

Gets the data out of the Message-ID header of a loaded email. No message id is generated for new emails.


multipartType

Type: infinity.mail.multipartType

Gets the multipart type of a loaded email according to the infinity.mail.multipartType enum: alternative for a multipart/alternative Content-Type, other for any other multipart/... type and none if the email is not a multipart email.

The property is also available under the misspelled name mulitpartType of earlier versions (before 3.11 only under this name).


receivedFrom

Type: infinity.mail.stringArray

Gets the IPv4 addresses found in the Received headers of a loaded email (each address once), starting with the server that handled the email first. Before 3.11 each address included the characters around it (e.g. [192.0.2.20]).


size

Type: number

Gets the size of the current email in bytes: the size of the stream or file it was loaded from, or the size of the stream or file it was last saved to. 0 for a new email that hasn't been saved yet.


Methods


constructor()

Lets you create an INFINITY.JS mail object instance.

Signature:

constructor( parseAttachment?: boolean )

Parameters:

  • parseAttachment: boolean, optional

    Whether to read the attachments when loading an email from a file or stream. If true, the attachments are stored as temporary files and listed in attachments; otherwise they are skipped (and are missing if the email is saved again). Default: false.

Example:

infinity.loadModule('infinity.mail');
let myMail = new infinity.mail();

clear()

Clears all data from the mail object instance and resets the properties to their defaults (a new random boundary is generated). The temporary files of loaded attachments are deleted (files of attachments you assigned yourself are kept).

Signature:

clear(): void

Example:

myMail.clear();

free()

Frees the memory previously occupied by the mail object instance. The temporary files of loaded attachments are deleted (files of attachments you assigned yourself are kept).

Signature:

free(): void

Example:

myMail.free();

getBestContent()

Returns the most appropriate HTML or text content of the current mail: in a multipart/alternative part, the HTML content is preferred over the text content; otherwise the contents of the type found first (text/html or text/plain) are returned, several contents of that type separated by a line. Whether the result is HTML or text is not returned.

Signature:

getBestContent( replaceCID: boolean ): string

Parameters:

  • replaceCID: boolean

    Specifies whether cid: references to images in an HTML result should be replaced by the image data (as data: URL). This requires that the attachments have been read (see the constructor); only BMP, JPEG, PNG and GIF images are replaced.

Return type: string

Example:

myMail.getBestContent(true);

getHtmlContent()

Returns the content of the email as HTML: all text/html contents and all text/plain contents (HTML-encoded) in their order, separated by <p><hr></p>.

Signature:

getHtmlContent( replaceCID: boolean ): string

Parameters:

  • replaceCID: boolean

    Specifies whether cid: references to images should be replaced by the image data (as data: URL). This requires that the attachments have been read (see the constructor); only BMP, JPEG, PNG and GIF images are replaced.

Return type: string

Example:

myMail.getHtmlContent(true);

getTextContent()

Returns the content of the email as plain text: all text/plain contents and all text/html contents (converted to text) in their order, separated by a line -----.

Signature:

getTextContent(): string

Return type: string

Example:

let textContent = myMail.getTextContent();

loadFromFile()

Loads data from the specified email file (RFC 5322 format, CRLF or - since 3.11 - LF line endings) into the mail object instance. All previous data is cleared first.

The file is read as it is: lines starting with . are text. Only data in SMTP transfer form (ending with a line that holds only ., e.g. files saved by imap4 retrieve() before 3.11) is dot-unstuffed (the first . of lines starting with .. is removed, the . line ends the message). Before 3.11 the first . of lines starting with .. was always removed and a . line ended a single-part text.

Signature:

loadFromFile( fileName: string ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be loaded.

Example:

myMail.loadFromFile(infinity.current.root + '../mail.eml');

loadFromStream()

Loads data from the given stream into the mail object instance, reading from the current position of the stream (set position to 0 to read a stream from the beginning). All previous data is cleared first.

Signature:

loadFromStream( stream: infinity.stream ): void

Parameters:

Example:

myMail.loadFromStream(stream);

saveToFile()

Saves standard conform email data from the mail object instance to a file. An existing file is overwritten. The BCC addresses are not written.

Signature:

saveToFile( fileName: string ): void

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file to save the data to.

Example:

myMail.saveToFile(infinity.current.root + '../mail.eml');

saveToStream()

Saves standard conform email data from the mail object instance to the given stream, at its current position.

Signature:

saveToStream( stream: infinity.stream ): void

Parameters:

Example:

myMail.saveToStream(stream);

Interfaces

attachmentArray

Extends: Array<{name: string, size: number, contentType: string, contentTransferEncoding: infinity.mail.transferEncoding, contentId: string, contentDisposition: string, storedFilePath: string}>

An array containing objects with attachment data inside its properties. Note: this array can not be modified. You need to assign an array with attachment items, but you cannot call push() on the attachments property of an infinity.mail object.

Properties:

  • name

    Type: string. The name of the attachment for the corresponding parameter of the Content-Type attachment header.

  • size

    Type: number. The size of the attachment in bytes (must not be negative). It is not used when the email is saved.

  • contentType

    Type: string. The MIME content type of the attachment (e.g. image/png).

  • contentTransferEncoding

    Type: infinity.mail.transferEncoding. The transfer encoding of the attachment in a loaded email. When the email is saved, attachments are always encoded with base64.

  • contentId

    Type: string. The content id ("cid") for e.g. inline insertion of the attachment into the email content.

  • contentDisposition

    Type: string. The entry for the Content-Disposition header. When the email is saved, inline is written for 'inline' and attachment for any other value.

  • storedFilePath

    Type: string. The filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the attachment file. Temporary files of loaded attachments are deleted when the mail object is freed or cleared, files you assigned yourself are kept (see attachments).


contentArray

Extends: Array<{contentType: string, contentTransferEncoding: infinity.mail.transferEncoding, charset: string, text: string, items: infinity.mail.contentArray}>

An array containing objects with content data inside its properties. Note: this array can not be modified. You need to assign an array with content items, but you cannot call push() on the contents property of an infinity.mail object.

Properties:

  • contentType

    Type: string. The MIME content type of the content (e.g. text/plain or text/html; empty for an item that only holds nested items).

  • contentTransferEncoding

    Type: infinity.mail.transferEncoding. The transfer encoding for the email body content.

  • charset

    Type: string. The character encoding identifier for the email body content.

  • text

    Type: string. The email body content (decoded).

  • items

    Type: infinity.mail.contentArray. The nested contents of a loaded multipart part (e.g. the text and HTML content of a multipart/alternative part inside a multipart/mixed email); empty for a text or HTML content.


headerArray

Extends: Array<{name: string, value: string}>

An array containing objects with data inside its properties.

Properties:

  • name

    Type: string. The field name.

  • value

    Type: string. The field value.


stringArray

Extends: Array<string>

An array of strings.


Enums

infinity.mail.multipartType

Values:

  • none: 0

    Not a multipart email.

  • alternative: 1

    Stands for the multipart/alternative Content-Type header declaration.

  • other: 2

    Stands for any other multipart/... Content-Type header declaration (e.g. multipart/mixed or multipart/related).

Example:

infinity.loadModule('infinity.mail');
let multipartType = infinity.mail.multipartType.none;

infinity.mail.priority

Values:

  • lowest: 0

    Stands for the 5 (Lowest) X-Priority email header declaration.

  • lower: 1

    Stands for the 4 (Lower) X-Priority email header declaration.

  • normal: 2

    Stands for the 3 (Normal) X-Priority email header declaration.

  • high: 3

    Stands for the 2 (High) X-Priority email header declaration.

  • highest: 4

    Stands for the 1 (Highest) X-Priority email header declaration. Also available under the name epHighest of earlier versions (before 3.11 only under this name).

Example:

infinity.loadModule('infinity.mail');
let priority = infinity.mail.priority.lowest;

infinity.mail.transferEncoding

Values:

  • bit7: 0

    Stands for the 7bit Content-Transfer-Encoding email header declaration.

  • bit8: 1

    Stands for the 8bit Content-Transfer-Encoding email header declaration.

  • binary: 2

    Stands for the binary Content-Transfer-Encoding email header declaration.

  • quotedPrintable: 3

    Stands for the quoted-printable Content-Transfer-Encoding email header declaration.

  • base64: 4

    Stands for the base64 Content-Transfer-Encoding email header declaration.

Example:

infinity.loadModule('infinity.mail');
let transferEncoding = infinity.mail.transferEncoding.bit7;