CoolCMS API

SendEmailTrait uses trait:short

Table of Contents

Properties

$siteBaseUrl  : SiteBaseUrl|null
Website address for links in emails ({@see SiteBaseUrl}); `null` leaves links unchanged (manager built without DI, SMTP test, queue). Filled by {@see \App\UI\Api\System\Models\Managers\EmailManager}.

Methods

disabledEmailResult()  : Result
Result of a send skipped because the operator turned the template (`active`) or its language (`active_translation`) off in the admin. It is a success: a disabled e-mail is a choice, not an error, so callers neither log it nor roll anything back.
dispatchQueuedEmail()  : Result
Sends a queued email directly through the mailer (bypasses the queue).
isMailBlocked()  : bool
Returns `true` when the emergency sending switch is on in neon (`params.1.mail.blocked`).
sendEmail()  : Result
sendEmailFromText()  : Result
Renders a template text (subject and body) into the wrapper `email.latte` and sends it.
sendEmailToAll()  : Result
Sends one template to several recipients.
sendNewsletterEmail()  : Result
Sends one newsletter e-mail: the newsletter body is wrapped by the template `BS_NEWSLETTER` (footer with the unsubscribe link, editable in the admin) in the language of the recipient, falling back to the default language.
sendRenderedEmail()  : Result
Sends an already rendered HTML body as it is: no Latte pass and no second wrapper.
sendTextAsTemplate()  : Result
Sends a text written for one record (an invoice e-mail edited in the admin) under the template `$emailKey`: the template decides the channel ({@see Email::getSendImmediately()}, `preferred`), the text is the caller's.
setSiteBaseUrl()  : void
fallbackEmailLanguageId()  : int
Returns the email language when the caller does not know it (no-language context: cron, internal calls).
dispatchEmailToRecipient()  : Result
Sends to one recipient from an already resolved `Email` template.
initializeDefaultMailer()  : Mailer
initializeMailer()  : Mailer
replyToAddresses()  : array<int, string>
Returns reply-to addresses as a list of non-empty strings.
saveIntoEmailQueue()  : Result
setParams()  : array<string, mixed>
setTemplate()  : string|null
Renders the body into the wrapper `email.latte` with Latte.

Properties

$siteBaseUrl

Website address for links in emails ({@see SiteBaseUrl}); `null` leaves links unchanged (manager built without DI, SMTP test, queue). Filled by {@see \App\UI\Api\System\Models\Managers\EmailManager}.

private SiteBaseUrl|null $siteBaseUrl = null

Methods

disabledEmailResult()

Result of a send skipped because the operator turned the template (`active`) or its language (`active_translation`) off in the admin. It is a success: a disabled e-mail is a choice, not an error, so callers neither log it nor roll anything back.

public static disabledEmailResult() : Result
Return values
Result

dispatchQueuedEmail()

Sends a queued email directly through the mailer (bypasses the queue).

public dispatchQueuedEmail(EmailQueue $queueItem) : Result

Used by the cron job that processes the queue.

Parameters
$queueItem : EmailQueue
Return values
Result

isMailBlocked()

Returns `true` when the emergency sending switch is on in neon (`params.1.mail.blocked`).

public isMailBlocked() : bool

The check lives here on purpose: every send passes through this place, including dispatchQueuedEmail() from cron, so the admin setting email.sending.mode cannot bypass it.

Return values
bool

sendEmail()

public sendEmail(int|null $langId, string $emailKey, string $toEmailAddrr[, array<string, mixed> $values = [] ][, array<string|int, File> $files = [] ][, array<string|int, mixed> $replyTos = [] ][, string|null $subjectSuffix = null ]) : Result
Parameters
$langId : int|null
$emailKey : string
$toEmailAddrr : string
$values : array<string, mixed> = []
$files : array<string|int, File> = []
$replyTos : array<string|int, mixed> = []
$subjectSuffix : string|null = null
Return values
Result

sendEmailFromText()

Renders a template text (subject and body) into the wrapper `email.latte` and sends it.

public sendEmailFromText(string $emailSubject, string $emailBody, string $from, string $toEmailAddrr[, array<string, mixed> $values = [] ][, array<string|int, File> $files = [] ][, array<string|int, mixed> $replyTos = [] ][, bool $preferred = false ][, int|null $newsletterId = null ][, bool $sendImmediately = false ]) : Result

A failed render returns a failure and sends nothing (the error is in the email.render log).

Parameters
$emailSubject : string
$emailBody : string
$from : string
$toEmailAddrr : string
$values : array<string, mixed> = []
$files : array<string|int, File> = []
$replyTos : array<string|int, mixed> = []
$preferred : bool = false
$newsletterId : int|null = null
$sendImmediately : bool = false
Return values
Result

sendEmailToAll()

Sends one template to several recipients.

public sendEmailToAll(string $emailKey, array<int, EmailRecipientDTO> $recipients[, array<string, mixed> $values = [] ][, array<string|int, File> $sharedFiles = [] ]) : Result

The template and from are resolved once; each EmailRecipientDTO has its own language, template values, reply-to addresses and attachments.

Parameters
$emailKey : string
$recipients : array<int, EmailRecipientDTO>
$values : array<string, mixed> = []

