email in emacs

email in emacs

# overview of email readers

The gnus email/news reader seems to be filled with features for special circumstances, especially as a response to exceptions to its own design.

Other email programs and packages seem to focus on handling large volumes of email with indexing and sorting. Like gnus, they come across as handling exceptions to their own designs, filled with features rather than opportunities.

On the other hand, rmail ("read mail") seems to have basic opportunities for reading and organizing email without the bloat of featureful exceptions. If there's anything against rmail it's that the time is ripe for rmail's refinement. Irregarding it as only an email reader, from within rmail mode the message mode can be evoked with m for composing a new email or with r for replying to the currently viewed email, too.

# rmail mode for reading email

The ESC x rmail command ("read mail") evokes a mode that gathers new email from various sources into a single file. Organize by outputting an email to other files with o, mark an email for deletion with d, and get new email with g. Info node "(emacs)Rmail" has details, as with any mode Controlh m reveals the mode's keybindings, and it's customized with ESC x customize-group and the name "rmail".

Briefly, customize rmail-primary-inbox-list with an entry for each email account. Evoking rmail ESC x rmail consolidates emails from all the sources into the file located at rmail-file-name, and then immediately displays the first unread email. It's as simple as that.

# rmail customization

# rmail-primary-inbox-list

Specify a URL to a single email server and/or a list of URLs to files for merging into the primary email file rmail-file-name (default "~/RMAIL"). Only the protocol and the host are required, or the type of file and its path.


There is support for imap email servers when emacs is built with mailutils.

When using movemail (of mailutils for emacs) any "@" symbols in the login name need to be replaced with "%40" so as to be distinct from the "@" that demarcates the email server. The login name or password will be queried when missing.

The password is used only when rmail-remote-password-required is set to non-nil. The password itself can be set in rmail-remote-password or will be queried when missing, and it will be saved there when it is known (either from rmail-primary-inbox-list or when queried). This is why rmail supports only a single remote source, t.i. a single email server, unless the same password is used for all email accounts at every email server.

