Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / mail / smtpmail.el
CommitLineData
092af6d8 1;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
24975917 2
73b0cd50 3;; Copyright (C) 1995-1996, 2001-2011 Free Software Foundation, Inc.
24975917
RS
4
5;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
9056f1c9
RS
6;; Maintainer: Simon Josefsson <simon@josefsson.org>
7;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
c50d5ce0 8;; ESMTP support: Simon Leinen <simon@switch.ch>
2b5c7e03
GM
9;; Hacked by Mike Taylor, 11th October 1999 to add support for
10;; automatically appending a domain to RCPT TO: addresses.
9056f1c9 11;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz>
24975917
RS
12;; Keywords: mail
13
14;; This file is part of GNU Emacs.
15
b1fc2b50 16;; GNU Emacs is free software: you can redistribute it and/or modify
24975917 17;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
18;; the Free Software Foundation, either version 3 of the License, or
19;; (at your option) any later version.
24975917
RS
20
21;; GNU Emacs is distributed in the hope that it will be useful,
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
b1fc2b50 27;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24975917
RS
28
29;;; Commentary:
30
31;; Send Mail to smtp host from smtpmail temp buffer.
24975917 32
f38d3514 33;; Please add these lines in your .emacs(_emacs) or use customize.
24975917 34;;
f38d3514 35;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
08b10dd4 36;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
24975917 37;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
24975917 38;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
e889eabc 39;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
f38d3514 40;;(setq smtpmail-debug-info t) ; only to debug problems
60ed3fa5 41;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
9056f1c9
RS
42;; '(("YOUR SMTP HOST" 25 "username" "password")))
43;;(setq smtpmail-starttls-credentials
44;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
758b7b09
SJ
45;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
46;; integer or a string, just as long as they match (eq).
24975917 47
75da36cc
RW
48;; To queue mail, set `smtpmail-queue-mail' to t and use
49;; `smtpmail-send-queued-mail' to send.
308bc5d8 50
9056f1c9
RS
51;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
52;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
53;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
54;; Rewritten by Simon Josefsson to use same credential variable as AUTH
55;; support below.
56
57;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
58;; Authentication by the AUTH mechanism.
59;; See http://www.ietf.org/rfc/rfc2554.txt
60
61;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
62;; STARTTLS. Requires external program
63;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
64;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
308bc5d8 65
24975917
RS
66;;; Code:
67
68(require 'sendmail)
863e372f 69(autoload 'starttls-any-program-available "starttls")
9056f1c9
RS
70(autoload 'starttls-open-stream "starttls")
71(autoload 'starttls-negotiate "starttls")
72(autoload 'mail-strip-quoted-names "mail-utils")
21bf0d6c
SM
73(autoload 'message-make-date "message")
74(autoload 'message-make-message-id "message")
9056f1c9 75(autoload 'rfc2104-hash "rfc2104")
60ed3fa5 76(autoload 'netrc-parse "netrc")
0d1c958a
RS
77(autoload 'netrc-machine "netrc")
78(autoload 'netrc-get "netrc")
b1cd57bc 79(autoload 'password-read "password-cache")
20e43520 80(autoload 'auth-source-user-or-password "auth-source")
e64a3841 81
24975917 82;;;
00ed33e7
RS
83(defgroup smtpmail nil
84 "SMTP protocol for sending mail."
85 :group 'mail)
24975917 86
00ed33e7
RS
87
88(defcustom smtpmail-default-smtp-server nil
0fc1e8fe 89 "Specify default SMTP server.
b559a041 90This only has effect if you specify it before loading the smtpmail library."
00ed33e7
RS
91 :type '(choice (const nil) string)
92 :group 'smtpmail)
93
a8ba4429 94(defcustom smtpmail-smtp-server
e2f7c221 95 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
0fc1e8fe 96 "The name of the host running SMTP server."
00ed33e7
RS
97 :type '(choice (const nil) string)
98 :group 'smtpmail)
24975917 99
00ed33e7 100(defcustom smtpmail-smtp-service 25
0fc1e8fe 101 "SMTP service port number.
1429d866 102The default value would be \"smtp\" or 25."
9056f1c9 103 :type '(choice (integer :tag "Port") (string :tag "Service"))
00ed33e7 104 :group 'smtpmail)
24975917 105
00ed33e7 106(defcustom smtpmail-local-domain nil
0fc1e8fe 107 "Local domain name without a host name.
1429d866 108If the function `system-name' returns the full internet address,
00ed33e7
RS
109don't define this value."
110 :type '(choice (const nil) string)
111 :group 'smtpmail)
112
e889eabc 113(defcustom smtpmail-sendto-domain nil
0fc1e8fe 114 "Local domain name without a host name.
e889eabc
GM
115This is appended (with an @-sign) to any specified recipients which do
116not include an @-sign, so that each RCPT TO address is fully qualified.
117\(Some configurations of sendmail require this.)
118
119Don't bother to set this unless you have get an error like:
120 Sending failed; SMTP protocol error
121when sending mail, and the *trace of SMTP session to <somewhere>*
122buffer includes an exchange like:
123 RCPT TO: <someone>
75da36cc 124 501 <someone>: recipient address must contain a domain."
e889eabc
GM
125 :type '(choice (const nil) string)
126 :group 'smtpmail)
127
00ed33e7 128(defcustom smtpmail-debug-info nil
9056f1c9
RS
129 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
130See also `smtpmail-debug-verb' which determines if the SMTP protocol should
131be verbose as well."
132 :type 'boolean
133 :group 'smtpmail)
134
135(defcustom smtpmail-debug-verb nil
136 "Whether this library sends the SMTP VERB command or not.
137The commands enables verbose information from the SMTP server."
00ed33e7
RS
138 :type 'boolean
139 :group 'smtpmail)
140
0fc1e8fe
GM
141(defcustom smtpmail-code-conv-from nil
142 "Coding system for encoding outgoing mail.
143Used for the value of `sendmail-coding-system' when
144`select-message-coding-system' is called. "
145 :type 'coding-system
00ed33e7 146 :group 'smtpmail)
24975917 147
a8ba4429 148(defcustom smtpmail-queue-mail nil
0fc1e8fe 149 "Non-nil means mail is queued; otherwise it is sent immediately.
308bc5d8
RS
150If queued, it is stored in the directory `smtpmail-queue-dir'
151and sent with `smtpmail-send-queued-mail'."
152 :type 'boolean
153 :group 'smtpmail)
154
155(defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
0fc1e8fe 156 "Directory where `smtpmail.el' stores queued mail."
308bc5d8
RS
157 :type 'directory
158 :group 'smtpmail)
159
60ed3fa5
RS
160(defcustom smtpmail-auth-credentials "~/.authinfo"
161 "Specify username and password for servers, directly or via .netrc file.
162This variable can either be a filename pointing to a file in netrc(5)
163format, or list of four-element lists that contain, in order,
9056f1c9 164`servername' (a string), `port' (an integer), `user' (a string) and
60ed3fa5
RS
165`password' (a string, or nil to query the user when needed). If you
166need to enter a `realm' too, add it to the user string, so that it
167looks like `user@realm'."
168 :type '(choice file
169 (repeat (list (string :tag "Server")
75da36cc
RW
170 (integer :tag "Port")
171 (string :tag "Username")
172 (choice (const :tag "Query when needed" nil)
60ed3fa5 173 (string :tag "Password")))))
bf247b6e 174 :version "22.1"
9056f1c9
RS
175 :group 'smtpmail)
176
177(defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
178 "Specify STARTTLS keys and certificates for servers.
179This is a list of four-element list with `servername' (a string),
8bf5f8c5
SJ
180`port' (an integer), `key' (a filename) and `certificate' (a
181filename).
182If you do not have a certificate/key pair, leave the `key' and
183`certificate' fields as `nil'. A key/certificate pair is only
184needed if you want to use X.509 client authenticated
185connections."
9056f1c9
RS
186 :type '(repeat (list (string :tag "Server")
187 (integer :tag "Port")
188 (file :tag "Key")
189 (file :tag "Certificate")))
190 :version "21.1"
191 :group 'smtpmail)
192
1b62b062 193(defcustom smtpmail-warn-about-unknown-extensions nil
0fc1e8fe 194 "If set, print warnings about unknown SMTP extensions.
1b62b062
GM
195This is mainly useful for development purposes, to learn about
196new SMTP extensions that might be useful to support."
197 :type 'boolean
fa87f673 198 :version "21.1"
1b62b062
GM
199 :group 'smtpmail)
200
0fc1e8fe 201(defcustom smtpmail-queue-index-file "index"
1429d866 202 "File name of queued mail index.
0fc1e8fe
GM
203This is relative to `smtpmail-queue-dir'."
204 :type 'string
205 :group 'smtpmail)
206
207;; End of customizable variables.
208
308bc5d8 209
fb035bbf
RS
210(defvar smtpmail-address-buffer)
211(defvar smtpmail-recipient-address-list)
212
08b10dd4
RS
213(defvar smtpmail-queue-counter 0)
214
fb035bbf
RS
215;; Buffer-local variable.
216(defvar smtpmail-read-point)
217
e81c51f0 218(defconst smtpmail-auth-supported '(cram-md5 plain login)
9927d250
SJ
219 "List of supported SMTP AUTH mechanisms.
220The list is in preference order.")
9056f1c9 221
850ed7b3 222(defvar smtpmail-mail-address nil
2265c623 223 "Value to use for envelope-from address for mail from ambient buffer.")
850ed7b3 224
f38d3514 225;;;###autoload
24975917
RS
226(defun smtpmail-send-it ()
227 (let ((errbuf (if mail-interactive
228 (generate-new-buffer " smtpmail errors")
229 0))
230 (tembuf (generate-new-buffer " smtpmail temp"))
231 (case-fold-search nil)
24975917 232 delimline
83af570e 233 (mailbuf (current-buffer))
ff981226
GM
234 ;; Examine this variable now, so that
235 ;; local binding in the mail buffer will take effect.
2265c623
GM
236 (smtpmail-mail-address
237 (or (and mail-specify-envelope-from (mail-envelope-from))
238 user-mail-address))
83af570e
KH
239 (smtpmail-code-conv-from
240 (if enable-multibyte-characters
241 (let ((sendmail-coding-system smtpmail-code-conv-from))
242 (select-message-coding-system)))))
24975917 243 (unwind-protect
937e6a56 244 (with-current-buffer tembuf
24975917 245 (erase-buffer)
75da36cc
RW
246 ;; Use the same `buffer-file-coding-system' as in the mail
247 ;; buffer, otherwise any `write-region' invocations (e.g., in
362e23e1
EZ
248 ;; mail-do-fcc below) will annoy with asking for a suitable
249 ;; encoding.
250 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
24975917
RS
251 (insert-buffer-substring mailbuf)
252 (goto-char (point-max))
253 ;; require one newline at the end.
254 (or (= (preceding-char) ?\n)
255 (insert ?\n))
256 ;; Change header-delimiter to be what sendmail expects.
92a3f23d 257 (mail-sendmail-undelimit-header)
24975917 258 (setq delimline (point-marker))
75da36cc 259 ;; (sendmail-synch-aliases)
24975917
RS
260 (if mail-aliases
261 (expand-mail-aliases (point-min) delimline))
262 (goto-char (point-min))
263 ;; ignore any blank lines in the header
264 (while (and (re-search-forward "\n\n\n*" delimline t)
265 (< (point) delimline))
266 (replace-match "\n"))
267 (let ((case-fold-search t))
5feeeae2
RS
268 ;; We used to process Resent-... headers here,
269 ;; but it was not done properly, and the job
75da36cc 270 ;; is done correctly in `smtpmail-deduce-address-list'.
24975917
RS
271 ;; Don't send out a blank subject line
272 (goto-char (point-min))
5feeeae2
RS
273 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
274 (replace-match "")
275 ;; This one matches a Subject just before the header delimiter.
276 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
277 (= (match-end 0) delimline))
278 (replace-match "")))
0e2701ca
RS
279 ;; Put the "From:" field in unless for some odd reason
280 ;; they put one in themselves.
281 (goto-char (point-min))
282 (if (not (re-search-forward "^From:" delimline t))
850ed7b3 283 (let* ((login smtpmail-mail-address)
0e2701ca
RS
284 (fullname (user-full-name)))
285 (cond ((eq mail-from-style 'angles)
286 (insert "From: " fullname)
287 (let ((fullname-start (+ (point-min) 6))
288 (fullname-end (point-marker)))
289 (goto-char fullname-start)
290 ;; Look for a character that cannot appear unquoted
291 ;; according to RFC 822.
292 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
293 fullname-end 1)
294 (progn
295 ;; Quote fullname, escaping specials.
296 (goto-char fullname-start)
297 (insert "\"")
298 (while (re-search-forward "[\"\\]"
299 fullname-end 1)
300 (replace-match "\\\\\\&" t))
301 (insert "\""))))
302 (insert " <" login ">\n"))
303 ((eq mail-from-style 'parens)
304 (insert "From: " login " (")
305 (let ((fullname-start (point)))
306 (insert fullname)
307 (let ((fullname-end (point-marker)))
308 (goto-char fullname-start)
309 ;; RFC 822 says \ and nonmatching parentheses
310 ;; must be escaped in comments.
311 ;; Escape every instance of ()\ ...
312 (while (re-search-forward "[()\\]" fullname-end 1)
313 (replace-match "\\\\\\&" t))
314 ;; ... then undo escaping of matching parentheses,
315 ;; including matching nested parentheses.
316 (goto-char fullname-start)
a8ba4429 317 (while (re-search-forward
0e2701ca
RS
318 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
319 fullname-end 1)
320 (replace-match "\\1(\\3)" t)
321 (goto-char fullname-start))))
322 (insert ")\n"))
323 ((null mail-from-style)
324 (insert "From: " login "\n")))))
21bf0d6c
SM
325 ;; Insert a `Message-Id:' field if there isn't one yet.
326 (goto-char (point-min))
327 (unless (re-search-forward "^Message-Id:" delimline t)
328 (insert "Message-Id: " (message-make-message-id) "\n"))
329 ;; Insert a `Date:' field if there isn't one yet.
330 (goto-char (point-min))
331 (unless (re-search-forward "^Date:" delimline t)
332 (insert "Date: " (message-make-date) "\n"))
362e23e1
EZ
333 ;; Possibly add a MIME header for the current coding system
334 (let (charset)
335 (goto-char (point-min))
336 (and (eq mail-send-nonascii 'mime)
337 (not (re-search-forward "^MIME-version:" delimline t))
338 (progn (skip-chars-forward "\0-\177")
339 (/= (point) (point-max)))
340 smtpmail-code-conv-from
341 (setq charset
342 (coding-system-get smtpmail-code-conv-from
343 'mime-charset))
344 (goto-char delimline)
345 (insert "MIME-version: 1.0\n"
346 "Content-type: text/plain; charset="
347 (symbol-name charset)
348 "\nContent-Transfer-Encoding: 8bit\n")))
24975917
RS
349 ;; Insert an extra newline if we need it to work around
350 ;; Sun's bug that swallows newlines.
351 (goto-char (1+ delimline))
352 (if (eval mail-mailer-swallows-blank-line)
353 (newline))
0e2701ca
RS
354 ;; Find and handle any FCC fields.
355 (goto-char (point-min))
356 (if (re-search-forward "^FCC:" delimline t)
75da36cc 357 ;; Force `mail-do-fcc' to use the encoding of the mail
362e23e1
EZ
358 ;; buffer to encode outgoing messages on FCC files.
359 (let ((coding-system-for-write smtpmail-code-conv-from))
360 (mail-do-fcc delimline)))
24975917 361 (if mail-interactive
21bf0d6c 362 (with-current-buffer errbuf
24975917
RS
363 (erase-buffer))))
364 ;;
24975917
RS
365 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
366 (setq smtpmail-recipient-address-list
75da36cc 367 (smtpmail-deduce-address-list tembuf (point-min) delimline))
24975917 368 (kill-buffer smtpmail-address-buffer)
a8ba4429 369
24975917 370 (smtpmail-do-bcc delimline)
75da36cc 371 ;; Send or queue
308bc5d8
RS
372 (if (not smtpmail-queue-mail)
373 (if (not (null smtpmail-recipient-address-list))
a8ba4429 374 (if (not (smtpmail-via-smtp
308bc5d8
RS
375 smtpmail-recipient-address-list tembuf))
376 (error "Sending failed; SMTP protocol error"))
377 (error "Sending failed; no recipients"))
bf6e31bc
SJ
378 (let* ((file-data
379 (expand-file-name
380 (format "%s_%i"
b2364eaa 381 (format-time-string "%Y-%m-%d_%H:%M:%S")
bf6e31bc
SJ
382 (setq smtpmail-queue-counter
383 (1+ smtpmail-queue-counter)))
384 smtpmail-queue-dir))
385 (file-data (convert-standard-filename file-data))
386 (file-elisp (concat file-data ".el"))
308bc5d8
RS
387 (buffer-data (create-file-buffer file-data))
388 (buffer-elisp (create-file-buffer file-elisp))
389 (buffer-scratch "*queue-mail*"))
2fbc1118
JB
390 (unless (file-exists-p smtpmail-queue-dir)
391 (make-directory smtpmail-queue-dir t))
21bf0d6c 392 (with-current-buffer buffer-data
308bc5d8 393 (erase-buffer)
95e4cc85
EZ
394 (set-buffer-file-coding-system
395 ;; We will be reading the file with no-conversion in
396 ;; smtpmail-send-queued-mail below, so write it out
397 ;; with Unix EOLs.
398 (coding-system-change-eol-conversion
399 (or smtpmail-code-conv-from 'undecided)
400 'unix)
401 nil t)
b0aea09e 402 (insert-buffer-substring tembuf)
308bc5d8
RS
403 (write-file file-data)
404 (set-buffer buffer-elisp)
405 (erase-buffer)
406 (insert (concat
407 "(setq smtpmail-recipient-address-list '"
408 (prin1-to-string smtpmail-recipient-address-list)
a8ba4429 409 ")\n"))
308bc5d8
RS
410 (write-file file-elisp)
411 (set-buffer (generate-new-buffer buffer-scratch))
412 (insert (concat file-data "\n"))
a8ba4429
SS
413 (append-to-file (point-min)
414 (point-max)
0fc1e8fe
GM
415 (expand-file-name smtpmail-queue-index-file
416 smtpmail-queue-dir)))
308bc5d8
RS
417 (kill-buffer buffer-scratch)
418 (kill-buffer buffer-data)
419 (kill-buffer buffer-elisp))))
24975917
RS
420 (kill-buffer tembuf)
421 (if (bufferp errbuf)
422 (kill-buffer errbuf)))))
423
74f39ce9 424;;;###autoload
308bc5d8
RS
425(defun smtpmail-send-queued-mail ()
426 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
427 (interactive)
7e3fa9f5 428 (with-temp-buffer
75da36cc
RW
429 ;; Get index, get first mail, send it, update index, get second
430 ;; mail, send it, etc...
0fc1e8fe
GM
431 (let ((file-msg "")
432 (qfile (expand-file-name smtpmail-queue-index-file
433 smtpmail-queue-dir)))
434 (insert-file-contents qfile)
88f2c9ad 435 (goto-char (point-min))
308bc5d8 436 (while (not (eobp))
21bf0d6c 437 (setq file-msg (buffer-substring (point) (line-end-position)))
308bc5d8 438 (load file-msg)
19a0baf7
EZ
439 ;; Insert the message literally: it is already encoded as per
440 ;; the MIME headers, and code conversions might guess the
441 ;; encoding wrongly.
7e3fa9f5
EZ
442 (with-temp-buffer
443 (let ((coding-system-for-read 'no-conversion))
444 (insert-file-contents file-msg))
ff981226
GM
445 (let ((smtpmail-mail-address
446 (or (and mail-specify-envelope-from (mail-envelope-from))
447 user-mail-address)))
448 (if (not (null smtpmail-recipient-address-list))
449 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
450 (current-buffer)))
451 (error "Sending failed; SMTP protocol error"))
452 (error "Sending failed; no recipients"))))
308bc5d8
RS
453 (delete-file file-msg)
454 (delete-file (concat file-msg ".el"))
95754b9f 455 (delete-region (point-at-bol) (point-at-bol 2)))
0fc1e8fe 456 (write-region (point-min) (point-max) qfile))))
24975917 457
75da36cc 458;; (defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
24975917
RS
459
460(defun smtpmail-fqdn ()
461 (if smtpmail-local-domain
462 (concat (system-name) "." smtpmail-local-domain)
463 (system-name)))
464
9056f1c9
RS
465(defsubst smtpmail-cred-server (cred)
466 (nth 0 cred))
467
468(defsubst smtpmail-cred-port (cred)
469 (nth 1 cred))
470
471(defsubst smtpmail-cred-key (cred)
472 (nth 2 cred))
473
474(defsubst smtpmail-cred-user (cred)
475 (nth 2 cred))
476
477(defsubst smtpmail-cred-cert (cred)
478 (nth 3 cred))
479
480(defsubst smtpmail-cred-passwd (cred)
481 (nth 3 cred))
482
483(defun smtpmail-find-credentials (cred server port)
484 (catch 'done
485 (let ((l cred) el)
486 (while (setq el (pop l))
487 (when (and (equal server (smtpmail-cred-server el))
488 (equal port (smtpmail-cred-port el)))
489 (throw 'done el))))))
490
2b5c7e03
GM
491(defun smtpmail-maybe-append-domain (recipient)
492 (if (or (not smtpmail-sendto-domain)
493 (string-match "@" recipient))
494 recipient
495 (concat recipient "@" smtpmail-sendto-domain)))
496
9056f1c9
RS
497(defun smtpmail-intersection (list1 list2)
498 (let ((result nil))
499 (dolist (el2 list2)
500 (when (memq el2 list1)
501 (push el2 result)))
502 (nreverse result)))
503
88f2c9ad
RS
504(defvar starttls-extra-args)
505(defvar starttls-extra-arguments)
506
9056f1c9
RS
507(defun smtpmail-open-stream (process-buffer host port)
508 (let ((cred (smtpmail-find-credentials
509 smtpmail-starttls-credentials host port)))
373f0312 510 (if (null (and cred (starttls-any-program-available)))
9056f1c9
RS
511 ;; The normal case.
512 (open-network-stream "SMTP" process-buffer host port)
513 (let* ((cred-key (smtpmail-cred-key cred))
514 (cred-cert (smtpmail-cred-cert cred))
515 (starttls-extra-args
e8620cb6
SJ
516 (append
517 starttls-extra-args
518 (when (and (stringp cred-key) (stringp cred-cert)
519 (file-regular-p
520 (setq cred-key (expand-file-name cred-key)))
521 (file-regular-p
522 (setq cred-cert (expand-file-name cred-cert))))
523 (list "--key-file" cred-key "--cert-file" cred-cert))))
4aa609dc 524 (starttls-extra-arguments
e8620cb6
SJ
525 (append
526 starttls-extra-arguments
527 (when (and (stringp cred-key) (stringp cred-cert)
528 (file-regular-p
529 (setq cred-key (expand-file-name cred-key)))
530 (file-regular-p
531 (setq cred-cert (expand-file-name cred-cert))))
532 (list "--x509keyfile" cred-key "--x509certfile" cred-cert)))))
9056f1c9
RS
533 (starttls-open-stream "SMTP" process-buffer host port)))))
534
75da36cc 535;; `password-read' autoloads password-cache.
48a731fe
GM
536(declare-function password-cache-add "password-cache" (key password))
537
9056f1c9
RS
538(defun smtpmail-try-auth-methods (process supported-extensions host port)
539 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
9927d250 540 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported)))
20e43520 541 (auth-user (auth-source-user-or-password
e64a3841 542 "login" host (or port "smtp")))
20e43520 543 (auth-pass (auth-source-user-or-password
e64a3841
TZ
544 "password" host (or port "smtp")))
545 (cred (if (and auth-user auth-pass) ; try user-auth-* before netrc-*
546 (list host port auth-user auth-pass)
547 ;; else, if auth-source didn't return them...
548 (if (stringp smtpmail-auth-credentials)
549 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
550 (port-name (format "%s" (or port "smtp")))
551 (hostentry (netrc-machine netrc host port-name
552 port-name)))
553 (when hostentry
554 (list host port
555 (netrc-get hostentry "login")
556 (netrc-get hostentry "password"))))
75da36cc
RW
557 ;; else, try `smtpmail-find-credentials' since
558 ;; `smtpmail-auth-credentials' is not a string
e64a3841
TZ
559 (smtpmail-find-credentials
560 smtpmail-auth-credentials host port))))
5b5eae56
BG
561 (prompt (when cred (format "SMTP password for %s:%s: "
562 (smtpmail-cred-server cred)
563 (smtpmail-cred-port cred))))
9056f1c9
RS
564 (passwd (when cred
565 (or (smtpmail-cred-passwd cred)
5b5eae56 566 (password-read prompt prompt))))
9056f1c9 567 ret)
dbfa9ed4 568 (when (and cred mech)
9056f1c9
RS
569 (cond
570 ((eq mech 'cram-md5)
1a63439b 571 (smtpmail-send-command process (upcase (format "AUTH %s" mech)))
9056f1c9
RS
572 (if (or (null (car (setq ret (smtpmail-read-response process))))
573 (not (integerp (car ret)))
574 (>= (car ret) 400))
575 (throw 'done nil))
576 (when (eq (car ret) 334)
577 (let* ((challenge (substring (cadr ret) 4))
578 (decoded (base64-decode-string challenge))
579 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
580 (response (concat (smtpmail-cred-user cred) " " hash))
9b3bf5e8
RS
581 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
582 ;; SMTP auth fails because the SMTP server identifies
583 ;; only the first part of the string (delimited by
584 ;; new line characters) as a response from the
585 ;; client, and the rest as distinct commands.
586
587 ;; In my case, the response string is 80 characters
588 ;; long. Without the no-line-break option for
75da36cc 589 ;; `base64-encode-string', only the first 76 characters
9b3bf5e8
RS
590 ;; are taken as a response to the server, and the
591 ;; authentication fails.
e4eb8462 592 (encoded (base64-encode-string response t)))
9056f1c9
RS
593 (smtpmail-send-command process (format "%s" encoded))
594 (if (or (null (car (setq ret (smtpmail-read-response process))))
595 (not (integerp (car ret)))
596 (>= (car ret) 400))
597 (throw 'done nil)))))
598 ((eq mech 'login)
599 (smtpmail-send-command process "AUTH LOGIN")
600 (if (or (null (car (setq ret (smtpmail-read-response process))))
601 (not (integerp (car ret)))
602 (>= (car ret) 400))
603 (throw 'done nil))
604 (smtpmail-send-command
6c77efbe 605 process (base64-encode-string (smtpmail-cred-user cred) t))
9056f1c9
RS
606 (if (or (null (car (setq ret (smtpmail-read-response process))))
607 (not (integerp (car ret)))
608 (>= (car ret) 400))
609 (throw 'done nil))
e4eb8462 610 (smtpmail-send-command process (base64-encode-string passwd t))
9056f1c9
RS
611 (if (or (null (car (setq ret (smtpmail-read-response process))))
612 (not (integerp (car ret)))
613 (>= (car ret) 400))
614 (throw 'done nil)))
4effb112 615 ((eq mech 'plain)
0c91399a
SJ
616 ;; We used to send an empty initial request, and wait for an
617 ;; empty response, and then send the password, but this
618 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
619 ;; is not sent if the server did not advertise AUTH PLAIN in
620 ;; the EHLO response. See RFC 2554 for more info.
c3f69831
SJ
621 (smtpmail-send-command process
622 (concat "AUTH PLAIN "
623 (base64-encode-string
4effb112
SJ
624 (concat "\0"
625 (smtpmail-cred-user cred)
626 "\0"
e4eb8462 627 passwd) t)))
4effb112
SJ
628 (if (or (null (car (setq ret (smtpmail-read-response process))))
629 (not (integerp (car ret)))
630 (not (equal (car ret) 235)))
631 (throw 'done nil)))
632
9056f1c9 633 (t
4effb112 634 (error "Mechanism %s not implemented" mech)))
9056f1c9 635 ;; Remember the password.
5b5eae56
BG
636 (when (null (smtpmail-cred-passwd cred))
637 (password-cache-add prompt passwd)))))
9056f1c9 638
24975917
RS
639(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
640 (let ((process nil)
f38d3514
KH
641 (host (or smtpmail-smtp-server
642 (error "`smtpmail-smtp-server' not defined")))
8805249b 643 (port smtpmail-smtp-service)
75da36cc 644 ;; `smtpmail-mail-address' should be set to the appropriate
ff981226
GM
645 ;; buffer-local value by the caller, but in case not:
646 (envelope-from (or smtpmail-mail-address
647 (and mail-specify-envelope-from
648 (mail-envelope-from))
649 user-mail-address))
24975917 650 response-code
8805249b 651 greeting
c50d5ce0
RS
652 process-buffer
653 (supported-extensions '()))
24975917
RS
654 (unwind-protect
655 (catch 'done
656 ;; get or create the trace buffer
657 (setq process-buffer
658 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
659
660 ;; clear the trace buffer of old output
21bf0d6c 661 (with-current-buffer process-buffer
9cf328cc 662 (setq buffer-undo-list t)
24975917
RS
663 (erase-buffer))
664
665 ;; open the connection to the server
9056f1c9 666 (setq process (smtpmail-open-stream process-buffer host port))
24975917
RS
667 (and (null process) (throw 'done nil))
668
669 ;; set the send-filter
670 (set-process-filter process 'smtpmail-process-filter)
671
21bf0d6c 672 (with-current-buffer process-buffer
4b876894 673 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
24975917
RS
674 (make-local-variable 'smtpmail-read-point)
675 (setq smtpmail-read-point (point-min))
676
a8ba4429 677
24975917
RS
678 (if (or (null (car (setq greeting (smtpmail-read-response process))))
679 (not (integerp (car greeting)))
680 (>= (car greeting) 400))
75da36cc 681 (throw 'done nil))
24975917 682
9056f1c9
RS
683 (let ((do-ehlo t)
684 (do-starttls t))
685 (while do-ehlo
75da36cc
RW
686 ;; EHLO
687 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
688
689 (if (or (null (car (setq response-code
690 (smtpmail-read-response process))))
691 (not (integerp (car response-code)))
692 (>= (car response-code) 400))
693 (progn
694 ;; HELO
695 (smtpmail-send-command
696 process (format "HELO %s" (smtpmail-fqdn)))
697
698 (if (or (null (car (setq response-code
699 (smtpmail-read-response process))))
700 (not (integerp (car response-code)))
701 (>= (car response-code) 400))
702 (throw 'done nil)))
703 (dolist (line (cdr (cdr response-code)))
704 (let ((name
705 (with-case-table ascii-case-table
706 (mapcar (lambda (s) (intern (downcase s)))
707 (split-string (substring line 4) "[ ]")))))
708 (and (eq (length name) 1)
709 (setq name (car name)))
710 (and name
711 (cond ((memq (if (consp name) (car name) name)
712 '(verb xvrb 8bitmime onex xone
713 expn size dsn etrn
714 enhancedstatuscodes
715 help xusr
716 auth=login auth starttls))
717 (setq supported-extensions
718 (cons name supported-extensions)))
719 (smtpmail-warn-about-unknown-extensions
720 (message "Unknown extension %s" name)))))))
721
722 (if (and do-starttls
723 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
724 (member 'starttls supported-extensions)
725 (numberp (process-id process)))
726 (progn
727 (smtpmail-send-command process (format "STARTTLS"))
728 (if (or (null (car (setq response-code (smtpmail-read-response process))))
729 (not (integerp (car response-code)))
730 (>= (car response-code) 400))
731 (throw 'done nil))
732 (starttls-negotiate process)
733 (setq do-starttls nil))
734 (setq do-ehlo nil))))
a8ba4429 735
9056f1c9 736 (smtpmail-try-auth-methods process supported-extensions host port)
c50d5ce0
RS
737
738 (if (or (member 'onex supported-extensions)
739 (member 'xone supported-extensions))
740 (progn
741 (smtpmail-send-command process (format "ONEX"))
742 (if (or (null (car (setq response-code (smtpmail-read-response process))))
743 (not (integerp (car response-code)))
744 (>= (car response-code) 400))
745 (throw 'done nil))))
746
9056f1c9 747 (if (and smtpmail-debug-verb
c50d5ce0
RS
748 (or (member 'verb supported-extensions)
749 (member 'xvrb supported-extensions)))
750 (progn
751 (smtpmail-send-command process (format "VERB"))
752 (if (or (null (car (setq response-code (smtpmail-read-response process))))
753 (not (integerp (car response-code)))
754 (>= (car response-code) 400))
755 (throw 'done nil))))
756
757 (if (member 'xusr supported-extensions)
758 (progn
759 (smtpmail-send-command process (format "XUSR"))
760 (if (or (null (car (setq response-code (smtpmail-read-response process))))
761 (not (integerp (car response-code)))
762 (>= (car response-code) 400))
763 (throw 'done nil))))
24975917 764
a8df98fd 765 ;; MAIL FROM:<sender>
c50d5ce0 766 (let ((size-part
9056f1c9
RS
767 (if (or (member 'size supported-extensions)
768 (assoc 'size supported-extensions))
c50d5ce0 769 (format " SIZE=%d"
21bf0d6c 770 (with-current-buffer smtpmail-text-buffer
c50d5ce0
RS
771 ;; size estimate:
772 (+ (- (point-max) (point-min))
773 ;; Add one byte for each change-of-line
73921ac1
GM
774 ;; because of CR-LF representation:
775 (count-lines (point-min) (point-max)))))
c50d5ce0
RS
776 ""))
777 (body-part
778 (if (member '8bitmime supported-extensions)
779 ;; FIXME:
780 ;; Code should be added here that transforms
781 ;; the contents of the message buffer into
782 ;; something the receiving SMTP can handle.
783 ;; For a receiver that supports 8BITMIME, this
784 ;; may mean converting BINARY to BASE64, or
785 ;; adding Content-Transfer-Encoding and the
786 ;; other MIME headers. The code should also
787 ;; return an indication of what encoding the
788 ;; message buffer is now, i.e. ASCII or
789 ;; 8BITMIME.
790 (if nil
791 " BODY=8BITMIME"
792 "")
793 "")))
75da36cc 794 ;; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
a8df98fd 795 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
ff981226 796 envelope-from
c50d5ce0
RS
797 size-part
798 body-part))
a1506d29 799
c50d5ce0
RS
800 (if (or (null (car (setq response-code (smtpmail-read-response process))))
801 (not (integerp (car response-code)))
802 (>= (car response-code) 400))
75da36cc 803 (throw 'done nil)))
a8ba4429 804
a8df98fd 805 ;; RCPT TO:<recipient>
8805249b
RS
806 (let ((n 0))
807 (while (not (null (nth n recipient)))
a8df98fd 808 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
8805249b
RS
809 (setq n (1+ n))
810
c50d5ce0
RS
811 (setq response-code (smtpmail-read-response process))
812 (if (or (null (car response-code))
8805249b
RS
813 (not (integerp (car response-code)))
814 (>= (car response-code) 400))
75da36cc 815 (throw 'done nil))))
a8ba4429 816
24975917
RS
817 ;; DATA
818 (smtpmail-send-command process "DATA")
819
820 (if (or (null (car (setq response-code (smtpmail-read-response process))))
821 (not (integerp (car response-code)))
822 (>= (car response-code) 400))
75da36cc 823 (throw 'done nil))
24975917
RS
824
825 ;; Mail contents
826 (smtpmail-send-data process smtpmail-text-buffer)
827
75da36cc 828 ;; DATA end "."
24975917
RS
829 (smtpmail-send-command process ".")
830
831 (if (or (null (car (setq response-code (smtpmail-read-response process))))
832 (not (integerp (car response-code)))
833 (>= (car response-code) 400))
75da36cc
RW
834 (throw 'done nil))
835
836 ;; QUIT
837 ;; (smtpmail-send-command process "QUIT")
838 ;; (and (null (car (smtpmail-read-response process)))
839 ;; (throw 'done nil))
840 t))
24975917 841 (if process
21bf0d6c 842 (with-current-buffer (process-buffer process)
24975917
RS
843 (smtpmail-send-command process "QUIT")
844 (smtpmail-read-response process)
845
75da36cc
RW
846 ;; (if (or (null (car (setq response-code (smtpmail-read-response process))))
847 ;; (not (integerp (car response-code)))
848 ;; (>= (car response-code) 400))
849 ;; (throw 'done nil))
1121afb4
SJ
850 (delete-process process)
851 (unless smtpmail-debug-info
852 (kill-buffer process-buffer)))))))
24975917
RS
853
854
24975917 855(defun smtpmail-process-filter (process output)
21bf0d6c 856 (with-current-buffer (process-buffer process)
24975917
RS
857 (goto-char (point-max))
858 (insert output)))
859
24975917
RS
860(defun smtpmail-read-response (process)
861 (let ((case-fold-search nil)
c50d5ce0 862 (response-strings nil)
24975917 863 (response-continue t)
c50d5ce0 864 (return-value '(nil ()))
24975917 865 match-end)
2ceed428
SJ
866 (catch 'done
867 (while response-continue
868 (goto-char smtpmail-read-point)
869 (while (not (search-forward "\r\n" nil t))
870 (unless (memq (process-status process) '(open run))
871 (throw 'done nil))
872 (accept-process-output process)
873 (goto-char smtpmail-read-point))
874
875 (setq match-end (point))
876 (setq response-strings
877 (cons (buffer-substring smtpmail-read-point (- match-end 2))
878 response-strings))
879
880 (goto-char smtpmail-read-point)
881 (if (looking-at "[0-9]+ ")
882 (let ((begin (match-beginning 0))
883 (end (match-end 0)))
884 (if smtpmail-debug-info
885 (message "%s" (car response-strings)))
886
887 (setq smtpmail-read-point match-end)
888
889 ;; ignore lines that start with "0"
890 (if (looking-at "0[0-9]+ ")
891 nil
892 (setq response-continue nil)
893 (setq return-value
027a4b6b 894 (cons (string-to-number
2ceed428
SJ
895 (buffer-substring begin end))
896 (nreverse response-strings)))))
897
898 (if (looking-at "[0-9]+-")
899 (progn (if smtpmail-debug-info
900 (message "%s" (car response-strings)))
901 (setq smtpmail-read-point match-end)
902 (setq response-continue t))
903 (progn
904 (setq smtpmail-read-point match-end)
c50d5ce0
RS
905 (setq response-continue nil)
906 (setq return-value
2ceed428
SJ
907 (cons nil (nreverse response-strings)))))))
908 (setq smtpmail-read-point match-end))
24975917
RS
909 return-value))
910
911
24975917
RS
912(defun smtpmail-send-command (process command)
913 (goto-char (point-max))
914 (if (= (aref command 0) ?P)
915 (insert "PASS <omitted>\r\n")
916 (insert command "\r\n"))
917 (setq smtpmail-read-point (point))
918 (process-send-string process command)
919 (process-send-string process "\r\n"))
920
24975917
RS
921(defun smtpmail-send-data-1 (process data)
922 (goto-char (point-max))
923
83af570e
KH
924 (if (and (multibyte-string-p data)
925 smtpmail-code-conv-from)
926 (setq data (string-as-multibyte
927 (encode-coding-string data smtpmail-code-conv-from))))
a8ba4429 928
24975917
RS
929 (if smtpmail-debug-info
930 (insert data "\r\n"))
931
932 (setq smtpmail-read-point (point))
57810560
KH
933 ;; Escape "." at start of a line
934 (if (eq (string-to-char data) ?.)
24975917 935 (process-send-string process "."))
57810560 936 (process-send-string process data)
75da36cc 937 (process-send-string process "\r\n"))
24975917
RS
938
939(defun smtpmail-send-data (process buffer)
dd64e5e5 940 (let ((data-continue t) sending-data)
21bf0d6c 941 (with-current-buffer buffer
24975917 942 (goto-char (point-min)))
24975917 943 (while data-continue
21bf0d6c 944 (with-current-buffer buffer
dd64e5e5
GM
945 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
946 (end-of-line 2)
947 (setq data-continue (not (eobp))))
948 (smtpmail-send-data-1 process sending-data))))
24975917
RS
949
950(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
951 "Get address list suitable for smtp RCPT TO: <address>."
1188dd37 952 (unwind-protect
21bf0d6c
SM
953 (with-current-buffer smtpmail-address-buffer
954 (erase-buffer)
75da36cc
RW
955 (let ((case-fold-search t)
956 (simple-address-list "")
957 this-line
958 this-line-end
959 addr-regexp)
24975917
RS
960 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
961 (goto-char (point-min))
13f1d088
KH
962 ;; RESENT-* fields should stop processing of regular fields.
963 (save-excursion
21bf0d6c
SM
964 (setq addr-regexp
965 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
966 header-end t)
967 "^Resent-\\(to\\|cc\\|bcc\\):"
968 "^\\(To:\\|Cc:\\|Bcc:\\)")))
13f1d088
KH
969
970 (while (re-search-forward addr-regexp header-end t)
24975917
RS
971 (replace-match "")
972 (setq this-line (match-beginning 0))
973 (forward-line 1)
974 ;; get any continuation lines
975 (while (and (looking-at "^[ \t]+") (< (point) header-end))
976 (forward-line 1))
977 (setq this-line-end (point-marker))
978 (setq simple-address-list
979 (concat simple-address-list " "
75da36cc 980 (mail-strip-quoted-names (buffer-substring this-line this-line-end)))))
24975917 981 (erase-buffer)
92dfd10c 982 (insert " " simple-address-list "\n")
75da36cc
RW
983 (subst-char-in-region (point-min) (point-max) 10 ? t) ; newline --> blank
984 (subst-char-in-region (point-min) (point-max) ?, ? t) ; comma --> blank
985 (subst-char-in-region (point-min) (point-max) 9 ? t) ; tab --> blank
24975917
RS
986
987 (goto-char (point-min))
988 ;; tidyness in case hook is not robust when it looks at this
989 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
990
991 (goto-char (point-min))
8805249b 992 (let (recipient-address-list)
e2f7c221 993 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
8805249b 994 (backward-char 1)
e2f7c221 995 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
75da36cc
RW
996 recipient-address-list)))
997 (setq smtpmail-recipient-address-list recipient-address-list))))))
24975917
RS
998
999(defun smtpmail-do-bcc (header-end)
5feeeae2 1000 "Delete [Resent-]BCC: and their continuation lines from the header area.
24975917
RS
1001There may be multiple BCC: lines, and each may have arbitrarily
1002many continuation lines."
1003 (let ((case-fold-search t))
067427f5
KH
1004 (save-excursion
1005 (goto-char (point-min))
1006 ;; iterate over all BCC: lines
1007 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
1008 (delete-region (match-beginning 0)
1009 (progn (forward-line 1) (point)))
1010 ;; get rid of any continuation lines
1011 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
1012 (replace-match ""))))))
24975917 1013
24975917
RS
1014(provide 'smtpmail)
1015
092af6d8 1016;;; smtpmail.el ends here