---
title: Sending email
project: Total.js V5
version: 0.0.20
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/total-js-v5/0-0-20/reference/mail/
updated: 2026-11-18T16:57:29.576Z
---

# Sending email

> MAIL(), HTMLMAIL(), LOGMAIL(), the Mail module, the message object and the SMTP keys.

This page lists the global mail functions, the `Mail` module with its events, the methods and properties of the message object, and the config keys for SMTP and the default sender. The guide [Sending email](../../guides/mail/) explains how they work together, with examples.

## API

### MAIL()

Type: `method`

Renders a view and sends it as an e-mail.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String|Array` | yes |  | One address, comma-separated addresses, or an array of addresses. Each becomes a `To:` recipient. |
| `subject` | `String` | yes |  | The subject. |
| `view` | `String` | yes |  | The name of the view, e.g. `mail/welcome`. |
| `model` | `Object` | no |  | The model of the view. |
| `language` | `String` | no |  | The language for the view and the subject. |
| `callback` | `Function` | no |  | `function(err, message)`, the same as `message.callback()`. |

Returns: `Message` The message that will be sent.

Renders the view `view` (a path in `/views/` without `.html`) with `model` and sends the HTML to `email`. The sender, the reply address and the copies come from `mail_from`, `mail_from_name`, `mail_reply`, `mail_cc` and `mail_bcc` (or `from`/`name` of `CONF.smtp`). With `language`, the view is rendered in that language and a subject with `@(...)` is translated. The message is sent on the next turn of the event loop, so you can still change the returned message. When `language` is a function, it is used as the callback.

Example: Welcome e-mail
```js
MAIL(user.email, '@(Welcome)', 'mail/welcome', user, user.language, function(err) {
	err && console.log('Not sent', err);
});
```

### HTMLMAIL()

Type: `method`

Sends an e-mail with an HTML body.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String|Array` | yes |  | One address, comma-separated addresses, or an array of addresses. Each becomes a `To:` recipient. |
| `subject` | `String` | yes |  | The subject. |
| `body` | `String` | yes |  | The HTML. |
| `language` | `String` | no |  | The language for translating the subject and the body. |
| `callback` | `Function` | no |  | `function(err, message)`, the same as `message.callback()`. |

Returns: `Message` The message that will be sent.

Sends `body` as HTML. A body without a `<body>` tag is wrapped in a simple HTML document with the subject as the title. With `language`, `@(...)` texts in the subject and the body are translated. The defaults are the same as for `MAIL()`. When `language` is a function, it is used as the callback.

Example: Send HTML
```js
HTMLMAIL('user@example.com', 'Password reset', '<p>Your code is <b>' + code + '</b>.</p>');
```

### LOGMAIL()

Type: `method`

Sends a short report as preformatted text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String|Array` | yes |  | One address, comma-separated addresses, or an array of addresses. Each becomes a `To:` recipient. |
| `subject` | `String` | no |  | The subject (default `CONF.name`). |
| `body` | `String|Object` | yes |  | The text, or an object to serialize. |
| `callback` | `Function` | no |  | `function(err, message)`, the same as `message.callback()`. |

Returns: `Message` The message that will be sent.

Wraps `body` in a `<pre>` block and sends it. A string is inserted as it is, so it can contain HTML. An object is serialized to JSON and HTML-encoded. Without `subject` (two arguments, or a function as the third one), `CONF.name` is the subject. The defaults are the same as for `MAIL()`.

Example: Report
```js
LOGMAIL('admin@example.com', 'Nightly import', { imported: 120, failed: 2 });
LOGMAIL('admin@example.com', 'The backup has finished.');
```

### DEF.onMail

Type: `delegate`

The function that builds and sends the messages of MAIL(), HTMLMAIL() and LOGMAIL().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String|Array` | yes |  | One address, comma-separated addresses, or an array of addresses. Each becomes a `To:` recipient. |
| `subject` | `String` | yes |  | The subject. |
| `body` | `String` | yes |  | The HTML body. |
| `callback` | `Function` | no |  | The callback. |
| `reply` | `String` | no |  | A reply address instead of `mail_reply`. |

Returns: `Message` The message.

Called as `DEF.onMail(email, subject, body, callback, reply)`. It creates the message, adds the recipients, the sender (`mail_from` or `CONF.smtp.from`/`user`, `mail_from_name` or `CONF.smtp.name`), the reply address (`reply` or `mail_reply`), `mail_cc` and `mail_bcc`, sets the callback and schedules the sending. It must return the message. When `callback` is a string, it is taken as `reply`.

