Sending mail through gmail Using Emacs

April 27, 2008 at 7:51 pm 4 comments

Very brief summary for the impatient
Ensure that the gnutls-bin program is installed (

apt-get install gnutls-bin

on ubuntu)

Paste the following into your init file changing username and password as appropriate.

(setq send-mail-function 'smtpmail-send-it)
(setq smtpmail-smtp-server "")
(setq smtpmail-smtp-service 25)
(setq smtpmail-auth-credentials '(("" 25 "USERNAME" "PASSWORD")))
(setq smtpmail-starttls-credentials '(("" 25 nil nil)))

Evaluate this with “M-x eval-buffer”


Longer explanation for the troubled / interested

Emacs can, if set up correctly, send mail through gmails smtp server.

First tell emacs to use smtp to send mail by evaluating:
(setq send-mail-function ‘smtpmail-send-it)

(If you don’t do this email will attempt to use sendmail to send mail – on my machine this resulted in mails not being sent despite emacs reporting that they were).

You must then set smtpmail’s authentification details. First run

(setq smtpmail-debug-info t)
(setq smtpmail-debug-verb t)

so as to get as much output as possible.

Then run

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

to tell emacs to use gmail for outgoing mail.

There are (at least) two mechanisms that smtp servers can use to authenticate users.

One method has the user provide a user name and password in plain(ish) text in the smtp connection. This connection can also be encrypted. This encryption mechanism is known as STARTTLS. Gmail uses a plain text authentication method sent over an encrypted connection in this manner.

With the other method, the user passes a certificate to the server which asserts that the user is who they say they are. This method of authentication is, confusingly, also know as STARTTLS.

To set-up authentication within SMTP evaluate

(setq smtpmail-auth-credentials '(("" 25 "USERNAME" "PASSWORD")))

To set up STARTTLS encryption of the connection call

(setq smtpmail-starttls-credentials '(("" 25 nil nil)))

(This variable is also used to set up authentication using STARTTLS).

After you have done this you should be able to send mail from within emacs. To test this run hit
C-x C-m. Write a test message, and then hit C-c C-s, before toggling to a buffer with a name beginning with “*trace of SMTP.” If anything goes wrong this should tell you the approximate reason.

More details

There doesn’t seem to be a way to make emacs prompt for a password when sending mail (though one could probably be hacked-together with half an hours work – after the other 10 hours needed to learn emacs lisp).

You can avoid keeping a password in your initialization file by using a .netrc file.

smtpmail’s documentation can be found here .

If things start not working you might like to use the source – but be careful, some older versions of smtpmail don’t support starttls.


Entry filed under: Uncategorized. Tags: , , .

Higher-order functions are quite like objects Emacs buttons – an introduction for the somewhat impatient

4 Comments Add your own

  • 1. susja  |  February 21, 2009 at 2:56 pm

    it seems it doesn’t work for me.
    I spent hours to set it up but it still complains about SMTP error.
    I copy/paste in my .xemacs your settings but when I tired to send email I’ve got the error:
    Debugger entered–Lisp error: (error “Sending failed; SMTP protocol error”)
    signal(error (“Sending failed; SMTP protocol error”))
    cerror(“Sending failed; SMTP protocol error”)
    apply(cerror “Sending failed; SMTP protocol error” nil)
    error(“Sending failed; SMTP protocol error”)

    Could you help me to understand what’s wrong?

  • 2. existentiality  |  February 21, 2009 at 7:04 pm

    Hmm. I haven’t looked at this in a while.

    What is inside the “trace of SMTP….” buffer?

    • 3. existentiality  |  February 21, 2009 at 7:29 pm

      On second thoughts, the likely problem is that you don’t have gnutls installed. The emacs code helpfully silently fails without any logging if this is the case. I’ve changed the post so that it includes this.

      Tell me if this doesn’t work.

  • 4. jh  |  October 21, 2010 at 11:43 pm

    In emacs 22, if you specified nil instead of a password string for the smtp password, it prompted you once per emacs session. In emacs 23, it prompts every message! This is unworkable, yet I cannot have my password in a file. How do I get the old behavior back? I’ve scoured the docs to no avail.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

April 2008
« Mar   May »

%d bloggers like this: