CL-HTTP Chapter 17: SMTP Mail


Reference Manual | Prev: Chapter 16. Lambda-IR | Next: Chapter 18. Server-Parsed HTML and CGI


Contents

  1. 17.1 Overview
  2. 17.2 Sending Mail
  3. 17.3 Configuration
  4. 17.4 Error Handling

17.1 Overview

The SMTP subsystem (http:smtp;) enables CL-HTTP applications to send email programmatically. It implements the Simple Mail Transfer Protocol with support for plain SMTP and SMTP over TLS (STARTTLS on port 587), SMTP authentication, store-and-forward failover, and integration with the CL-HTTP bug reporting facility.

The SMTP package uses the WWW-UTILS portability layer for networking and is defined in http:smtp;package.lisp. The high-level mail-sending interfaces are in http:smtp;mail.lisp, while the low-level SMTP protocol implementation is in http:smtp;smtp.lisp.


17.2 Sending Mail

17.2.1 send-mail-from

The primary interface for sending email from CL-HTTP applications:

(send-mail-from from to subject mail-writer
                &key keywords comments file-references
                     reply-to additional-headers user password)
from
The sender's email address (a string).
to
The recipient's email address (a string or list of strings).
subject
The email subject line (a string).
mail-writer
Either a string containing the message body, or a function of one argument (stream) that writes the message body.
:keywords
A list of keywords for the Keywords header.
:comments
A string for the Comments header.
:file-references
A list of pathnames for the File-References header.
:reply-to
The Reply-To address (automatically added if not supplied).
:user, :password
SMTP authentication credentials. Default to the per-host credentials registered via smtp:get-smtp-authentication-user and smtp:get-smtp-authentication-password.
;; Send a simple message
(send-mail-from "webmaster@example.com"
                "admin@example.com"
                "Daily Report"
                "The daily report is ready.")

;; Send with a writer function
(send-mail-from "webmaster@example.com"
                "admin@example.com"
                "Server Statistics"
                #'(lambda (stream)
                    (http::write-server-statistics stream))
                :keywords '(:statistics :daily))

17.2.2 send-mail

A convenience synonym for send-mail-from with identical arguments:

(send-mail from to subject mail-writer
           &key keywords comments file-references
                reply-to additional-headers user password)

17.2.3 send-mail-message

The generic function that dispatches on the mail transport method. Application code normally uses send-mail-from instead.

(send-mail-message method from to subject mail-writer
                   &optional reply-to keywords comments
                             file-references additional-headers
                             user password)

Methods are defined for :smtp (plain SMTP) and :smtp-tls (SMTP with TLS on port 587). The variable smtp:*send-mail-method* controls which method is used by default.

17.2.4 report-bug

Sends a bug report email, typically from a response function when an error is caught:

(report-bug to subject format-string &rest format-args)

The from address is taken from http:*server-mail-address*. The bug report is sent asynchronously in a background process. The behavior is controlled by http:*bug-report-destination*:

:mail — Send the bug report by email.
:log-window — Report in the notification window only.
:none — Suppress the report.

17.3 Configuration

17.3.1 Mail Host

;; Set the SMTP server
(setq smtp:*network-mail-host* "mail.example.com")

;; Set the server's return address
(setq http:*server-mail-address* "webmaster@example.com")

The variable smtp:*network-mail-host* must be set before any mail can be sent. It specifies the SMTP server to connect to.

17.3.2 TLS Configuration

;; Use TLS for mail (STARTTLS on port 587)
(setq smtp:*send-mail-method* :smtp-tls)

17.3.3 Authentication

;; Set per-host authentication credentials
(setf (smtp:get-smtp-authentication-user "mail.example.com")
      "myuser")
(setf (smtp:get-smtp-authentication-password "mail.example.com")
      "mypassword")

17.3.4 Store-and-Forward Failover

If the primary mail host is unreachable, CL-HTTP can try alternate hosts:

(setq smtp:*store-and-forward-mail-hosts*
      '("mail1.example.com" "mail2.example.com"))

The system tries each host in order, skipping the local host, until one accepts the message or all fail.

17.3.5 Debugging

;; Enable SMTP debugging (bypasses error handling)
(smtp:debug-mailer t)

;; Disable
(smtp:debug-mailer nil)

When smtp:*debug-mailer* is true, SMTP errors are signalled directly rather than being caught by the error-handling wrappers. This is useful for diagnosing connection and authentication problems.


17.4 Error Handling

The SMTP subsystem defines a hierarchy of conditions for error handling:

17.4.1 Mailer Conditions

smtp:mailer-error
Base condition for all mailer errors.
smtp:mailer-temporary-error
Transient server error (4xx SMTP reply). The message may succeed on retry.
smtp:mailer-permanent-error
Permanent server error (5xx SMTP reply). The message will not succeed.
smtp:mailer-protocol-error
Unexpected SMTP protocol response.
smtp:mailer-timeout
Connection or response timeout.
smtp:mailer-connection-timeout
Timeout establishing the TCP connection.
smtp:mailer-unknown-recipient
Recipient address rejected by the server.
smtp:mailer-incomplete-delivery-error
Some but not all recipients accepted.

17.4.2 SMTP Protocol Conditions

smtp:smtp-error
Base condition for SMTP-level errors.
smtp:smtp-host-not-available
Cannot connect to the mail server.
smtp:smtp-mailbox-not-found
Recipient mailbox does not exist.
smtp:smtp-syntax-error
SMTP command syntax error.
smtp:smtp-bad-command-sequence
SMTP commands sent in wrong order.
smtp:smtp-insufficient-system-storage
Server has insufficient storage.

Application code can handle these conditions to implement retry logic or user notification:

(handler-case
    (send-mail-from "web@example.com"
                    "user@example.com"
                    "Notification"
                    "Your request has been processed.")
  (smtp:mailer-temporary-error (c)
    (log-mail-failure c :retry-later t))
  (smtp:mailer-permanent-error (c)
    (log-mail-failure c :retry-later nil))
  (network-error (c)
    (log-mail-failure c :network-problem t)))