Example: Add a header to every message
```js
var onmail = DEF.onMail;
DEF.onMail = function(email, subject, body, callback, reply) {
	var msg = onmail(email, subject, body, callback, reply);
	msg.headers = { 'X-Application': CONF.name };
	return msg;
};
```

### Mail

Type: `class`

The global SMTP client.

Returns: `Object`

`Mail` is the mailer of the framework. It creates messages (`Mail.create()`), sends them (`Mail.send()`, `Mail.send2()`), tests a server (`Mail.try()`) and emits the `send` and `error` events. `Mail.Message` (also `Mail.Mail`) is the constructor of messages.

Example: Create and send
```js
var msg = Mail.create('Hello', '<p>Hello</p>');
msg.from('info@example.com').to('user@example.com').send2();
```

### Mail.create()

Type: `method`

Creates a new message.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `subject` | `String` | yes |  | The subject. |
| `body` | `String` | yes |  | The body (HTML by default). |

Returns: `Message` A new message.

Returns a new message with the subject and the body. Unlike `MAIL()`, nothing is set or sent for you: add the sender and the recipients and call `message.send2()` or `message.send()`.

Example: Create
```js
var msg = Mail.create('Hello', '<p>Hello from Total.js</p>');
msg.from('info@example.com', 'My application');
msg.to('user@example.com');
msg.send2();
```

### Mail.send()

Type: `method`

Sends one or more messages with the given SMTP options.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The SMTP options. |
| `messages` | `Message|Array` | yes |  | A message or an array of messages. |
| `callback` | `Function` | no |  | `function(err)`. |

Opens a connection with `options` (the same keys as `CONF.smtp`), sends the messages one after another and closes the connection. The callback receives `err` when the connection or the login fails. Use `message.callback()` for the result of each message.

Example: Two messages, one connection
```js
var a = Mail.create('A', '<p>A</p>').from('info@example.com').to('a@example.com');
var b = Mail.create('B', '<p>B</p>').from('info@example.com').to('b@example.com');
Mail.send(CONF.smtp, [a, b]);
```

### Mail.send2()

Type: `method`

Sends messages with CONF.smtp.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `messages` | `Message|Array` | yes |  | A message or an array of messages. |
| `callback` | `Function` | no |  | `function(err)`. |

The same as `Mail.send(CONF.smtp, messages, callback)`.

Example: Send
```js
Mail.send2([msg1, msg2]);
```

### Mail.try()

Type: `method`

Tests the connection and the login to an SMTP server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The SMTP options. |
| `callback` | `Function` | no |  | `function(err)`. |

Returns: `Promise` Without a callback.

Connects with `options`, logs in and quits without sending anything. Without a callback it returns a promise.

Example: Check the settings
```js
Mail.try(CONF.smtp).then(function() {
	console.log('SMTP works');
}).catch(function(err) {
	console.log('SMTP error:', err);
});
```

### Mail.debug

Type: `property`

Prints the SMTP conversation to the console.

Returns: `Boolean` Default `false`.

With `true`, every line sent to and received from the server is printed. Default `false`.

Example: Debug
```js
Mail.debug = true;
```

### Mail.on('send')

Type: `event`

Emitted when a connection is ready to send.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `connection` | `Object` | yes |  | The internal connection object. |

`Mail.on('send', function(connection) {})`. Emitted when the client starts the SMTP conversation of a connection.

Example: Listen
```js
Mail.on('send', function() {
	console.log('SMTP connection ready');
});
```

### Mail.on('error')

Type: `event`

Emitted on SMTP errors.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `err` | `Error|String` | yes |  | The error. |
| `connection` | `Object` | yes |  | The internal connection object. |

`Mail.on('error', function(err, connection) {})`. Emitted when the connection fails, the login is refused or the server answers with an error. `err` is an `Error` or the reply line of the server.

Example: Log SMTP errors
```js
Mail.on('error', function(err) {
	console.log('SMTP error', err);
});
```

### ON('$mail')

Type: `event`

Emitted for every message that is being sent.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `Message` | yes |  | The message. |

`ON('$mail', function(message) {})`. Emitted just before the message is written to the SMTP server.

