Skip to content
Last updated

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

NameDescription
HEADER_MARKER
FOOTER_MARKER
RFC3834_HEADERS
RECIPIENT_TYPES

Methods

__construct

public function __construct()

setEmailLogId

public function setEmailLogId($id)

Set the ID of the email relative to tblemails.id

Returns $this

createFromTemplate

public static function createFromTemplate(Template $template)

Build an Email Message from a predefined Template

Parameters

TypeNameDescription
Template$template

Returns Message

setType

public function setType($type)

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

Parameters

TypeNameDescription
string$type

Returns $this

getType

public function getType()

Retrieve the Message type

Returns string

setTemplateName

public function setTemplateName($templateName)

Set the Message template name

Parameters

TypeNameDescription
string$templateName

Returns $this

getTemplateName

public function getTemplateName()

Retrieve the Message template name

Returns string

addRecipient

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

Add a recipient for the Message

Parameters

TypeNameDescription
string$typeEither 'to', 'cc' or 'bcc'
string$emailRecipient Email Address are normalized (lower-cased)
string$nameRecipient Name

Returns $this

getAllRecipients

public function getAllRecipients(): array

hasRecipientByEmail

public function hasRecipientByEmail($email): bool

clearRecipients

public function clearRecipients($type)

Clear recipients for a specific kind

Parameters

TypeNameDescription
string$typeEither 'to', 'cc' or 'bcc'

Returns $this

setFromName

public function setFromName($name)

Set the "From" name for the Message

Parameters

TypeNameDescription
string$name

Returns $this

getFromName

public function getFromName()

Retrieve the "From" name for the message

Returns string

setFromEmail

public function setFromEmail($email)

Set the sender Email Address for the Message

Parameters

TypeNameDescription
string$email

Returns $this

getFromEmail

public function getFromEmail()

Retrieve the sender Email Address for the Message

Returns mixed

getFormattedFrom

public function getFormattedFrom()

Retrieve the sender Email Address and Name formatted for sending. e.g. 'John Doe<[email protected]>'

Returns string

setReplyTo

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

Set the Reply To address and name for the Message

Parameters

TypeNameDescription
string$email
string$name

Returns $this

getReplyTo

public function getReplyTo()

Retrieve the Reply To address and name for the Message

Returns array

getReplyToName

public function getReplyToName()

getReplyToEmail

public function getReplyToEmail()

getFormattedReplyTo

public function getFormattedReplyTo(): string

Retrieve the reply-to Email Address and Name formatted for sending. e.g. 'John Doe<[email protected]>'

Returns string

getRecipients

public function getRecipients($type)

Retrieve the recipients for the Message

Parameters

TypeNameDescription
string$typeEither 'to', 'cc' or 'bcc'

Returns array

getRecipientsEmailAddress

public function getRecipientsEmailAddress($type)

Retrieve the recipients email addresses for the Message

Parameters

TypeNameDescription
string$typeEither 'to', 'cc' or 'bcc'

Returns string[]

getFormattedRecipients

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

Retrieve the Email Addresses and Names formatted for sending. e.g. 'John Doe <[email protected]>'

Parameters

TypeNameDescription
string|null$typeIf $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

public function setSubject($subject)

Set the subject of the Message

Parameters

TypeNameDescription
string$subject

Returns $this

getSubject

public function getSubject()

Retrieve the subject of the Message

Returns string

setBodyAndPlainText

public function setBodyAndPlainText($body)

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

Parameters

TypeNameDescription
string$body

Returns $this

setBody

public function setBody($body)

Set the HTML body of the Message

Parameters

TypeNameDescription
string$body

Returns $this

applyGlobalWrapper

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

TypeNameDescription
string$blob

Returns string

appendGlobalHeader

public function appendGlobalHeader(string $blob): string

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

Parameters

TypeNameDescription
string$blob

Returns string

appendGlobalFooter

public function appendGlobalFooter(string $blob): string

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

Parameters

TypeNameDescription
string$blob

Returns string

setBodyFromSmarty

public function setBodyFromSmarty($body)

Set the Message body from a parsed Smarty template

Parameters

TypeNameDescription
string$body

Returns $this

getBody

public function getBody()

Retrieve the HTML body of the Message

Returns string

getBodyWithoutCSS

public function getBodyWithoutCSS()

Retrieve the HTML body of the Message without Global CSS included

Returns string

setPlainText

public function setPlainText($text)

Set the Plain-Text body of the Message

Parameters

TypeNameDescription
string$text

Returns $this

getPlainText

public function getPlainText()

Retrieve the Plain-Text body of the Message

Returns string

addStringAttachment

public function addStringAttachment($filename, $data)

Add string attachment

Parameters

TypeNameDescription
string$filenameThe name of the attachment
string$dataThe attachment data

Returns $this

addFileAttachment

public function addFileAttachment($filename, $filepath)

Add attachment from file system

Parameters

TypeNameDescription
string$filenameThe name of the file
string$filepathThe path to the file

Returns $this

getAttachments

public function getAttachments()

Retrieve attachments for the Message

Returns array

getAttachmentNames

public function getAttachmentNames(): array

Retrieve the filenames for attachments associated with the Message

Returns array

hasRecipients

public function hasRecipients()

Check if the Message has at least one recipient defined

Returns bool

saveToEmailLog

public function saveToEmailLog(int $userId)

Save the Message to the Email Log

Parameters

TypeNameDescription
int$userIdThe User ID receiving the Message

Returns int — Email Log Entry ID

setHeader

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

getHeaders

public function getHeaders(): array