# MessageInterface

> MessageInterface is the interface that should be implemented by mail message classes.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [mail](/docs/api/yii/mail/index.md) › MessageInterface

| interface | `yii\mail\MessageInterface` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/mail/MessageInterface.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/mail/MessageInterface.php#L34) |

Implemented by 1 type:

- [`BaseMessage`](/docs/api/yii/mail/basemessage/index.md)

MessageInterface is the interface that should be implemented by mail message classes.

A message represents the settings and content of an email, such as the sender, recipient,
subject, body, etc.

Messages are sent by a [mailer](/docs/api/yii/mail/mailerinterface/index.md), like the following,

```
Yii::$app->mailer->compose()
    ->setFrom('from@domain.com')
    ->setTo($form->email)
    ->setSubject($form->subject)
    ->setTextBody('Plain text content')
    ->setHtmlBody('<b>HTML content</b>')
    ->send();
```

See also: [`MailerInterface`](/docs/api/yii/mail/mailerinterface/index.md).

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`attach()`](#method-attach) abstract | Attaches existing file to the email message. | `MessageInterface` |
| [`attachContent()`](#method-attachContent) abstract | Attach specified content as file for the email message. | `MessageInterface` |
| [`embed()`](#method-embed) abstract | Attach a file and return it's CID source. | `MessageInterface` |
| [`embedContent()`](#method-embedContent) abstract | Attach a content as file and return it's CID source. | `MessageInterface` |
| [`getBcc()`](#method-getBcc) abstract | Returns the Bcc (hidden copy receiver) addresses of this message. | `MessageInterface` |
| [`getCc()`](#method-getCc) abstract | Returns the Cc (additional copy receiver) addresses of this message. | `MessageInterface` |
| [`getCharset()`](#method-getCharset) abstract | Returns the character set of this message. | `MessageInterface` |
| [`getFrom()`](#method-getFrom) abstract | Returns the message sender. | `MessageInterface` |
| [`getReplyTo()`](#method-getReplyTo) abstract | Returns the reply-to address of this message. | `MessageInterface` |
| [`getSubject()`](#method-getSubject) abstract | Returns the message subject. | `MessageInterface` |
| [`getTo()`](#method-getTo) abstract | Returns the message recipient(s). | `MessageInterface` |
| [`send()`](#method-send) abstract | Sends this email message. | `MessageInterface` |
| [`setBcc()`](#method-setBcc) abstract | Sets the Bcc (hidden copy receiver) addresses of this message. | `MessageInterface` |
| [`setCc()`](#method-setCc) abstract | Sets the Cc (additional copy receiver) addresses of this message. | `MessageInterface` |
| [`setCharset()`](#method-setCharset) abstract | Sets the character set of this message. | `MessageInterface` |
| [`setFrom()`](#method-setFrom) abstract | Sets the message sender. | `MessageInterface` |
| [`setHtmlBody()`](#method-setHtmlBody) abstract | Sets message HTML content. | `MessageInterface` |
| [`setReplyTo()`](#method-setReplyTo) abstract | Sets the reply-to address of this message. | `MessageInterface` |
| [`setSubject()`](#method-setSubject) abstract | Sets the message subject. | `MessageInterface` |
| [`setTextBody()`](#method-setTextBody) abstract | Sets message plain text content. | `MessageInterface` |
| [`setTo()`](#method-setTo) abstract | Sets the message recipient(s). | `MessageInterface` |
| [`toString()`](#method-toString) abstract | Returns string representation of this message. | `MessageInterface` |

## Method details

### attach() <span id="method-attach"></span>

```php
abstract public function attach(string $fileName, array $options = []): $this
```

Attaches existing file to the email message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileName` | `string` | Full file name |
| `$options` | `array` | Options for embed file. Valid options are: - fileName: name, which should be used to attach file. - contentType: attached file MIME type. |

**Returns**

`$this`: Self reference.

### attachContent() <span id="method-attachContent"></span>

```php
abstract public function attachContent(string $content, array $options = []): $this
```

Attach specified content as file for the email message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string` | Attachment file content. |
| `$options` | `array` | Options for embed file. Valid options are: - fileName: name, which should be used to attach file. - contentType: attached file MIME type. |

**Returns**

`$this`: Self reference.

### embed() <span id="method-embed"></span>

```php
abstract public function embed(string $fileName, array $options = []): string
```

Attach a file and return it's CID source.

This method should be used when embedding images or other data in a message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fileName` | `string` | File name. |
| `$options` | `array` | Options for embed file. Valid options are: - fileName: name, which should be used to attach file. - contentType: attached file MIME type. |

**Returns**

`string`: Attachment CID.

### embedContent() <span id="method-embedContent"></span>

```php
abstract public function embedContent(string $content, array $options = []): string
```

Attach a content as file and return it's CID source.

This method should be used when embedding images or other data in a message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$content` | `string` | Attachment file content. |
| `$options` | `array` | Options for embed file. Valid options are: - fileName: name, which should be used to attach file. - contentType: attached file MIME type. |

**Returns**

`string`: Attachment CID.

### getBcc() <span id="method-getBcc"></span>

```php
abstract public function getBcc(): string|array
```

Returns the Bcc (hidden copy receiver) addresses of this message.

**Returns**

`string|array`: The Bcc (hidden copy receiver) addresses of this message.

### getCc() <span id="method-getCc"></span>

```php
abstract public function getCc(): string|array
```

Returns the Cc (additional copy receiver) addresses of this message.

**Returns**

`string|array`: The Cc (additional copy receiver) addresses of this message.

### getCharset() <span id="method-getCharset"></span>

```php
abstract public function getCharset(): string
```

Returns the character set of this message.

**Returns**

`string`: The character set of this message.

### getFrom() <span id="method-getFrom"></span>

```php
abstract public function getFrom(): string|array
```

Returns the message sender.

**Returns**

`string|array`: The sender

### getReplyTo() <span id="method-getReplyTo"></span>

```php
abstract public function getReplyTo(): string|array
```

Returns the reply-to address of this message.

**Returns**

`string|array`: The reply-to address of this message.

### getSubject() <span id="method-getSubject"></span>

```php
abstract public function getSubject(): string
```

Returns the message subject.

**Returns**

`string`: The message subject

### getTo() <span id="method-getTo"></span>

```php
abstract public function getTo(): string|array
```

Returns the message recipient(s).

**Returns**

`string|array`: The message recipients

### send() <span id="method-send"></span>

```php
abstract public function send(yii\mail\MailerInterface|null $mailer = null): bool
```

Sends this email message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$mailer` | [`MailerInterface`](/docs/api/yii/mail/mailerinterface/index.md)`\|null` | The mailer that should be used to send this message. If null, the "mailer" application component will be used instead. |

**Returns**

`bool`: Whether this message is sent successfully.

### setBcc() <span id="method-setBcc"></span>

```php
abstract public function setBcc(string|array $bcc): $this
```

Sets the Bcc (hidden copy receiver) addresses of this message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$bcc` | `string\|array` | Hidden copy receiver email address. You may pass an array of addresses if multiple recipients should receive this message. You may also specify receiver name in addition to email address using format: `[email => name]`. |

**Returns**

`$this`: Self reference.

### setCc() <span id="method-setCc"></span>

```php
abstract public function setCc(string|array $cc): $this
```

Sets the Cc (additional copy receiver) addresses of this message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$cc` | `string\|array` | Copy receiver email address. You may pass an array of addresses if multiple recipients should receive this message. You may also specify receiver name in addition to email address using format: `[email => name]`. |

**Returns**

`$this`: Self reference.

### setCharset() <span id="method-setCharset"></span>

```php
abstract public function setCharset(string $charset): $this
```

Sets the character set of this message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$charset` | `string` | Character set name. |

**Returns**

`$this`: Self reference.

### setFrom() <span id="method-setFrom"></span>

```php
abstract public function setFrom(string|array $from): $this
```

Sets the message sender.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$from` | `string\|array` | Sender email address. You may pass an array of addresses if this message is from multiple people. You may also specify sender name in addition to email address using format: `[email => name]`. |

**Returns**

`$this`: Self reference.

### setHtmlBody() <span id="method-setHtmlBody"></span>

```php
abstract public function setHtmlBody(string $html): $this
```

Sets message HTML content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$html` | `string` | Message HTML content. |

**Returns**

`$this`: Self reference.

### setReplyTo() <span id="method-setReplyTo"></span>

```php
abstract public function setReplyTo(string|array $replyTo): $this
```

Sets the reply-to address of this message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$replyTo` | `string\|array` | The reply-to address. You may pass an array of addresses if this message should be replied to multiple people. You may also specify reply-to name in addition to email address using format: `[email => name]`. |

**Returns**

`$this`: Self reference.

### setSubject() <span id="method-setSubject"></span>

```php
abstract public function setSubject(string $subject): $this
```

Sets the message subject.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$subject` | `string` | Message subject |

**Returns**

`$this`: Self reference.

### setTextBody() <span id="method-setTextBody"></span>

```php
abstract public function setTextBody(string $text): $this
```

Sets message plain text content.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$text` | `string` | Message plain text content. |

**Returns**

`$this`: Self reference.

### setTo() <span id="method-setTo"></span>

```php
abstract public function setTo(string|array $to): $this
```

Sets the message recipient(s).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$to` | `string\|array` | Receiver email address. You may pass an array of addresses if multiple recipients should receive this message. You may also specify receiver name in addition to email address using format: `[email => name]`. |

**Returns**

`$this`: Self reference.

### toString() <span id="method-toString"></span>

```php
abstract public function toString(): string
```

Returns string representation of this message.

**Returns**

`string`: The string representation of this message.