Example: Log every e-mail
```js
ON('$mail', function(message) {
	console.log('Sending', message.subject, 'to', message.email_to);
});
```

### Mail.Message

Type: `class`

The message object.

Returns: `Object`

A message is returned by `MAIL()`, `HTMLMAIL()`, `LOGMAIL()` and `Mail.create()`; `new Mail.Message(subject, body)` creates one too (`Mail.Mail` is an alias). Its methods return the message, so calls can be chained. The recipients are in `email_to`, `email_cc`, `email_bcc` and `email_reply` (arrays), the sender in `email_from` and `email_from_name`.

Example: Chain
```js
Mail.create('Hi', '<p>Hi</p>').from('info@example.com').to('a@example.com').cc('b@example.com').send2();
```

### message.to()

Type: `method`

Adds a recipient.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | One address. |
| `clear` | `Boolean` | no | `false` | Remove the current recipients first. |

Returns: `Message` The message, for chaining.

Adds one address to the `To:` header. With `clear`, the current recipients are removed first.

Example: Recipients
```js
msg.to('a@example.com').to('b@example.com');
```

### message.cc()

Type: `method`

Adds a copy recipient.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | One address. |
| `clear` | `Boolean` | no | `false` | Remove the current copies first. |

Returns: `Message` The message, for chaining.

Adds one address to the `Cc:` header. With `clear`, the current copies are removed first.

Example: Copy
```js
msg.cc('boss@example.com');
```

### message.bcc()

Type: `method`

Adds a blind copy recipient.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | One address. |
| `clear` | `Boolean` | no | `false` | Remove the current blind copies first. |

Returns: `Message` The message, for chaining.

Adds one address that receives the message without being listed in the headers. With `clear`, the current blind copies are removed first.

Example: Blind copy
```js
msg.bcc('archive@example.com');
```

### message.reply()

Type: `method`

Adds a reply address.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | One address. |
| `clear` | `Boolean` | no | `false` | Remove the current reply addresses first. |

Returns: `Message` The message, for chaining.

Adds one address to the `Reply-To:` header. With `clear`, the current reply addresses are removed first.

Example: Reply to support
```js
msg.reply('support@example.com');
```

### message.from()

Type: `method`

Sets the sender.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | The address. |
| `name` | `String` | no |  | The display name. |

Returns: `Message` The message, for chaining.

Sets the `From:` address and, optionally, the display name. `message.sender()` is the same method.

Example: Sender
```js
msg.from('info@example.com', 'My application');
```

### message.sender()

Type: `method`

An alias of message.from().

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `email` | `String` | yes |  | The address. |
| `name` | `String` | no |  | The display name. |

Returns: `Message` The message, for chaining.

The same as `message.from(email, [name])`.

Example: Sender
```js
msg.sender('info@example.com');
```

### message.attachment()

Type: `method`

Attaches a file from the disk or a Buffer.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `filename` | `String|Buffer` | yes |  | The path to the file, or a `Buffer` with the content. |
| `name` | `String` | yes |  | The file name for the recipient, with its extension. |
| `contentid` | `String` | no |  | The content ID for an inline file. |

Returns: `Message` The message, for chaining.

Adds the file `filename`, or the content of a `Buffer`, as an attachment. `name` is the file name shown to the recipient, and its extension sets the content type (`application/octet-stream` for unknown ones). With `contentid`, the file is an inline part that the HTML body refers to as `cid:<contentid>`. The file is read when the message is sent.

Example: Attach a PDF
```js
msg.attachment(PATH.databases('invoices/2025-001.pdf'), 'invoice.pdf');
```

Example: Inline image
```js
var msg = HTMLMAIL('user@example.com', 'News', '<img src="cid:logo" />');
msg.attachment(PATH.public('img/logo.png'), 'logo.png', 'logo');
```

Example: From a Buffer
```js
var pdf = Buffer.from('%PDF-1.4 ...');
var msg = HTMLMAIL('user@example.com', 'Invoice', '<p>Your invoice is attached.</p>');
msg.attachment(pdf, 'invoice.pdf');
```

### message.attachmentfs(storage, id, [name], [contentid])

Type: `method`

Attaches a file from a file storage.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `storage` | `String` | yes |  | The name of the file storage. |
| `id` | `String` | yes |  | The id of the file in the storage. |
| `name` | `String` | no |  | The file name for the recipient, with its extension. Default: the name stored with the file. |
| `contentid` | `String` | no |  | The content ID for an inline file. |

