[**Documentation**](../../../README)

***

[Documentation](../../../README) / [@zerotal/notifications](../README) / MailMessage

# Class: MailMessage

Defined in: [notifications/src/messages/MailMessage.ts:122](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L122)

The fluent representation a notification returns from `toMail()` — symmetric with
`toSms() → SmsMessage` and `toSlack() → SlackMessage`. Compose a branded email from
a greeting, lines, and an optional call-to-action button, or drop to `.html()` for a
fully custom body.

Lines accept inline styling. Pass a `TextStyle` to style a whole line, or use the
callback form to mix differently-styled runs within one line.

The recipient is normally the notifiable (its `email`), so you don't set `to()` —
`MailChannel` fills it in. Use `to()`/`cc()`/`bcc()`/`from()`/`replyTo()` only to
override.

## Example

```ts
toMail(n: Notifiable): MailMessage {
  return new MailMessage()
    .subject("Your order shipped")
    .greeting(`Hi ${n.name ?? "there"},`, { bold: true })
    .line("Your order is on its way.")
    .line("Action required", { bold: true, color: "#dc2626", size: 18 })
    .line((t) => t.text("Tracking number: ").bold(this.order.tracking))
    .action("Track package", `https://app.test/orders/${this.order.id}`)
    .line("Thanks for shopping with us!");
}
```

## Constructors

### Constructor

> **new MailMessage**(): `MailMessage`

#### Returns

`MailMessage`

## Methods

### subject()

> **subject**(`text`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:139](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L139)

#### Parameters

##### text

`string`

#### Returns

`this`

***

### greeting()

#### Call Signature

> **greeting**(`text`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:149](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L149)

Opening line, e.g. "Hi Ada,". Defaults to "Hello," when omitted. Accepts the same
styling forms as `line()`: a plain string, a string + `TextStyle`, or a `RichLine`
callback for mixed runs.

##### Parameters

###### text

`string`

##### Returns

`this`

#### Call Signature

> **greeting**(`text`, `style`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:150](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L150)

Opening line, e.g. "Hi Ada,". Defaults to "Hello," when omitted. Accepts the same
styling forms as `line()`: a plain string, a string + `TextStyle`, or a `RichLine`
callback for mixed runs.

##### Parameters

###### text

`string`

###### style

[`TextStyle`](../interfaces/TextStyle)

##### Returns

`this`

#### Call Signature

> **greeting**(`build`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:151](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L151)

Opening line, e.g. "Hi Ada,". Defaults to "Hello," when omitted. Accepts the same
styling forms as `line()`: a plain string, a string + `TextStyle`, or a `RichLine`
callback for mixed runs.

##### Parameters

###### build

(`t`) => `void`

##### Returns

`this`

***

### salutation()

#### Call Signature

> **salutation**(`text`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:161](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L161)

Closing line, e.g. "Regards,". Defaults to "Regards,". Accepts the same styling
forms as `line()`.

##### Parameters

###### text

`string`

##### Returns

`this`

#### Call Signature

> **salutation**(`text`, `style`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:162](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L162)

Closing line, e.g. "Regards,". Defaults to "Regards,". Accepts the same styling
forms as `line()`.

##### Parameters

###### text

`string`

###### style

[`TextStyle`](../interfaces/TextStyle)

##### Returns

`this`

#### Call Signature

> **salutation**(`build`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:163](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L163)

Closing line, e.g. "Regards,". Defaults to "Regards,". Accepts the same styling
forms as `line()`.

##### Parameters

###### build

(`t`) => `void`

##### Returns

`this`

***

### line()

#### Call Signature

> **line**(`text`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:176](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L176)

Add a paragraph. Lines before `action()` render above the button; lines after, below it.

- Plain: `.line("Welcome aboard.")`
- Whole-line style: `.line("Heads up", { bold: true, color: "#dc2626", size: 18 })`
- Mixed runs: `.line((t) => t.text("Code: ").bold("AB12").color(" (expires soon)", "#6b7280"))`

##### Parameters

###### text

`string`

##### Returns

`this`

#### Call Signature

> **line**(`text`, `style`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:177](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L177)

Add a paragraph. Lines before `action()` render above the button; lines after, below it.

- Plain: `.line("Welcome aboard.")`
- Whole-line style: `.line("Heads up", { bold: true, color: "#dc2626", size: 18 })`
- Mixed runs: `.line((t) => t.text("Code: ").bold("AB12").color(" (expires soon)", "#6b7280"))`

##### Parameters

###### text

`string`

###### style

[`TextStyle`](../interfaces/TextStyle)

##### Returns

`this`

#### Call Signature

> **line**(`build`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:178](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L178)

Add a paragraph. Lines before `action()` render above the button; lines after, below it.

- Plain: `.line("Welcome aboard.")`
- Whole-line style: `.line("Heads up", { bold: true, color: "#dc2626", size: 18 })`
- Mixed runs: `.line((t) => t.text("Code: ").bold("AB12").color(" (expires soon)", "#6b7280"))`

##### Parameters

###### build

(`t`) => `void`

##### Returns

`this`

***

### action()

> **action**(`text`, `url`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:187](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L187)

A call-to-action button. At most one per message.

#### Parameters

##### text

`string`

##### url

`string`

#### Returns

`this`

***

### html()

> **html**(`content`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:194](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L194)

Provide a fully custom HTML body, bypassing the greeting/line/action template.

#### Parameters

##### content

`string`

#### Returns

`this`

***

### text()

> **text**(`content`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:200](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L200)

Provide an explicit plain-text body (otherwise one is derived from the lines).

#### Parameters

##### content

`string`

#### Returns

`this`

***

### to()

> **to**(`address`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:205](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L205)

#### Parameters

##### address

[`AddressInput`](../type-aliases/AddressInput) \| [`AddressInput`](../type-aliases/AddressInput)[]

#### Returns

`this`

***

### cc()

> **cc**(`address`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:209](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L209)

#### Parameters

##### address

[`AddressInput`](../type-aliases/AddressInput) \| [`AddressInput`](../type-aliases/AddressInput)[]

#### Returns

`this`

***

### bcc()

> **bcc**(`address`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:213](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L213)

#### Parameters

##### address

[`AddressInput`](../type-aliases/AddressInput) \| [`AddressInput`](../type-aliases/AddressInput)[]

#### Returns

`this`

***

### from()

> **from**(`address`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:217](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L217)

#### Parameters

##### address

[`AddressInput`](../type-aliases/AddressInput)

#### Returns

`this`

***

### replyTo()

> **replyTo**(`address`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:222](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L222)

#### Parameters

##### address

[`AddressInput`](../type-aliases/AddressInput)

#### Returns

`this`

***

### attach()

> **attach**(`attachment`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:233](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L233)

Attach a file to the message.

#### Parameters

##### attachment

[`MailAttachment`](../interfaces/MailAttachment)

#### Returns

`this`

#### Example

```ts
.attach({ filename: "invoice.pdf", content: pdfBytes, contentType: "application/pdf" })
```

***

### attachFile()

> **attachFile**(`path`, `options?`): `Promise`\<`MailMessage`\>

Defined in: [notifications/src/messages/MailMessage.ts:245](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L245)

Attach a file read from disk. Reads the bytes when called, so the message is
self-contained by the time a driver sends it.

#### Parameters

##### path

`string`

##### options?

###### filename?

`string`

###### contentType?

`string`

#### Returns

`Promise`\<`MailMessage`\>

#### Example

```ts
await new MailMessage().subject("Your invoice").attachFile("./storage/invoice.pdf");
```

***

### embed()

> **embed**(`cid`, `attachment`): `this`

Defined in: [notifications/src/messages/MailMessage.ts:258](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L258)

Embed an image referenced from the HTML body as `<img src="cid:the-id">`,
rather than listing it as a download.

#### Parameters

##### cid

`string`

##### attachment

`Omit`\<[`MailAttachment`](../interfaces/MailAttachment), `"cid"` \| `"inline"`\>

#### Returns

`this`

***

### toPayload()

> **toPayload**(`defaultFrom`, `fallbackTo`): [`MailPayload`](../interfaces/MailPayload)

Defined in: [notifications/src/messages/MailMessage.ts:281](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/notifications/src/messages/MailMessage.ts#L281)

Resolve to the wire payload a driver sends. `defaultFrom` and `fallbackTo` come from
MailChannel (config default sender + the notifiable's address).

#### Parameters

##### defaultFrom

[`MailAddress`](../interfaces/MailAddress)

##### fallbackTo

[`MailAddress`](../interfaces/MailAddress)[]

#### Returns

[`MailPayload`](../interfaces/MailPayload)