template values shared by all; getValues() of a recipient overrides them (array_merge)

$sharedFiles : array<string|int, File> = []

attachments shared by all recipients

Return values
Result

sendNewsletterEmail()

Sends one newsletter e-mail: the newsletter body is wrapped by the template `BS_NEWSLETTER` (footer with the unsubscribe link, editable in the admin) in the language of the recipient, falling back to the default language.

public sendNewsletterEmail(int $newsletterId, int|null $langId, string $to, string $subject, string $content, string $unsubscribeLink[, bool $test = false ]) : Result

The newsletter HTML goes into the template as the value content, so the editor HTML is never parsed by Latte. A dispatch e-mail always goes to the queue (newsletterId keeps one e-mail per newsletter and address); a test e-mail goes straight to the mailer. The subject is the newsletter subject. The template is used even when turned off, it is a wrapper, not a notification.

Parameters
$newsletterId : int
$langId : int|null
$to : string
$subject : string
$content : string
$unsubscribeLink : string
$test : bool = false
Return values
Result

sendRenderedEmail()

Sends an already rendered HTML body as it is: no Latte pass and no second wrapper.

public sendRenderedEmail(string $emailSubject, string $htmlBody, string $from, string $toEmailAddrr[, array<string|int, File> $files = [] ][, array<string|int, mixed> $replyTos = [] ][, bool $preferred = false ][, int|null $newsletterId = null ][, bool $sendImmediately = false ]) : Result

For bodies rendered earlier and stored (the watchdog e-mail text). Running them through sendEmailFromText() again would wrap them in email.latte a second time and parse every { of the inline CSS as Latte. Goes to the queue or straight to the mailer by email.sending.mode, same as every other e-mail.

Parameters
$emailSubject : string
$htmlBody : string
$from : string
$toEmailAddrr : string
$files : array<string|int, File> = []
$replyTos : array<string|int, mixed> = []
$preferred : bool = false
$newsletterId : int|null = null
$sendImmediately : bool = false
Return values
Result

sendTextAsTemplate()

Sends a text written for one record (an invoice e-mail edited in the admin) under the template `$emailKey`: the template decides the channel ({@see Email::getSendImmediately()}, `preferred`), the text is the caller's.

public sendTextAsTemplate(string $emailKey, string $emailSubject, string $emailBody, string $from, string $toEmailAddrr[, array<string|int, File> $files = [] ]) : Result

A missing template sends by email.sending.mode.

Parameters
$emailKey : string
$emailSubject : string
$emailBody : string
$from : string
$toEmailAddrr : string
$files : array<string|int, File> = []
Return values
Result

fallbackEmailLanguageId()

Returns the email language when the caller does not know it (no-language context: cron, internal calls).

protected fallbackEmailLanguageId() : int

The default throws a readable error instead of a TypeError; the Api EmailManager returns the default website language.

Return values
int

dispatchEmailToRecipient()

Sends to one recipient from an already resolved `Email` template.

private dispatchEmailToRecipient(Email $email, string $from, EmailRecipientDTO $recipient[, array<string, mixed> $sharedValues = [] ][, array<string|int, File> $sharedFiles = [] ][, string|null $subjectSuffix = null ]) : Result

Used by both sendEmail() (one recipient) and sendEmailToAll() (many).

Parameters
$email : Email
$from : string
$recipient : EmailRecipientDTO
$sharedValues : array<string, mixed> = []
$sharedFiles : array<string|int, File> = []
$subjectSuffix : string|null = null
Return values
Result

initializeDefaultMailer()

private initializeDefaultMailer() : Mailer
Return values
Mailer

initializeMailer()

private initializeMailer([string|null $from = null ]) : Mailer
Parameters
$from : string|null = null
Return values
Mailer

replyToAddresses()

Returns reply-to addresses as a list of non-empty strings.

private static replyToAddresses(array<string|int, mixed> $replyTos) : array<int, string>

An empty address would make addReplyTo() fail the whole send.

Parameters
$replyTos : array<string|int, mixed>
Return values
array<int, string>

saveIntoEmailQueue()

private saveIntoEmailQueue(string $subject, string $body, string $from, string $to[, array<string|int, File> $files = [] ][, array<string|int, mixed> $replyTos = [] ][, bool $preferred = false ][, int|null $newsletterId = null ]) : Result
Parameters
$subject : string
$body : string
$from : string
$to : string
$files : array<string|int, File> = []
$replyTos : array<string|int, mixed> = []
$preferred : bool = false
$newsletterId : int|null = null
Return values
Result

setParams()

private setParams([array<string, mixed> $params = [] ]) : array<string, mixed>
Parameters
$params : array<string, mixed> = []
Return values
array<string, mixed>

setTemplate()

Renders the body into the wrapper `email.latte` with Latte.

private setTemplate(string $templateDir, string $body[, array<string|int, mixed> $paramters = [] ]) : string|null

Returns null when the render fails (the error is logged to the email.render channel). The caller must not send anything then: an empty string used to go out as an e-mail with an empty body.

Parameters
$templateDir : string
$body : string
$paramters : array<string|int, mixed> = []
Return values
string|null
On this page

Search results