Module std::net::smtp

SMTP client for sending email.

Provides functions for connecting to an SMTP server and sending plain-text or HTML email messages.

Examples

import std.net.smtp;

fn main() {
    let _ = smtp.send(
        "smtp.example.com",
        587,
        "user",
        "pass",
        "from@example.com",
        "to@example.com",
        "Subject",
        "Body",
    ).expect("the call succeeds");
}

Contents

Functions

Function last_error

pub fn last_error() -> string

Return the last SMTP client error observed on this thread.

Failed connect()/connect_tls()/send() calls update this string. Successful sends clear it. Call immediately after a failed connect/send for the precise reason.

Function connect

pub fn connect(host: string, port: i64, username: string, password: string) -> Result<Conn, string>

Connect to an SMTP server using STARTTLS.

The connection is opened and probed before it is returned, so an unreachable host, a refused port, or a server that is not speaking SMTP is reported here rather than at the first send.

Examples

import std.net.smtp;

fn main() {
    match smtp.connect("smtp.example.com", 587, "user", "pass") {
        .Ok(conn) => conn.close(),
        .Err(reason) => println(f"connect failed: {reason}"),
    }
}

Function connect_tls

pub fn connect_tls(host: string, port: i64, username: string, password: string) -> Result<Conn, string>

Connect to an SMTP server using implicit TLS.

The connection is opened and probed before it is returned, so an unreachable host, a refused port, or a server that is not speaking SMTP is reported here rather than at the first send.

Examples

import std.net.smtp;

fn main() {
    match smtp.connect_tls("smtp.example.com", 465, "user", "pass") {
        .Ok(conn) => conn.close(),
        .Err(reason) => println(f"connect failed: {reason}"),
    }
}

Function send

pub fn send(host: string, port: i64, username: string, password: string, from: string, to: string, subject: string, body: string) -> Result<(), string>

Connect with the same STARTTLS path as connect(...), send one plain-text email, and close the connection.

Function send_html

pub fn send_html(host: string, port: i64, username: string, password: string, from: string, to: string, subject: string, html: string) -> Result<(), string>

Connect with the same STARTTLS path as connect(...), send one HTML email, and close the connection.

Types

Struct Conn

An open SMTP connection handle.

Created by calling smtp.connect(...) or smtp.connect_tls(...), which probe the server before returning Ok and report an unreachable or non-SMTP endpoint as Err. Use send() or send_html() to deliver messages.

Traits

Trait ConnMethods

Methods available on an SMTP Conn.

Methods

fn send(self: Self, from: string, to: string, subject: string, body: string) -> Result<(), string>

Send a plain-text email.

fn send_html(self: Self, from: string, to: string, subject: string, html: string) -> Result<(), string>

Send an HTML email.