Sending Mail using Emacs

Like lot others, i too have a love and hate kind of relationship with Emacs. I love it when it works as i expect it to, and i hate it when i have to read its manuals (often more than once) to configure it as i want. When i am done configuring, i start to love it again!

Here, i am going to tell you how to configure your Emacs to send your emails. Sometimes its cool to be able to send mails from within your editor; for example, say you wrote an interesting code snippet and you want to send it to your friends immediately. Newbies should understand that Receving-Mail and Sending-Mail are completely unrelated. Only thing that keeps them close is we normally use a single program, like Thunderbird or Evolution (Mail User Agent - MUA) to do both tasks. WTH, you don’t need any email account to send a mail, where as to receive mails you must have an email account (or else, there is no meaning for *your* mails, get it?)

To write a mail in Emacs, you have to use C-x m (compose-mail) command. It opens a *mail* buffer where you can fill in To, CC, Subject fields (called headers) and body of your mail. Once you are done with editing you use C-c C-c (mail-send-and-exit) command. This command will send the mail you prepared, through a mail server you configured. I love that this procedure is straight and simple, but what i hate is, i *have* to pre-configure my mail server and to do that i have to read the manuals.

Below i will explain how to configure Emacs to send mail using any SMTP server. By default emacs uses sendmail program to send its mails. You have to tell Emacs to use remote SMTP server instead of local sendmail program, you do this by adding

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

to your ~/.emacs file. Also, you have to tell about the SMTP server name and port number. To do this, add

(setq smtpmail-smtp-server "smtp.server.address")
(setq smtpmail-smtp-service 25)

lines into ~/.emacs. Number 25 is the default SMTP service port number. You may want to change it to 587 or something else. Ideally this should be sufficient to send your mail. But to reduce spamming, almost all SMTP servers now require you to authenticate before sending any mail. THIS is the ONLY reason you need an account to send a mail. So if your SMTP server requires authentication, you should give Emacs your username and passwords. You do this by adding

(setq smtpmail-auth-credentials '(("smtp.server.address" 25 "username" "password")))

or

(setq smtpmail-auth-credentials '(("smtp.server.address" 25 "username" nil)))

to your ~/.emacs file. In the second case above, Emacs will ask you for your password and you will be safe because you didn’t store your password anywhere on disks (remember root can read everyone’s files).

Thats it. Just add the above four lines into your ~/.emacs file and you will be able to send mails from within your Emacs session.

If your SMTP server needs you to connect using TLS, then you can tell Emacs about that too. Just add,

(setq smtpmail-starttls-credentials '(("smtp.server.address" 25 nil nil)))

SMTP servers with TLS usually use different port number. GMail uses 587, so change it as necessary. BTW, TLS settings above requires an external program, starttls on your machine. Check for that before complaining! May be this is the right time, you should read ‘Emacs SMTP Library’ manual. Manual is pretty small, just 12 pages.

Finally, here is the complete Emacs configuration to send mails using GMail as SMTP server:

(setq send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "smtp.gmail.com")
(setq smtpmail-smtp-service 587)
(setq smtpmail-auth-credentials '(("smtp.gmail.com" 587 "username@gmail.com" nil)))
(setq smtpmail-starttls-credentials '(("smtp.gmail.com" 587 nil nil)))

Have Fun!

Leave a Reply