Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.17.2

std::sendmail


A library for sending email using the system's sendmail binary.

Example

import std::sendmail;

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

# Set the email's recipient, sender, and subject.
email:to("receiver@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, 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(address: 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(address: 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.