std::sendmail
A utility module for sending email. Uses the system's sendmail
command.
Example
import std::sendmail; # Create a new email. var email = sendmail::Email(); # Set the email's recipient, sender, and subject. email:to("johndoe@example.com"); email:from("sender@example.com"); email:subject("Important Subject"); # Optional: add additional headers. email:add_header("Content-Type: text/plain; charset=utf-8"); # Write the email's content to its `body` buffer. email:write("This is the email's content."); # Optional: print the email's raw text. echo email; # Send the email. var result = email:send(); if $is_err(result) { echo result; }
Classes
-
Email() -> Email
-
Creates a new
Email
object.You need to set the email's
to
,from
, andsubject
headers before sending it.
Email
objects have the following methods:
-
:add_header(header: str) -> Email
-
Adds a header to the email.
Returns
self
to allow chaining. -
:from(email_addr: str) -> Email
-
Sets the value of the email's
From:
header.The argument can be a bare email address, e.g.
email:from("johndoe@example.com");
Alternatively, the argument can combine a name and an email address, e.g.
email:from("John Doe <johndoe@example.com>");
Returns
self
to allow chaining. -
:send() -> err?
-
Sends the email.
Returns an
err
if the attempt to send the email fails. -
:subject(text: str) -> Email
-
Sets the value of the email's
Subject:
header.Returns
self
to allow chaining. -
:to(email_addr: str) -> Email
-
Sets the value of the email's
To:
header.Returns
self
to allow chaining. -
:write(arg: any) -> Email
:write(format_string: str, *args: any) -> Email
-
Writes to the email's
body
buffer.-
Calling this method with a single argument is equivalent to calling
$str()
on that argument first and writing the resulting string. -
Calling this method with more than one argument is equivalent to calling
$fmt()
on those arguments first and writing the resulting string.
Returns
self
to allow chaining.You can call this method multiple times to keep appending content to the email.
-
Calling this method with a single argument is equivalent to calling