2 Emacs Speaks SMTP

Emacs includes a package for sending your mail to a SMTP server and have it take care of delivering it to the final destination, rather than letting the MTA on your local system take care of it. This can be useful if you don’t have a MTA set up on your host, or if your machine is often disconnected from the internet.

Sending mail via SMTP requires configuring your mail user agent (see (emacs)Mail Methods) to use the SMTP library. If you have not configured anything, then in Emacs 24.1 and later the first time you try to send a mail Emacs will ask how you want to send mail. To use this library, answer ‘smtp’ when prompted. Emacs then asks for the name of the SMTP server.

If you prefer, or if you are using a non-standard mail user agent, you can configure this yourself. The normal way to do this is to set the variable send-mail-function (see (emacs)Mail Sending) to the value you want to use. To use this library:

(setq send-mail-function 'smtpmail-send-it)

The default value for this variable is sendmail-query-once, which interactively asks how you want to send mail.

Your mail user agent might use a different variable for this purpose. It should inherit from send-mail-function, but if it does not, or if you prefer, you can set that variable directly. Consult your mail user agent’s documentation for more details. For example, (see (message)Mail Variables).

Before using SMTP you must find out the hostname of the SMTP server to use. Your system administrator or mail service provider should supply this information. Often it is some variant of the server you receive mail from. If your email address is ‘yourname@example.com’, then the name of the SMTP server is may be something like ‘smtp.example.com’.

smtpmail-smtp-server

The variable smtpmail-smtp-server controls the hostname of the server to use. It is a string with an IP address or hostname. It defaults to the contents of the SMTPSERVER environment variable, or, if empty, the contents of smtpmail-default-smtp-server.

smtpmail-default-smtp-server

The variable smtpmail-default-smtp-server controls the default hostname of the server to use. It is a string with an IP address or hostname. It must be set before the SMTP library is loaded. It has no effect if set after the SMTP library has been loaded, or if smtpmail-smtp-server is defined. It is usually set by system administrators in a site wide initialization file.

The following example illustrates what you could put in ~/.emacs to set the SMTP server name.

;; Send mail using SMTP via mail.example.org.
(setq smtpmail-smtp-server "mail.example.org")

SMTP is normally used on the registered “smtp” TCP service port 25. Some environments use SMTP in “Mail Submission” mode, which uses port 587. Using other ports is not uncommon, either for security by obscurity purposes, port forwarding, or otherwise.

smtpmail-smtp-service

The variable smtpmail-smtp-service controls the port on the server to contact. It is either a string, in which case it will be translated into an integer using system calls, or an integer.

The following example illustrates what you could put in ~/.emacs to set the SMTP service port.

;; Send mail using SMTP on the mail submission port 587.
(setq smtpmail-smtp-service 587)