Index

Mail

MMO\core\server ยท 1 symbol ยท generated 30 Sep 2026

class

Mail

Mail.php:89

Build and send a MIME email via PHP's mail() (which shells out to sendmail), with support for HTML/plain alternative bodies, attachments, inline (data-URI) images, and DKIM signing. Adapted from PHPMailer.

class MMO\core\server\Mail use MMO\core\classes\tWithHeaders
Namespace
MMO\core\server
Uses traits
tWithHeaders
$mail = new Mail('to@example.com', 'Subject', '<p>Hello</p>', 'from@example.com'); $mail->bodyAlt = 'Hello (plain text)'; $mail->addAttachment('/path/to/file.pdf'); $mail->send();

Index

MAX_LINE_LENGTH
attachments
body
bodyAlt
dkimDomainDKIM signing domain name.
DKIMHeaders static
dkimIdentityDKIM Identity: Usually the email address used as the source of the email.
dkimPassphraseDKIM passphrase if your key is encrypted.
dkimPrivateFileDKIM private key file path.
dkimSelectorDKIM selector.
headers
ikeys
recipients
subject
uniqueididentifiant for boundaries
addAttachment($path, $name, $type, $disposition, $id)Add an attachment from a file.
addBCC($address, $name)
addCC($address, $name)
addConfirmReading($address, $name)
addHeader($name, $value)Append a value to a header, keeping any value(s) already set (as opposed to setHeader, which replaces).
addInlineAttachment($cid, $path, $name, $type)Add an inline attachment from a file.
addRecipient($kind, $address, $name)
addReplyTo($address, $name)
addTo($address, $name)Add one To recipient (on top of any already set -- setTo to replace them all).
clearAttachments()Remove every attachment (regular and inline).
debugName()
DKIMEncodeBody($body)Generate a DKIM canonicalization body.
DKIMEncodeHeaders($headers, $subject, $body)
DKIMEncodeRelaxedHeader($header)Generate a DKIM canonicalization header.
DKIMEncodeText($txt)Quoted-Printable-encode a DKIM header.
DKIMSign($header)
encodeHeader($text, $secure)Encode text to be placed in the header.
getBCC()
getCC()
getCharset()
getConfirmReading()
getContentExtension()
getContentType($single)
getFrom()
getHeader($name)Retrieves a message header value by the given case-insensitive name.
getHeaderLine($name)Retrieves a comma-separated string of the values for a single header.
getHeaders($boundaries)Build the full header set for send: the manually-set headers (tWithHeaders) plus one line per recipient kind (except 'To', which send passes to mail() separately)โ€ฆ
getOrigin()Get origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.
getPriority()
getReplyTo()
getTo()
getUserAgent()
hasAttachment()
hasBCC($address)
hasCC($address)
hasConfirmReading($address)
hasHeader($name)Checks if a header exists by the given case-insensitive name.
hasInlineAttachment()
hasRecipient($kind, $address)
hasReplyTo($address)
hasTo($address)
isHTML()
isIe()
isIphone()
isJSON()
isMac()
isMobile()
isMultipart()
isWindows()
replaceInlineImage($text)Replace all images passed as datauri (<img src="data:....>) to inline attachment
secureHeader($text)Strip CR/LF from $text -- prevents header injection when embedding untrusted content (e.g.
send()Build the full MIME message (headers, multipart body, DKIM signature if $dkimIdentity is set) and send it via PHP's mail().
setBCC($addresses)
setBody($body)Set the body and replace all uri images into inline attachment
setCacheControl($cache, $pragma)Set the Cache-Control header (and, optionally, the legacy Pragma header alongside it).
setCC($addresses)
setConfirmReading($addresses)
setContentType($type, $charset)Set the Content-Type header, appending a charset parameter (unless $charset is null/empty).
setExpires($time)Set the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.
setFrom($address, $name)Replaces the From address (there is only ever one).
setHeader($name, $value)Set header, replacing any previous value(s) for the same (case-insensitively matched) header name.
setPriority($value)Email priority.
setRecipients($kind, $addresses)Set recipients of a kind
setReplyTo($addresses)
setTo($addresses)Replace every To recipient.
unsetHeader($name)Remove a header entirely.
__construct($to, $subject, $body, $from, $contentType, $charset)

Constants1

MAX_LINE_LENGTH

const MAX_LINE_LENGTH = 75

Properties14

attachments

protected array $attachments = []
Type
array

body

string $body = ''
Type
string

dkimDomain

DKIM signing domain name.

string $dkimDomain = ''
Type
string

DKIMHeaders

static
static array $DKIMHeaders = [โ€ฆ10 items]
Type
array

dkimIdentity

DKIM Identity: Usually the email address used as the source of the email.

?string $dkimIdentity = null
Type
null | string

dkimPassphrase

DKIM passphrase if your key is encrypted.

?string $dkimPassphrase = null
Type
null | string

dkimPrivateFile

DKIM private key file path.

?string $dkimPrivateFile = null
Type
null | string

dkimSelector

DKIM selector.

string $dkimSelector = 'mail'
Type
string

headers

protected $headers = []

ikeys

protected $ikeys = []

recipients

protected array $recipients = []
Type
array

subject

string $subject = ''
Type
string

uniqueid

identifiant for boundaries

protected string $uniqueid
Type
string

Methods67

addAttachment($path, $name, $type, $disposition, $id)

Mail.php:470

Add an attachment from a file.

function addAttachment( string $path, ?string $name = null, ?string $type = null, string $disposition = 'attachment', ?string $id = null ): void
$pathstringPath to the attachment
$name = nullnull | stringOverrides the attachment filename
$type = nullnull | stringFile MIME type (by default mapped from the $path filename's extension)
$disposition = 'attachment'stringDisposition to use: inline or attachment
$id = nullnull | stringthe identifaint to use for inline attachment
returnsvoid

addConfirmReading($address, $name)

Mail.php:424
function addConfirmReading(string $address, ?string $name = null)
$addressstring
$name = nullnull | string
returns

addHeader($name, $value)

tWithHeaders.php:166

Append a value to a header, keeping any value(s) already set (as opposed to setHeader, which replaces).

function addHeader(string $name, array|string $value): self
$namestring
$valuearray | stringa plain string is appended as a single new value (NOT split on ',', unlike setHeader)
returnsself
$msg->addHeader('X-Tags', 'a'); $msg->addHeader('X-Tags', 'b'); $msg->getHeader('X-Tags'); // ['a', 'b']

addInlineAttachment($cid, $path, $name, $type)

Mail.php:491

Add an inline attachment from a file.

function addInlineAttachment(string $cid, string $path, ?string $name = null, ?string $type = null): voidThis can include images, sounds, and just about any other document type. These differ from 'regular' attachments in that they are intended to be displayed inline with the message, not just attached for download. This is used in HTML messages that embed the images the HTML refers to using the `$cid` value in `img` tags, for example `<img src="cid:mylogo">`.
$cidstringContent ID of the attachment; Use this to reference the content when using an embedded image in HTML
$pathstringPath to the attachment
$name = nullnull | stringOverrides the attachment filename
$type = nullnull | stringFile MIME type (by default mapped from the $path filename's extension)
returnsvoid

addRecipient($kind, $address, $name)

Mail.php:276
protected function addRecipient(string $kind, string $address, ?string $name = null)
$kindstringe.g. 'To', 'Cc', 'Bcc', 'Reply-To', 'From'. @param string $address secureHeader'd before storing.
$addressstring
$name = nullnull | string
returns

addReplyTo($address, $name)

Mail.php:404
function addReplyTo(string $address, ?string $name = null)
$addressstring
$name = nullnull | string
returns

addTo($address, $name)

Mail.php:335

Add one To recipient (on top of any already set -- setTo to replace them all).

function addTo(string $address, ?string $name = null)
$addressstring
$name = nullnull | string
returns

clearAttachments()

Mail.php:443

Remove every attachment (regular and inline).

function clearAttachments()
returns

DKIMEncodeHeaders($headers, $subject, $body)

Mail.php:570
protected function DKIMEncodeHeaders(array $headers, string $subject, string $body): string
$headersarray
$subjectstring
$bodystring
returnsstring

DKIMEncodeText($txt)

Mail.php:503

Quoted-Printable-encode a DKIM header.

protected function DKIMEncodeText(string $txt): string
$txtstring
returnsstring

encodeHeader($text, $secure)

Mail.php:226

Encode text to be placed in the header.

protected function encodeHeader(string $text, bool $secure = true)In order to encode quickly, and for simplicity, we only use base64 (B) encoding, leaving Q encoding.
$textstringthe text to encode
$secure = trueboolif true, remove eol from text
returns

getCharset()

tWithHeaders.php:55
function getCharset(): ?string
returnsnull | stringthe charset parameter of the Content-Type header, or null if not present
$msg->setContentType('text/html'); $msg->getCharset(); // 'utf-8' (the default charset)

getContentExtension()

tWithHeaders.php:63
function getContentExtension(): ?string
returnsnull | stringthe file extension conventionally associated with the Content-Type header (e.g. 'html', 'json')
$msg->setContentType('text/html'); $msg->getContentExtension(); // 'html'

getContentType($single)

tWithHeaders.php:47
function getContentType(bool $single = true): array|string
$single = trueboolif true, return only the media type (e.g. 'text/html'); if false, every parameter too
returnsarray | string
$msg->setContentType('text/html'); $msg->getContentType(); // 'text/html'

getHeader($name)

tWithHeaders.php:254

Retrieves a message header value by the given case-insensitive name.

function getHeader(string $name): arrayThis method returns an array of all the header values of the given case-insensitive header name. If the header does not appear in the message, this method MUST return an empty array.
$namestringCase-insensitive header field name.
returnsarrayAn array of string values as provided for the given header. If the header does not appear in the message, this method MUST return an empty array.
$msg->addHeader('Accept', 'text/html'); $msg->addHeader('Accept', 'application/json'); $msg->getHeader('Accept'); // ['text/html', 'application/json']

getHeaderLine($name)

tWithHeaders.php:281

Retrieves a comma-separated string of the values for a single header.

function getHeaderLine(string $name): stringThis method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma. NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating. If the header does not appear in the message, this method MUST return an empty string.
$namestringCase-insensitive header field name.
returnsstringA string of values as provided for the given header concatenated together using a comma. If the header does not appear in the message, this method MUST return an empty string.
$msg->addHeader('Accept', 'text/html'); $msg->addHeader('Accept', 'application/json'); $msg->getHeaderLine('Accept'); // 'text/html, application/json'

getHeaders($boundaries)

Mail.php:635

Build the full header set for send: the manually-set headers (tWithHeaders) plus one line per recipient kind (except 'To', which send passes to mail() separately) and the right Content-Type for whatever MIME structure the message needs (isMultipart).

function getHeaders(array &$boundaries = []): array
$boundaries = []array
returnsarrayheader name => value (or list of values).

getOrigin()

tWithHeaders.php:35

Get origin of the request by looking at 'Origin', then 'Referer', then 'Sender' in the request's headers.

function getOrigin(): string
returnsstring'' if none of those headers are set
$msg->setHeader('Origin', 'https://example.com'); $msg->getOrigin(); // 'https://example.com'

getTo()

Mail.php:327
function getTo(): array
returnsarrayaddress => name (name is null when not given -- addTo).
$mail->addTo('a@x.com', 'Alice'); $mail->getTo(); // ['a@x.com' => 'Alice']

hasConfirmReading($address)

Mail.php:434
function hasConfirmReading(?string $address = null)
$address = nullnull | string
returns

hasHeader($name)

tWithHeaders.php:231

Checks if a header exists by the given case-insensitive name.

function hasHeader(string $name): bool
$namestringCase-insensitive header field name.
returnsboolReturns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.
$msg->setHeader('Content-Type', 'text/html'); $msg->hasHeader('content-type'); // true

hasRecipient($kind, $address)

Mail.php:303
protected function hasRecipient(string $kind, ?string $address = null): bool
$kindstring
$address = nullnull | stringif given, check for this specific address; if null, check whether ANY address of $kind is set.
returnsbool

hasReplyTo($address)

Mail.php:414
function hasReplyTo(?string $address = null)
$address = nullnull | string
returns

hasTo($address)

Mail.php:353
function hasTo(?string $address = null)
$address = nullnull | stringif given, check for this specific address; if null, check whether any To recipient is set.
returns

isIphone()

tWithHeaders.php:77
function isIphone(): bool
returnsbooltrue if the User-Agent looks like mobile Safari on iPhone

isMobile()

tWithHeaders.php:105
function isMobile(): bool
returnsbooltrue if the User-Agent looks like a mobile device/browser

isMultipart()

Mail.php:458
function isMultipart(): bool
returnsboolwhether the message will be sent as MIME multipart (attachments, inline images, or an alternative body).

replaceInlineImage($text)

Mail.php:248

Replace all images passed as datauri (<img src="data:....>) to inline attachment

protected function replaceInlineImage(string $text): string
$textstring
returnsstring

secureHeader($text)

Mail.php:215

Strip CR/LF from $text -- prevents header injection when embedding untrusted content (e.g. a recipient name) into a header line.

protected function secureHeader(string $text): string
$textstring
returnsstring

send()

Mail.php:685

Build the full MIME message (headers, multipart body, DKIM signature if $dkimIdentity is set) and send it via PHP's mail().

function send()
returns
throwsif no From/To address or body is set, mail() itself fails, or (via DKIMSign) DKIM signing fails.

setBody($body)

Mail.php:208

Set the body and replace all uri images into inline attachment

function setBody(string $body): void
$bodystring
returnsvoid

setCacheControl($cache, $pragma)

tWithHeaders.php:305

Set the Cache-Control header (and, optionally, the legacy Pragma header alongside it).

function setCacheControl($cache, $pragma = null): self
$cachecache method 'public', 'private', 'no-cache', 'no-store'
$pragma = nullif given, also sets the Pragma header (e.g. 'no-cache', for HTTP/1.0 clients)
returnsself
$msg->setCacheControl('no-cache', 'no-cache');

setConfirmReading($addresses)

Mail.php:429
function setConfirmReading(array|string $addresses)
$addressesarray | string
returns

setContentType($type, $charset)

tWithHeaders.php:293

Set the Content-Type header, appending a charset parameter (unless $charset is null/empty).

function setContentType(string $type, ?string $charset = 'utf-8'): self
$typestringthe media type, e.g. 'text/html' or one of the framework's MIME_* constants
$charset = 'utf-8'null | stringappended as ;charset=$charset; pass null/'' to omit it entirely
returnsself
$msg->setContentType('text/html'); $msg->getHeaderLine('Content-Type'); // 'text/html;charset=utf-8'$msg->setContentType('application/octet-stream', null); // no charset appended

setExpires($time)

tWithHeaders.php:317

Set the Expires header from a unix timestamp, formatted as an RFC 7231 HTTP-date.

function setExpires(?int $time): self
$timenull | inta unix timestamp; 0 (or any past time) tells clients/caches the response is already stale
returnsself
$msg->setExpires(0); $msg->getHeaderLine('Expires'); // 'Thu, 01 Jan 1970 00:00:00 GMT'

setFrom($address, $name)

Mail.php:317

Replaces the From address (there is only ever one).

function setFrom(string $address, ?string $name = null)
$addressstring
$name = nullnull | string
returns

setHeader($name, $value)

tWithHeaders.php:143

Set header, replacing any previous value(s) for the same (case-insensitively matched) header name.

function setHeader(string $name, array|string $value): self
$namestring
$valuearray | stringif a string, it is split on ',' into the multiple values PSR-7 expects a header to hold (e.g. 'a,b' becomes ['a', 'b']) -- pass an array (even a single-element one, e.g. [$value]) if $value may itself legitimately contain a comma that must NOT be split (an HTTP-date value like 'Thu, 01 Jan ...' is the classic example -- see setExpires)
returnsself
$msg->setHeader('Content-Type', 'text/html');$msg->setHeader('Accept', 'text/html,application/json'); // stored as ['text/html', 'application/json']

setPriority($value)

Mail.php:193

Email priority.

function setPriority(?int $value)When null, the header is not set at all.
$valuenull | intnull (default), 1 = High, 3 = Normal, 5 = low.
returns

setRecipients($kind, $addresses)

Mail.php:287

Set recipients of a kind

protected function setRecipients(string $kind, array|string $addresses)
$kindstringthe kind of recipient (to,cc,bcc,reply-to)
$addressesarray | stringarray<name: address> It also can be a list of addresses (name is consider null)
returns

setTo($addresses)

Mail.php:345

Replace every To recipient.

function setTo(array|string $addresses)
$addressesarray | string['name' => 'address', ...] (or a plain list, name null), or a single "Name <address>, address2, ..."-style string (decodeRecipients).
returns

unsetHeader($name)

tWithHeaders.php:179

Remove a header entirely.

function unsetHeader(string $name): self
$namestring
returnsself
$msg->unsetHeader('X-Tags'); $msg->hasHeader('X-Tags'); // false

__construct($to, $subject, $body, $from, $contentType, $charset)

Mail.php:153
function __construct( array|string|null $to = null, string $subject = '', string $body = '', ?string $from = null, string $contentType = 'text/html', string $charset = 'utf-8' )
$to = nullarray | string | nullsetTo.
$subject = ''string
$body = ''stringsetBody -- also inline-extracts any <img src="data:..."> into an attachment.
$from = nullnull | stringsetFrom; defaults to the sendmail_from ini setting if not given.
$contentType = 'text/html'string
$charset = 'utf-8'string