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