Pyro

A scripting language for people who enjoy the simpler things in life.

Version 0.9.15

std::email


A utility module for sending email. Uses the system's sendmail command.

Example

import std::email;

# Create a new email.
var mail = email::Email();

# Set the email's recipient, sender, and subject.
mail:to("johndoe@example.com");
mail:from("sender@example.com");
mail:subject("Important Subject");

# Optional: add additional headers.
mail:add_header("Content-Type: text/plain; charset=utf-8");

# Write the email's content to its `body` buffer.
mail:write("This is the email's content.");

# Optional: print the email's raw text.
echo mail;

# Send the email.
var result = mail:send();
if $is_err(result) {
    echo result;
}

Classes

Email() -> Email

Creates a new Email object.

You need to set the email's to, from, and subject 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.

mail:from("johndoe@example.com");

Alternatively, the argument can combine a name and an email address, e.g.

mail: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.