KScript docs
EN

KScript.Mail

Email: @send_mail over SMTP, to the log or to a fake, now or on a queue, with HTML, text or a view, and @fake_mail for tests.

Import it at the top of a script:

<import package="KScript.Mail" />
import KScript.Mail

KScript.Mail sends email, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.

Commands

Commands are called with @, in any expression: @name(arguments).

#@fake_mail() → fake_mail

For tests: from now on in this run, mail is kept instead of sent, queued or not. $mail->has_sent('joan@example.com') checks it and $mail->sent() lists it.
Example
<let name="mail" value="@fake_mail()" />
<do>@send_mail(to: 'joan@example.com', subject: 'Hi', text: 'Hello')</do>
<echo>{ $mail->has_sent('joan@example.com') }</echo>
$mail = @fake_mail()
@send_mail(to: 'joan@example.com', subject: 'Hi', text: 'Hello')
echo $mail->has_sent('joan@example.com')

#@send_mail(to, subject: string, html?: string, text?: string, view?: string, data?: map, cc?, bcc?, reply_to?, from?, attachments?: list, queue?: bool) → map

Sends an email and returns it as a map. to (and cc and bcc) take an address, a user with an email, or a list of them. The body is html, text, or a view from the views folder (mail/welcome is views/mail/welcome.html or .md) given data as its variables; HTML mail gets a plain-text copy too. from defaults to config.mail.from. attachments are file paths. queue sends it from kscript worker instead of now, on config.mail.queue. config.mail.driver picks how it is sent: smtp, log (the default) or fake.
ParameterTakes
to requiredany value
subject requiredstring
html optionalstring
text optionalstring
view optionalstring
data optionalmap
cc optionalany value
bcc optionalany value
reply_to optionalany value
from optionalany value
attachments optionallist
queue optionala flag: queue or not queue

Arguments can be named: @send_mail(…, subject: …, html: …).

Example
<do>@send_mail(to: 'joan@example.com', subject: 'Welcome', text: 'Thanks for signing up.')</do>
@send_mail(to: 'joan@example.com', subject: 'Welcome', text: 'Thanks for signing up.')

Methods

On fake_mail values: ->sent(), ->queued(), ->has_sent(), ->count(), ->count_of(), ->clear()