Returns: `Message` The message, for chaining.

Adds a file of a [file storage](../file-storage/) as an attachment: `storage` is the name of the storage (as in `FILESTORAGE(name)`) and `id` the id of the file. Without `name`, the recipient sees the name of the file in the storage and the content type is the type stored with the file; with `name`, its extension sets the content type. With `contentid`, the file is an inline part that the HTML body refers to as `cid:<contentid>`.

The file is read when the message is sent. When it does not exist, the error is written to the error log and the message is sent without the attachment.

Example: Attach a stored file
```js
var id = UID();
await FILESTORAGE('documents').save(id, 'contract.pdf', Buffer.from('%PDF-1.4 ...'));

var msg = HTMLMAIL('user@example.com', 'Contract', '<p>The contract is attached.</p>');
msg.attachmentfs('documents', id);
```

Example: Another name
```js
msg.attachmentfs('documents', id, 'contract-2025.pdf');
```

### message.unsubscribe()

Type: `method`

Adds the List-Unsubscribe headers.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The unsubscribe URL or e-mail address. |

Returns: `Message` The message, for chaining.

Adds `List-Unsubscribe` and `List-Unsubscribe-Post: List-Unsubscribe=One-Click`. A value that starts with `http:` or `https:` is used as a URL; any other value is an e-mail address (`mailto:`).

Example: Newsletter
```js
msg.unsubscribe('https://example.com/unsubscribe/?id=' + user.id);
```

### message.callback()

Type: `method`

