Reference Manual | Prev: Chapter 16. Lambda-IR | Next: Chapter 18. Server-Parsed HTML and CGI
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.
send-mail-fromThe 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)
:keywords
:comments
:file-references
:reply-to
:user, :password
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))
send-mailA 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)
send-mail-messageThe 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.
report-bugSends 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.
;; 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.
;; Use TLS for mail (STARTTLS on port 587) (setq smtp:*send-mail-method* :smtp-tls)
;; 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")
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.
;; 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.
The SMTP subsystem defines a hierarchy of conditions for error handling:
smtp:mailer-error
smtp:mailer-temporary-error
smtp:mailer-permanent-error
smtp:mailer-protocol-error
smtp:mailer-timeout
smtp:mailer-connection-timeout
smtp:mailer-unknown-recipient
smtp:mailer-incomplete-delivery-error
smtp:smtp-error
smtp:smtp-host-not-available
smtp:smtp-mailbox-not-found
smtp:smtp-syntax-error
smtp:smtp-bad-command-sequence
smtp:smtp-insufficient-system-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)))