# WHMCS\Mail\Message

`Class`

An Email Message

Messages are sent out for various reasons, including for events such as
invoice creation and Email Campaigns.

The Message class allows you to interact with the message to update any
respective properties before use.

## Constants

| Name | Description |
|  --- | --- |
| `HEADER_MARKER` |  |
| `FOOTER_MARKER` |  |
| `RFC3834_HEADERS` |  |
| `RECIPIENT_TYPES` |  |


## Methods

### __construct

```php
public function __construct()
```

### setEmailLogId

```php
public function setEmailLogId($id)
```

Set the ID of the email relative to tblemails.id

**Returns** `$this`

### createFromTemplate

```php
public static function createFromTemplate(Template $template)
```

Build an Email Message from a predefined Template

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `Template` | `$template` |  |


**Returns** `Message`

### setType

```php
public function setType($type)
```

Set the Email Type. e.g. 'general'

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$type` |  |


**Returns** `$this`

### getType

```php
public function getType()
```

Retrieve the Message type

**Returns** `string`

### setTemplateName

```php
public function setTemplateName($templateName)
```

Set the Message template name

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$templateName` |  |


**Returns** `$this`

### getTemplateName

```php
public function getTemplateName()
```

Retrieve the Message template name

**Returns** `string`

### addRecipient

```php
public function addRecipient(string $type, string $email, string $name = '')
```

Add a recipient for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$type` | Either 'to', 'cc' or 'bcc' |
| `string` | `$email` | Recipient Email Address are normalized (lower-cased) |
| `string` | `$name` | Recipient Name |


**Returns** `$this`

### getAllRecipients

```php
public function getAllRecipients(): array
```

### hasRecipientByEmail

```php
public function hasRecipientByEmail($email): bool
```

### clearRecipients

```php
public function clearRecipients($type)
```

Clear recipients for a specific kind

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$type` | Either 'to', 'cc' or 'bcc' |


**Returns** `$this`

### setFromName

```php
public function setFromName($name)
```

Set the "From" name for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$name` |  |


**Returns** `$this`

### getFromName

```php
public function getFromName()
```

Retrieve the "From" name for the message

**Returns** `string`

### setFromEmail

```php
public function setFromEmail($email)
```

Set the sender Email Address for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$email` |  |


**Returns** `$this`

### getFromEmail

```php
public function getFromEmail()
```

Retrieve the sender Email Address for the Message

**Returns** `mixed`

### getFormattedFrom

```php
public function getFormattedFrom()
```

Retrieve the sender Email Address and Name formatted for sending. e.g. 'John Doe<john@example.net>'

**Returns** `string`

### setReplyTo

```php
public function setReplyTo($email, $name = '')
```

Set the Reply To address and name for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$email` |  |
| `string` | `$name` |  |


**Returns** `$this`

### getReplyTo

```php
public function getReplyTo()
```

Retrieve the Reply To address and name for the Message

**Returns** `array`

### getReplyToName

```php
public function getReplyToName()
```

### getReplyToEmail

```php
public function getReplyToEmail()
```

### getFormattedReplyTo

```php
public function getFormattedReplyTo(): string
```

Retrieve the reply-to Email Address and Name formatted for sending. e.g. 'John Doe<john@example.net>'

**Returns** `string`

### getRecipients

```php
public function getRecipients($type)
```

Retrieve the recipients for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$type` | Either 'to', 'cc' or 'bcc' |


**Returns** `array`

### getRecipientsEmailAddress

```php
public function getRecipientsEmailAddress($type)
```

Retrieve the recipients email addresses for the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$type` | Either 'to', 'cc' or 'bcc' |


**Returns** `string[]`

### getFormattedRecipients

```php
public function getFormattedRecipients(string $type = null): array
```

Retrieve the Email Addresses and Names formatted for sending. e.g. 'John Doe <john@example.net>'

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string|null` | `$type` | If $type is null, recipients across all types will be returned. Otherwise, only recipients for the provided $type |


**Returns** `array` — Recipients or an empty array if no recipients are present

### setSubject

```php
public function setSubject($subject)
```

Set the subject of the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$subject` |  |


**Returns** `$this`

### getSubject

```php
public function getSubject()
```

Retrieve the subject of the Message

**Returns** `string`

### setBodyAndPlainText

```php
public function setBodyAndPlainText($body)
```

Set the Message body. Providing HTML will set both HTML and Plain-Text

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$body` |  |


**Returns** `$this`

### setBody

```php
public function setBody($body)
```

Set the HTML body of the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$body` |  |


**Returns** `$this`

### applyGlobalWrapper

```php
public function applyGlobalWrapper(string $blob): string
```

If defined and not already present, wrap $blob in any header and/or footer
defined by global settings.

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$blob` |  |


**Returns** `string`

### appendGlobalHeader

```php
public function appendGlobalHeader(string $blob): string
```

If defined and not already present, wrap $blob in any header defined by global settings.

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$blob` |  |


**Returns** `string`

### appendGlobalFooter

```php
public function appendGlobalFooter(string $blob): string
```

If defined and not already present, wrap $blob in any footer defined by global settings.

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$blob` |  |


**Returns** `string`

### setBodyFromSmarty

```php
public function setBodyFromSmarty($body)
```

Set the Message body from a parsed Smarty template

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$body` |  |


**Returns** `$this`

### getBody

```php
public function getBody()
```

Retrieve the HTML body of the Message

**Returns** `string`

### getBodyWithoutCSS

```php
public function getBodyWithoutCSS()
```

Retrieve the HTML body of the Message without Global CSS included

**Returns** `string`

### setPlainText

```php
public function setPlainText($text)
```

Set the Plain-Text body of the Message

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$text` |  |


**Returns** `$this`

### getPlainText

```php
public function getPlainText()
```

Retrieve the Plain-Text body of the Message

**Returns** `string`

### addStringAttachment

```php
public function addStringAttachment($filename, $data)
```

Add string attachment

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$filename` | The name of the attachment |
| `string` | `$data` | The attachment data |


**Returns** `$this`

### addFileAttachment

```php
public function addFileAttachment($filename, $filepath)
```

Add attachment from file system

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `string` | `$filename` | The name of the file |
| `string` | `$filepath` | The path to the file |


**Returns** `$this`

### getAttachments

```php
public function getAttachments()
```

Retrieve attachments for the Message

**Returns** `array`

### getAttachmentNames

```php
public function getAttachmentNames(): array
```

Retrieve the filenames for attachments associated with the Message

**Returns** `array`

### hasRecipients

```php
public function hasRecipients()
```

Check if the Message has at least one recipient defined

**Returns** `bool`

### saveToEmailLog

```php
public function saveToEmailLog(int $userId)
```

Save the Message to the Email Log

**Parameters**

| Type | Name | Description |
|  --- | --- | --- |
| `int` | `$userId` | The User ID receiving the Message |


**Returns** `int` — Email Log Entry ID

### setHeader

```php
public function setHeader(string $name, string $value = ''): self
```

### getHeaders

```php
public function getHeaders(): array
```