Sets the function called with the result of the message.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(err, message)`. |

Returns: `Message` The message, for chaining.

`fn(err, message)` is called with an empty `err` when the message has been handed over to the server, or with an error: the reply line of the server (in upper case) when it refuses the message or the login, `No SMTP server configuration.`, or a connection error or timeout. An error that reaches a callback is not written to the error log. `MAIL()`, `HTMLMAIL()` and `LOGMAIL()` set it from their `callback` argument.

Example: Result
```js
msg.callback(function(err, message) {
	if (err)
		console.log('Not sent:', message.subject, err);
});
```

### message.manually()

Type: `method`

Stops the automatic sending.

Returns: `Message` The message, for chaining.

`MAIL()`, `HTMLMAIL()` and `LOGMAIL()` send the message on the next turn of the event loop. Call `manually()` right after them to cancel that, and send the message later with `message.send2()` or `message.send()`.

Example: Send later
```js
var msg = MAIL('user@example.com', 'Report', 'mail/report', data);
msg.manually();
// ...
msg.send2();
```

### message.send2()

Type: `method`

Sends the message with CONF.smtp.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | `function(err)`. |

Sends the message with the configured SMTP server (`CONF.smtp`).

Example: Send
```js
Mail.create('Hello', '<p>Hello</p>').from('info@example.com').to('user@example.com').send2(function(err) {
	err && console.log(err);
});
```

### message.send(options)

Type: `method`

Sends the message with other SMTP options.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | The SMTP options. |

Returns: `Message` The message, for chaining.

Sends the message over a new connection with `options` (the same keys as `CONF.smtp`). Use `message.callback()` for the result.

Example: Another server
```js
msg.callback(function(err) {
	err && console.log(err);
});
msg.send({ server: 'smtp.other.com', port: 587, user: 'me@other.com', password: 'secret' });
```

### message.subject

Type: `property`

The subject.

Returns: `String`

The subject of the message. Non-ASCII characters are encoded for you.

Example: Change
```js
msg.subject = 'Updated subject';
```

### message.body

Type: `property`

The body.

Returns: `String`

The body of the message: HTML, or plain text when `type` is `plain`.

Example: Change
```js
msg.body = '<p>New text</p>';
```

### message.type

Type: `property`

The type of the body.

Returns: `String` Default `html`.

`html` (default) or `plain`. It becomes the `Content-Type` of the body (`text/html` or `text/plain`).

Example: Plain text
```js
var msg = Mail.create('Hello', 'Hello in plain text');
msg.type = 'plain';
```

### message.headers

Type: `property`

Extra headers of the message.

Returns: `Object`

An object of header names and values written into the message as they are. It is not set by default.

Example: Custom header
```js
msg.headers = { 'X-Campaign': 'spring-2025' };
```

### message.high()

Type: `method`

Marks the message as high priority.

Returns: `Message` The same message.

Adds the header `X-Priority: 1`. Returns the message.

Example: Urgent
```js
HTMLMAIL('admin@example.com', 'Disk full', '<p>The disk is full.</p>').high();
```

### message.low()

Type: `method`

Marks the message as low priority.

Returns: `Message` The same message.

Adds the header `X-Priority: 5`. Returns the message.

Example: Weekly digest
```js
MAIL(user.email, 'Your week', 'mail/digest', model).low();
```

### message.confidential()

Type: `method`

Marks the message as confidential.

Returns: `Message` The same message.

Adds the header `Sensitivity: Company-Confidential`. Returns the message.

Example: Confidential report
```js
HTMLMAIL('board@example.com', 'Salaries', html).confidential().high();
```

### CONF.smtp

Type: `config`

The SMTP server and its options.

Returns: `Object`

An object with `server` (or `host`, `hostname`, `smtp`, `url`), `port`, `secure`, `user`, `password`, `token`, `tls`, `timeout`, `from` and `name`. With `secure: true` the connection uses TLS from the start and `port` defaults to `465`; otherwise set `port`. The key `mail` in the config file is the same.

Before the connection, `server` is converted with `U.tohost()`: an international name is sent as punycode and an IPv4 address in its dotted form (`127.1` becomes `127.0.0.1`). A value that is not a valid host name gives the error `Invalid SMTP server hostname.` and nothing is sent.

Example: /config
```text
smtp (json) : {"server":"smtp.example.com","port":465,"secure":true,"user":"info@example.com","password":"secret"}
```

Example: STARTTLS on port 587
```text
smtp (json) : {"server":"smtp.example.com","port":587,"user":"info@example.com","password":"secret"}
```

### CONF.mail_smtp

Type: `config`

The SMTP server name.

Returns: `String`

Sets `CONF.smtp.server`. Use it with `mail_smtp_options` instead of one `smtp` object.

Example: /config
```text
mail_smtp : smtp.example.com
```

### CONF.mail_smtp_options

Type: `config`

The SMTP options without the server.

Returns: `Object`

An object whose keys are copied into `CONF.smtp` (`port`, `secure`, `user`, `password`...).

Example: /config
```text
mail_smtp_options : {"port":587,"user":"info@example.com","password":"secret"}
```

### CONF.mail_from

Type: `config`

The default sender address.

Returns: `String`

The `From:` address of `MAIL()`, `HTMLMAIL()` and `LOGMAIL()`. Without it, `CONF.smtp.from` or `CONF.smtp.user` is used.

Example: /config
```text
mail_from : info@example.com
```

### CONF.mail_from_name

Type: `config`

The default sender name.

Returns: `String`

The display name of the sender of `MAIL()`, `HTMLMAIL()` and `LOGMAIL()`. Without it, `CONF.smtp.name` is used.

Example: /config
```text
mail_from_name : My application
```

### CONF.mail_reply

Type: `config`

The default reply address.

Returns: `String`

One address added as `Reply-To:` to the messages of `MAIL()`, `HTMLMAIL()` and `LOGMAIL()`.

Example: /config
```text
mail_reply : support@example.com
```

### CONF.mail_cc

Type: `config`

A default copy recipient.

Returns: `String`

One address added as `Cc:` to the messages of `MAIL()`, `HTMLMAIL()` and `LOGMAIL()`.

Example: /config
```text
mail_cc : office@example.com
```

### CONF.mail_bcc

Type: `config`

A default blind copy recipient.

Returns: `String`

One address added as a blind copy to the messages of `MAIL()`, `HTMLMAIL()` and `LOGMAIL()`.

Example: /config
```text
mail_bcc : archive@example.com
```

### $tapimail : true

Type: `config`

Sends the messages through the Total.js API instead of SMTP.

Returns: `Boolean`

Default `false`. With `true`, `MAIL()`, `HTMLMAIL()`, `LOGMAIL()` and `message.send2()` send the message with `API('TAPI', 'mail', data)` and the token `secret_totalapi`; `$tapi` must be `true` (the default). The service gets the recipients (`to`, `cc`, `bcc`, `reply`), `from`, `subject`, `type`, `body`, `priority`, `unsubscribe` and `confidential`. Attachments and `message.headers` are not sent. The callback of the message gets the answer of the service.

Example: /config
```text
$tapimail       : true
secret_totalapi : your-totalapi-token
```
