| 1 | \input texinfo @c -*-texinfo-*- |
| 2 | @setfilename ../../info/smtpmail |
| 3 | @settitle Emacs SMTP Library |
| 4 | @syncodeindex vr fn |
| 5 | @copying |
| 6 | Copyright @copyright{} 2003-2012 |
| 7 | Free Software Foundation, Inc. |
| 8 | |
| 9 | @quotation |
| 10 | Permission is granted to copy, distribute and/or modify this document |
| 11 | under the terms of the GNU Free Documentation License, Version 1.3 or |
| 12 | any later version published by the Free Software Foundation; with no |
| 13 | Invariant Sections, with the Front-Cover texts being ``A GNU Manual'', |
| 14 | and with the Back-Cover Texts as in (a) below. A copy of the license |
| 15 | is included in the section entitled ``GNU Free Documentation License''. |
| 16 | |
| 17 | (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and |
| 18 | modify this GNU manual. Buying copies from the FSF supports it in |
| 19 | developing GNU and promoting software freedom.'' |
| 20 | @end quotation |
| 21 | @end copying |
| 22 | |
| 23 | @dircategory Emacs lisp libraries |
| 24 | @direntry |
| 25 | * SMTP: (smtpmail). Emacs library for sending mail via SMTP. |
| 26 | @end direntry |
| 27 | |
| 28 | @titlepage |
| 29 | @title{Emacs SMTP Library} |
| 30 | @subtitle{An Emacs package for sending mail via SMTP} |
| 31 | @author{Simon Josefsson, Alex Schroeder} |
| 32 | @page |
| 33 | @vskip 0pt plus 1filll |
| 34 | @insertcopying |
| 35 | @end titlepage |
| 36 | |
| 37 | @contents |
| 38 | |
| 39 | @ifnottex |
| 40 | @node Top |
| 41 | @top Emacs SMTP Library |
| 42 | |
| 43 | @insertcopying |
| 44 | @end ifnottex |
| 45 | |
| 46 | @menu |
| 47 | * How Mail Works:: Brief introduction to mail concepts. |
| 48 | * Emacs Speaks SMTP:: How to use the SMTP library in Emacs. |
| 49 | * Authentication:: Authenticating yourself to the server. |
| 50 | * Encryption:: Protecting your connection to the server. |
| 51 | * Queued delivery:: Sending mail without an internet connection. |
| 52 | * Server workarounds:: Mail servers with special requirements. |
| 53 | * Debugging:: Tracking down problems. |
| 54 | * GNU Free Documentation License:: The license for this documentation. |
| 55 | |
| 56 | Indices |
| 57 | |
| 58 | * Index:: Index over variables and functions. |
| 59 | @end menu |
| 60 | |
| 61 | @node How Mail Works |
| 62 | @chapter How Mail Works |
| 63 | |
| 64 | @cindex SMTP |
| 65 | @cindex MTA |
| 66 | On the internet, mail is sent from mail host to mail host using the |
| 67 | simple mail transfer protocol (SMTP). To send and receive mail, you |
| 68 | must get it from and send it to a mail host. Every mail host runs a |
| 69 | mail transfer agent (MTA) such as Exim that accepts mails and passes |
| 70 | them on. The communication between a mail host and other clients does |
| 71 | not necessarily involve SMTP, however. Here is short overview of what |
| 72 | is involved. |
| 73 | |
| 74 | @cindex MUA |
| 75 | The mail program --- also called a mail user agent (MUA) --- |
| 76 | usually sends outgoing mail to a mail host. When your computer is |
| 77 | permanently connected to the internet, it might even be a mail host |
| 78 | itself. In this case, the MUA will pipe mail to the |
| 79 | @file{/usr/lib/sendmail} application. It will take care of your mail |
| 80 | and pass it on to the next mail host. |
| 81 | |
| 82 | @cindex ISP |
| 83 | When you are only connected to the internet from time to time, your |
| 84 | internet service provider (ISP) has probably told you which mail host |
| 85 | to use. You must configure your MUA to use that mail host. Since you |
| 86 | are reading this manual, you probably want to configure Emacs to use |
| 87 | SMTP to send mail to that mail host. More on that in the next |
| 88 | section. |
| 89 | |
| 90 | @cindex MDA |
| 91 | Things are different when reading mail. The mail host responsible |
| 92 | for your mail keeps it in a file somewhere. The messages get into the |
| 93 | file by way of a mail delivery agent (MDA) such as procmail. These |
| 94 | delivery agents often allow you to filter and munge your mails before |
| 95 | you get to see it. When your computer is that mail host, this file is |
| 96 | called a spool, and sometimes located in the directory |
| 97 | @file{/var/spool/mail/}. All your MUA has to do is read mail from the |
| 98 | spool, then. |
| 99 | |
| 100 | @cindex POP3 |
| 101 | @cindex IMAP |
| 102 | When your computer is not always connected to the internet, you |
| 103 | must get the mail from the remote mail host using a protocol such as |
| 104 | POP3 or IMAP@. POP3 essentially downloads all your mail from the mail |
| 105 | host to your computer. The mail is stored in some file on your |
| 106 | computer, and again, all your MUA has to do is read mail from the |
| 107 | spool. |
| 108 | |
| 109 | When you read mail from various machines, downloading mail from the |
| 110 | mail host to your current machine is not convenient. In that case, |
| 111 | you will probably want to use the IMAP protocol. Your mail is kept on |
| 112 | the mail host, and you can read it while you are connected via IMAP to |
| 113 | the mail host. |
| 114 | |
| 115 | @cindex Webmail |
| 116 | So how does reading mail via the web work, you ask. In that case, |
| 117 | the web interface just allows you to remote-control a MUA on the web |
| 118 | host. Whether the web host is also a mail host, and how all the |
| 119 | pieces interact is completely irrelevant. You usually cannot use |
| 120 | Emacs to read mail via the web, unless you use software that parses |
| 121 | the ever-changing HTML of the web interface. |
| 122 | |
| 123 | @node Emacs Speaks SMTP |
| 124 | @chapter Emacs Speaks SMTP |
| 125 | |
| 126 | Emacs includes a package for sending your mail to a SMTP server and |
| 127 | have it take care of delivering it to the final destination, rather |
| 128 | than letting the MTA on your local system take care of it. This can |
| 129 | be useful if you don't have a MTA set up on your host, or if your |
| 130 | machine is often disconnected from the internet. |
| 131 | |
| 132 | Sending mail via SMTP requires configuring your mail user agent |
| 133 | (@pxref{Mail Methods,,,emacs}) to use the SMTP library. If you |
| 134 | have not configured anything, then in Emacs 24.1 and later the first |
| 135 | time you try to send a mail Emacs will ask how you want to send |
| 136 | mail. To use this library, answer @samp{smtp} when prompted. Emacs |
| 137 | then asks for the name of the SMTP server. |
| 138 | |
| 139 | If you prefer, or if you are using a non-standard mail user agent, |
| 140 | you can configure this yourself. The normal way to do this is to set |
| 141 | the variable @code{send-mail-function} (@pxref{Mail |
| 142 | Sending,,,emacs}) to the value you want to use. To use this library: |
| 143 | |
| 144 | @smallexample |
| 145 | (setq send-mail-function 'smtpmail-send-it) |
| 146 | @end smallexample |
| 147 | |
| 148 | @noindent |
| 149 | The default value for this variable is @code{sendmail-query-once}, |
| 150 | which interactively asks how you want to send mail. |
| 151 | |
| 152 | Your mail user agent might use a different variable for this purpose. |
| 153 | It should inherit from @code{send-mail-function}, but if it does not, |
| 154 | or if you prefer, you can set that variable directly. Consult your |
| 155 | mail user agent's documentation for more details. For example, |
| 156 | (@pxref{Mail Variables,,,message}). |
| 157 | |
| 158 | Before using SMTP you must find out the hostname of the SMTP server |
| 159 | to use. Your system administrator or mail service provider should |
| 160 | supply this information. Often it is some variant of the server you |
| 161 | receive mail from. If your email address is |
| 162 | @samp{yourname@@example.com}, then the name of the SMTP server is |
| 163 | may be something like @samp{smtp.example.com}. |
| 164 | |
| 165 | @table @code |
| 166 | @item smtpmail-smtp-server |
| 167 | @vindex smtpmail-smtp-server |
| 168 | @vindex SMTPSERVER |
| 169 | The variable @code{smtpmail-smtp-server} controls the hostname of |
| 170 | the server to use. It is a string with an IP address or hostname. It |
| 171 | defaults to the contents of the @env{SMTPSERVER} environment |
| 172 | variable, or, if empty, the contents of |
| 173 | @code{smtpmail-default-smtp-server}. |
| 174 | |
| 175 | @item smtpmail-default-smtp-server |
| 176 | @vindex smtpmail-default-smtp-server |
| 177 | The variable @code{smtpmail-default-smtp-server} controls the |
| 178 | default hostname of the server to use. It is a string with an IP |
| 179 | address or hostname. It must be set before the SMTP library is |
| 180 | loaded. It has no effect if set after the SMTP library has been |
| 181 | loaded, or if @code{smtpmail-smtp-server} is defined. It is usually |
| 182 | set by system administrators in a site wide initialization file. |
| 183 | @end table |
| 184 | |
| 185 | The following example illustrates what you could put in |
| 186 | @file{~/.emacs} to set the SMTP server name. |
| 187 | |
| 188 | @example |
| 189 | ;; Send mail using SMTP via mail.example.org. |
| 190 | (setq smtpmail-smtp-server "mail.example.org") |
| 191 | @end example |
| 192 | |
| 193 | @cindex Mail Submission |
| 194 | SMTP is normally used on the registered ``smtp'' TCP service port 25. |
| 195 | Some environments use SMTP in ``Mail Submission'' mode, which uses |
| 196 | port 587. Using other ports is not uncommon, either for security by |
| 197 | obscurity purposes, port forwarding, or otherwise. |
| 198 | |
| 199 | @table @code |
| 200 | @item smtpmail-smtp-service |
| 201 | @vindex smtpmail-smtp-service |
| 202 | The variable @code{smtpmail-smtp-service} controls the port on the |
| 203 | server to contact. It is either a string, in which case it will be |
| 204 | translated into an integer using system calls, or an integer. |
| 205 | @end table |
| 206 | |
| 207 | The following example illustrates what you could put in |
| 208 | @file{~/.emacs} to set the SMTP service port. |
| 209 | |
| 210 | @example |
| 211 | ;; Send mail using SMTP on the mail submission port 587. |
| 212 | (setq smtpmail-smtp-service 587) |
| 213 | @end example |
| 214 | |
| 215 | @node Authentication |
| 216 | @chapter Authentication |
| 217 | |
| 218 | @cindex password |
| 219 | @cindex user name |
| 220 | Most SMTP servers require clients to authenticate themselves before |
| 221 | they are allowed to send mail. Authentication usually involves |
| 222 | supplying a user name and password. |
| 223 | |
| 224 | If you have not configured anything, then the first time you try to |
| 225 | send mail via a server, Emacs (version 24.1 and later) prompts you |
| 226 | for the user name and password to use, and then offers to save the |
| 227 | information. By default, Emacs stores authentication information in |
| 228 | a file @file{~/.authinfo}. |
| 229 | |
| 230 | @cindex authinfo |
| 231 | The basic format of the @file{~/.authinfo} file is one line for each |
| 232 | set of credentials. Each line consists of pairs of variables and |
| 233 | values. A simple example would be: |
| 234 | |
| 235 | @smallexample |
| 236 | machine mail.example.org port 25 login myuser password mypassword |
| 237 | @end smallexample |
| 238 | |
| 239 | @noindent |
| 240 | This specifies that when using the SMTP server called @samp{mail.example.org} |
| 241 | on port 25, Emacs should send the user name @samp{myuser} and the |
| 242 | password @samp{mypassword}. Either or both of the login and password |
| 243 | fields may be absent, in which case Emacs prompts for the information |
| 244 | when you try to send mail. (This replaces the old |
| 245 | @code{smtpmail-auth-credentials} variable used prior to Emacs 24.1.) |
| 246 | |
| 247 | @vindex smtpmail-smtp-user |
| 248 | When the SMTP library connects to a host on a certain port, it |
| 249 | searches the @file{~/.authinfo} file for a matching entry. If an |
| 250 | entry is found, the authentication process is invoked and the |
| 251 | credentials are used. If the variable @code{smtpmail-smtp-user} is |
| 252 | set to a non-@code{nil} value, then only entries for that user are |
| 253 | considered. For more information on the @file{~/.authinfo} |
| 254 | file, @pxref{Top,,auth-source, auth, Emacs auth-source Library}. |
| 255 | |
| 256 | @cindex SASL |
| 257 | @cindex CRAM-MD5 |
| 258 | @cindex PLAIN |
| 259 | @cindex LOGIN |
| 260 | The process by which the SMTP library authenticates you to the server |
| 261 | is known as ``Simple Authentication and Security Layer'' (SASL). |
| 262 | There are various SASL mechanisms, and this library supports three of |
| 263 | them: CRAM-MD5, PLAIN, and LOGIN@. It tries each of them, in that order, |
| 264 | until one succeeds. The first uses a form of encryption to obscure |
| 265 | your password, while the other two do not. |
| 266 | |
| 267 | |
| 268 | @node Encryption |
| 269 | @chapter Encryption |
| 270 | |
| 271 | @cindex STARTTLS |
| 272 | @cindex TLS |
| 273 | @cindex SSL |
| 274 | For greater security, you can encrypt your connection to the SMTP |
| 275 | server. If this is to work, both Emacs and the server must support it. |
| 276 | |
| 277 | The SMTP library supports the ``Transport Layer Security'' (TLS), and |
| 278 | the older ``Secure Sockets Layer'' (SSL) encryption mechanisms. |
| 279 | It also supports STARTTLS, which is a variant of TLS in which the |
| 280 | initial connection to the server is made in plain text, requesting a |
| 281 | switch to an encrypted channel for the rest of the process. |
| 282 | |
| 283 | @vindex smtpmail-stream-type |
| 284 | The variable @code{smtpmail-stream-type} controls what form of |
| 285 | connection the SMTP library uses. The default value is @code{nil}, |
| 286 | which means to use a plain connection, but try to switch to a STARTTLS |
| 287 | encrypted connection if the server supports it. Other possible values |
| 288 | are: @code{starttls} - insist on STARTTLS; @code{ssl} - use TLS/SSL; |
| 289 | and @code{plain} - no encryption. |
| 290 | |
| 291 | Use of any form of TLS/SSL requires support in Emacs. You can either |
| 292 | use the built-in support (in Emacs 24.1 and later), or the |
| 293 | @file{starttls.el} Lisp library. The built-in support uses the GnuTLS |
| 294 | @footnote{@url{http://www.gnu.org/software/gnutls/}} library. |
| 295 | If your Emacs has GnuTLS support built-in, the function |
| 296 | @code{gnutls-available-p} is defined and returns non-@code{nil}. |
| 297 | Otherwise, you must use the @file{starttls.el} library (see that file for |
| 298 | more information on customization options, etc.). The Lisp library |
| 299 | requires one of the following external tools to be installed: |
| 300 | |
| 301 | @enumerate |
| 302 | @item |
| 303 | The GnuTLS command line tool @samp{gnutls-cli}, which you can get from |
| 304 | @url{http://www.gnu.org/software/gnutls/}. This is the recommended |
| 305 | tool, mainly because it can verify server certificates. |
| 306 | |
| 307 | @item |
| 308 | The @samp{starttls} external program, which you can get from |
| 309 | @file{starttls-*.tar.gz} from @uref{ftp://ftp.opaopa.org/pub/elisp/}. |
| 310 | @end enumerate |
| 311 | |
| 312 | @cindex certificates |
| 313 | @cindex keys |
| 314 | The SMTP server may also request that you verify your identity by |
| 315 | sending a certificate and the associated encryption key to the server. |
| 316 | If you need to do this, you can use an @file{~/.authinfo} entry like this: |
| 317 | |
| 318 | @smallexample |
| 319 | machine mail.example.org port 25 key "~/.my_smtp_tls.key" cert "~/.my_smtp_tls.cert" |
| 320 | @end smallexample |
| 321 | |
| 322 | @noindent |
| 323 | (This replaces the old @code{smtpmail-starttls-credentials} variable used |
| 324 | prior to Emacs 24.1.) |
| 325 | |
| 326 | |
| 327 | @node Queued delivery |
| 328 | @chapter Queued delivery |
| 329 | |
| 330 | @cindex Dialup connection |
| 331 | If you connect to the internet via a dialup connection, or for some |
| 332 | other reason don't have permanent internet connection, sending mail |
| 333 | will fail when you are not connected. The SMTP library implements |
| 334 | queued delivery, and the following variable control its behavior. |
| 335 | |
| 336 | @table @code |
| 337 | @item smtpmail-queue-mail |
| 338 | @vindex smtpmail-queue-mail |
| 339 | The variable @code{smtpmail-queue-mail} controls whether a simple |
| 340 | off line mail sender is active. This variable is a boolean, and |
| 341 | defaults to @code{nil} (disabled). If this is non-@code{nil}, mail is |
| 342 | not sent immediately but rather queued in the directory |
| 343 | @code{smtpmail-queue-dir} and can be later sent manually by invoking |
| 344 | @code{smtpmail-send-queued-mail} (typically when you connect to the |
| 345 | internet). |
| 346 | |
| 347 | @item smtpmail-queue-dir |
| 348 | @vindex smtpmail-queue-dir |
| 349 | The variable @code{smtpmail-queue-dir} specifies the name of the |
| 350 | directory to hold queued messages. It defaults to |
| 351 | @file{~/Mail/queued-mail/}. |
| 352 | @end table |
| 353 | |
| 354 | @findex smtpmail-send-queued-mail |
| 355 | The function @code{smtpmail-send-queued-mail} can be used to send |
| 356 | any queued mail when @code{smtpmail-queue-mail} is enabled. It is |
| 357 | typically invoked interactively with @kbd{M-x |
| 358 | smtpmail-send-queued-mail RET} when you are connected to the internet. |
| 359 | |
| 360 | @node Server workarounds |
| 361 | @chapter Server workarounds |
| 362 | |
| 363 | Some SMTP servers have special requirements. The following variables |
| 364 | implement support for common requirements. |
| 365 | |
| 366 | @table @code |
| 367 | |
| 368 | @item smtpmail-local-domain |
| 369 | @vindex smtpmail-local-domain |
| 370 | The variable @code{smtpmail-local-domain} controls the hostname sent |
| 371 | in the first @code{EHLO} or @code{HELO} command sent to the server. |
| 372 | It should only be set if the @code{system-name} function returns a |
| 373 | name that isn't accepted by the server. Do not set this variable |
| 374 | unless your server complains. |
| 375 | |
| 376 | @item smtpmail-sendto-domain |
| 377 | @vindex smtpmail-sendto-domain |
| 378 | The variable @code{smtpmail-sendto-domain} makes the SMTP library |
| 379 | add @samp{@@} and the specified value to recipients specified in the |
| 380 | message when they are sent using the @code{RCPT TO} command. Some |
| 381 | configurations of sendmail requires this behavior. Don't bother to |
| 382 | set this unless you have get an error like: |
| 383 | |
| 384 | @example |
| 385 | Sending failed; SMTP protocol error |
| 386 | @end example |
| 387 | |
| 388 | when sending mail, and the debug buffer (@pxref{Debugging})) contains |
| 389 | an error such as: |
| 390 | |
| 391 | @example |
| 392 | RCPT TO: @var{someone} |
| 393 | 501 @var{someone}: recipient address must contain a domain |
| 394 | @end example |
| 395 | |
| 396 | @end table |
| 397 | |
| 398 | |
| 399 | @node Debugging |
| 400 | @chapter Debugging |
| 401 | |
| 402 | Sometimes delivery fails, often with the generic error message |
| 403 | @samp{Sending failed; SMTP protocol error}. Enabling one or both of |
| 404 | the following variables and inspecting a trace buffer will often give |
| 405 | clues to the reason for the error. |
| 406 | |
| 407 | @table @code |
| 408 | |
| 409 | @item smtpmail-debug-info |
| 410 | @vindex smtpmail-debug-info |
| 411 | The variable @code{smtpmail-debug-info} controls whether to print |
| 412 | the SMTP protocol exchange in the minibuffer, and retain the entire |
| 413 | exchange in a buffer @samp{*trace of SMTP session to @var{server}*}, |
| 414 | where @var{server} is the name of the mail server to which you send |
| 415 | mail. |
| 416 | |
| 417 | @item smtpmail-debug-verb |
| 418 | @vindex smtpmail-debug-verb |
| 419 | The variable @code{smtpmail-debug-verb} controls whether to send the |
| 420 | @code{VERB} token to the server. The @code{VERB} server instructs the |
| 421 | server to be more verbose, and often also to attempt final delivery |
| 422 | while your SMTP session is still running. It is usually only useful |
| 423 | together with @code{smtpmail-debug-info}. Note that this may cause |
| 424 | mail delivery to take considerable time if the final destination |
| 425 | cannot accept mail. |
| 426 | |
| 427 | @end table |
| 428 | |
| 429 | @node GNU Free Documentation License |
| 430 | @chapter GNU Free Documentation License |
| 431 | @include doclicense.texi |
| 432 | |
| 433 | @node Index |
| 434 | @chapter Index |
| 435 | |
| 436 | @section Concept Index |
| 437 | |
| 438 | @printindex cp |
| 439 | |
| 440 | @section Function and Variable Index |
| 441 | |
| 442 | @printindex fn |
| 443 | |
| 444 | @bye |