Chapter 11

SMTP Client

The smtp structure provides an client library for the Simple Mail Transfer Protocol, commonly used for sending email on the Internet. This library provides a simple wrapper for sending complete emails as well as procedures for composing custom SMTP transactions.

Some of the procedures described here return an SMTP reply code. For details, see RFC 821.

(smtp-send-mail from to-list headers body [host])     --->     undefined         (procedure) 
(smtp-error? thing)     --->     boolean         (procedure) 
(smtp-recipients-rejected-error? thing)     --->     boolean         (procedure) 
This emails message body with headers headers to recipients in list to-list, using a sender address from. The email is handed off to the SMTP server running on host; default is the local host. Body is either a list of strings representing the lines of the message body or an input port which is exhausted to determine the message body. Headers is an association lists, mapping symbols representing RFC 822 field names to strings representing field bodies.

If some transaction-related error happens, smtp-send-mail signals an smtp-error condition with predicate smtp-error?. More specifically, it raises an smtp-recipients-rejected-error (a subtype of smtp-error) if some recipients were rejected. For smtp-error, the arguments to the signal call are the error code and the error message, represented as a list of lines. For smtp-recipients-rejected-error, the arguments are reply code 700 and an association list whose elements are of the form (loser-recipient code . text) -- that is, for each recipient refused by the server, you get the error data sent back for that guy. The success check is (< code 400).

(smtp-expand name host)     --->     code text         (procedure) 
(smtp-verify name host)     --->     code text         (procedure) 
(smtp-get-help host [details])     --->     code text-list         (procedure) 
These three are simple queries of the server as stated in the RFC 821: smtp-expann asks the server to confirm that the argument identifies a mailing list, and if so, to return the membership of that list. The full name of the users (if known) and the fully specified mailboxes are returned in a multiline reply. Smtp-verify asks the receiver to confirm that the argument identifies a user. If it is a user name, the full name of the user (if known) and the fully specified mailbox are returned. Smtp-get-help causes the server to send helpful information. The command may take an argument (details) (e.g., any command name) and return more specific information as a response.

(smtp-connect host [port])     --->     smtp-connection         (procedure) 
Smtp-connect returns an SMTP connection value that represents a connection to the SMTP server.

(smtp-transactions smtp-connection transaction1 ...)     --->     code text-list         (procedure) 
(smtp-transactions/no-close smtp-connection transaction1 ...)     --->     code text-list         (procedure) 
These procedures make it easy to do simple sequences of SMTP commands. Smtp-connection must be an SMTP connection as returned by smtp-connect. The transaction arguments must be transactions as returned by the procedures below. Smtp-transactions and smtp-transactions/no-close execute the transactions specified by the arguments.

For each transaction,

Smtp-transactions closes the socket after the transaction. (The smtp-quit transaction, when executed, also closes the transaction.)

If the socket should be kept open in the case of an abort, use Smtp-transactions/no-close.

(smtp-helo local-host-name)     --->     smtp-transaction         (procedure) 
(smtp-mail sender-address)     --->     smtp-transaction         (procedure) 
(smtp-rcpt destination-address)     --->     smtp-transaction         (procedure) 
(smtp-data socket message)     --->     smtp-transaction         (procedure) 
(smtp-send sender-address)     --->     smtp-transaction         (procedure) 
(smtp-soml sender-address)     --->     smtp-transaction         (procedure) 
(smtp-saml sender-address)     --->     smtp-transaction         (procedure) 
smtp-rset         smtp-transaction 
(smtp-vrfy user)     --->     smtp-transaction         (procedure) 
(smtp-expn user)     --->     smtp-transaction         (procedure) 
(smtp-help details)     --->     smtp-transaction         (procedure) 
smtp-noop         smtp-transaction 
smtp-quit         smtp-transaction 
smtp-turn         smtp-transaction 
These transactions represent the commands of the SMTP protocol for use in smtp-transactions and smtp-transactions/no-close, i.e. they send the corresponding command along with the argument(s), if any. For details, consult RFC 821.

The smtp-quit transaction, in addition to sending a QUIT command to the SMTP server, also closes the socket of its SMTP connection.