This alternate rmail.el library file uses auth-source rather than saving a remote password in the init file, thereby also permitting more than one remote source. (See bug report #24274 for a similar difference, though that only adds the use of auth-source.)

# rmail-file-name

The path for the file where new emails are appended when gathered from sources listed in rmail-primary-inbox-list. Default is "~/RMAIL".

# rmail-preserve-inbox

Get new email without deleting it from the source.

untried settings


Specify the default filename for outputting a message (o or Controlo) based on a regular expression matching anything within the message.


Specify a folder (filename?) for moving a message based on regular expressions matching headers.
(Unclear whether the action is suggested when viewing the message, or automatically performed when viewing the message, or automatically performed when viewing the rmail inbox (rmail-file-name) as a whole (thereby also after getting new mail).)

# message mode

Compose a new email by typing m within rmail and a new buffer will appear using message mode. Adding an attachment is as simple as Controlc Controla, and the manual for writing more intricate MIME messages is in Info node "(emacs-mime)". Send the message with Controlc Controls. Of course, Controlh m reveals other keybindings.

Documentation for message mode is briefly described in Info node "(emacs)Mail Commands" and its full manual is in Info node "(message)". It's actually part of gnus so it has an excessive amount of customization, available with ESC x customize-group and the name "message".

Notably, within message mode is a footnote minor mode. Its documentation is most readily available with Controlh f footnote-mode, and is customizable with ESC x customize-group and the name "footnote".

saving sent mail

There are several ways of saving sent mail. One way is to add "Fcc:" fields to the message before sending it, specifying where to file a carbon copy of the message. If a location is an rmail file, then it'll be saved in rmail format. This can also be done automatically by customizing the variable message-default-mail-headers (and making sure to add a newline after each header in it).


Alternatively, the variable mail-archive-file-name is for saving sent messagss to a file, though it seems like there was a warning about misuse or deprecation of that variable when creating or maybe sending a message.

Similarly, adding "Bcc:" fields works, too. However, that requires checking for new email after sending the message in order to receive it, and then possibly organizing such received messages. Either add the fields to the default headers variable, or copy the "From:" field to a "Bcc:" field in each message. Or use the variable mail-self-blind to have a message automatically initialized with the "Bcc:" field, though that uses the singular email address from the variable user-mail-address.

# configuration for sending email

Sending mail involves Simple Mail Transfer Protocol (SMTP). Info node "(smtpmail)Emacs Speaks SMTP" details the basic variables to setup. However, for multiple SMTP email accounts consider using this alternate smtpmail.el library.

Summary of variables for sending email, all customizable:


All five of these variables are unnecessary when using this alternate smtpmail.el library.

The value for user-mail-address is the default email address for the "From:" field when creating a new message. Otherwise, just type or paste it in for each new message. (Or select an email address in an email and use ESC x append-to-buffer to transfer a copy of it to the message buffer.)

The value for smtpmail-smtp-user when non-nil is matched with the login value in the authentication file when seeking credentials.

user-mail-address "name@domain.tld"
smtpmail-smtp-user "name@domain.tld"
smtpmail-smtp-server "mail.domain.tld"
smtpmail-smtp-service 587
smtpmail-stream-type 'starttls)

smtpmail-smtp-service 465
smtpmail-stream-type 'ssl)

Service port 465 goes with stream type TLS/SSL. Service port 587 goes with stream type STARTTLS. Using the default of no value (t.i. nil) for smtpmail-stream-type casually upgrades to encryption, which works with service port 587. (Service port 25 probably goes with no encryption, "plain".)


For whatever reason, the send-mail-function was "Changed outside of Emacs" even though its value matched the default value declared in the documentation. The default value is sendmail-query-once, which asks for the settings individually, however that failed. (Maybe it failed because the email server required a special authentication, which was resolved later.)

In general, set to smtpmail-send-it in order to use SMTP.


For whatever reason, the message-send-mail-function was "Changed outside of Emacs" to sendmail-query-once. Default value is probably message-smtpmail-send-it, which uses the hook message-send-mail-hook (default value nil) before doing the same thing as smtpmail-send-it.

According to Info node "(message)Mail Variables", this optionally also somehow has something to do with the email header X-Message-SMTP-Method for specifying the server, service, and user. This header overrides the variables smtpmail-smtp-server, smtpmail-smtp-service, and smtpmail-smtp-user for that email message.

X-Message-SMTP-Method: smtp smtp.domain.tld 587 other-user

However, using the alternate smtpmail.el library obviates the need for that optional header by automatically matching the "From:" field with the auth-source credentials.

Info node "(smtpmail)Authentication" says the ".authinfo" file is needed for login credentials, or will be created to save them. Ensure gpg is used so it will instead be ".authinfo.gpg".

other interests

Functions for using the header of an email (with LISP):

# authentication with gpg

According to Info node "(smtpmail)Authentication", the credentials are stored in "~/.authinfo". According to Info node "(auth)GnuPG and EasyPG Assistant Configuration", the file can be encrypted with gpg. Probably a good idea to set auth-sources to prefer ".authinfo.gpg", per Info node "(auth)" for auth-source.

In particular, always remember the relevant files and folders must have non-public permissions, t.i. drwx------ or 700 for directories, and -rw------- or 600 for files. Otherwise, there will be ambiguous error messages and absolute failure.

# gnupg and gpg

install gnupg

Somehow install gnupg utilities in order to have gpg.

brew install gnupg

According to Info nodes "(gnupg)Invoking GPG-AGENT" and "(gnupg)Agent Examples", the shell initialization file (f.e. ".bash_profile") should have something like:

export GPG_TTY=$(tty)

create and manage keys

Info node "(gnupg)OpenPGP Key Management" has a listing of options for key management. Generate a new key, which is actually a pair of public key for encoding and private key for decoding.

gpg --full-generate-key

Choose default type and its max value, and the key's expiration. Name, email, and comment are optional, though at least one is needed for the key's label in the key list.

The Info node "(epa)" has more about using gpg from emacs once it's installed. In emacs, gpg keys can be listed with ESC x epa-list-keys and ESC x epa-list-secret-keys, with TAB for moving to each entry and RET for showing more info about one.

gpg pin entry

Info node "(epa)GnuPG version compatibility" mentions how to enable the passphrase prompt for use within emacs. Add "allow-emacs-pinentry" to "~/.gnupg/gpg-agent.conf" and let gpg-agent reload the configuration, with: "gpgconf --reload gpg-agent". Info node "(gnupg)Agent Options" lists this option. Note this is similar to but distinctly different from the "pinentry-mode" option listed in Info node "(gnupg)GPG Esoteric Options". Info node "(gnupg)GPG Options" has more on the format of the configuration file.

Alternatively, in emacs epg-pinentry-mode (formerly "epa-pinentry-mode") has the same values as the gpg option "pinentry-mode", and when set to "loopback" seems to be equivalent to setting the gpg option "allow-emacs-pinentry" in the gpg config file. In other words, no need to configure gpg-agent with gpg-conf.

# .authinfo

Create a file named ".authinfo.gpg" with permission mode 600. According to Info node "(auth)Help for users", each entry is one line with machine name, login (or "account", or "user"), password, and port. Using "host" instead of "machine" fails for retrieving email with movemail (of mailutils for emacs). Each of those are optional and can be listed in any order. Use "%40" instead of "@" in the login for retrieving emails with movemail (of mailutils for emacs). Leave the "@" as is for the login of SMTP machines.

machine smtp.domain.tld port 587 login someone@domain.tld password abc123
login someone%40domain.tld
machine server.domain.tld:80 port http

An entry without a login or without a password means to ask for it when anything else matches.

More in:
tweaks for rmail
tweaks for smtpmail