Sending mail
@send_mail takes who the mail is for, a subject and a body, and returns the message it sent as a map:
<import package="KScript.Mail" />
<let name="sent" value="@send_mail(to: 'joan@example.com', subject: 'Your receipt', text: 'Thanks for your order.')" />
<echo value="$sent.subject" />import KScript.Mail
$sent = @send_mail(to: 'joan@example.com', subject: 'Your receipt', text: 'Thanks for your order.')
echo $sent.subjectto: takes an address, a user (any map or record with an email), or a list of them. cc: and bcc: take the same, and reply_to: and from: take one address:
<import package="KScript.Mail" />
<do>@send_mail(
to: ['joan@example.com', 'ada@example.com'],
cc: 'orders@example.com',
reply_to: 'help@example.com',
subject: 'Your order has shipped',
text: 'It is on its way.',
)</do>import KScript.Mail
@send_mail(
to: ['joan@example.com', 'ada@example.com'],
cc: 'orders@example.com',
reply_to: 'help@example.com',
subject: 'Your order has shipped',
text: 'It is on its way.',
)The body
A message has one body: text:, html: or view:. Giving two is an error. HTML mail gets a plain-text copy made from it, for mail readers that don't show HTML.
<import package="KScript.Mail" />
<do>@send_mail(to: 'joan@example.com', subject: 'Welcome', html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>')</do>import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome', html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>')Views
Longer messages belong in a view, like a page. view: 'mail/welcome' renders views/mail/welcome.html (or .md, for Markdown mail), and the keys of data: are its variables:
<!-- views/mail/welcome.html -->
<h1>Welcome, { $name }</h1>
<p>Your account is ready. <a href="{ $link }">Sign in</a> to get started.</p><import package="KScript.Mail" />
<do>@send_mail(to: 'joan@example.com', subject: 'Welcome', view: 'mail/welcome', data: { name: 'Joan', link: 'https://example.com/login' })</do>import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Welcome', view: 'mail/welcome', data: { name: 'Joan', link: 'https://example.com/login' })kscript make mail welcome --vars name,link writes a view to start from, and says how to send it.
Attachments
attachments: takes a list of files, as paths relative to the script or file values:
<import package="KScript.Mail" />
<do>@send_mail(to: 'joan@example.com', subject: 'Your invoice', text: 'It is attached.', attachments: ['invoices/1042.pdf'])</do>import KScript.Mail
@send_mail(to: 'joan@example.com', subject: 'Your invoice', text: 'It is attached.', attachments: ['invoices/1042.pdf'])