Sync to HEAD
[bpt/emacs.git] / lisp / mail / smtpmail.el
CommitLineData
092af6d8 1;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
24975917 2
6b61353c
KH
3;; Copyright (C) 1995, 1996, 2001, 2002, 2003, 2004
4;; 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
19;; the Free Software Foundation; either version 2, or (at your option)
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
29;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30;; Boston, MA 02111-1307, USA.
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")))
6b61353c
KH
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")
24975917
RS
81
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
b559a041
SJ
89 "*Specify default SMTP server.
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)
00ed33e7
RS
96 "*The name of the host running SMTP server."
97 :type '(choice (const nil) string)
98 :group 'smtpmail)
24975917 99
00ed33e7 100(defcustom smtpmail-smtp-service 25
9056f1c9
RS
101 "*SMTP service port number.
102The default value would be \"smtp\" or 25 ."
103 :type '(choice (integer :tag "Port") (string :tag "Service"))
00ed33e7 104 :group 'smtpmail)
24975917 105
00ed33e7 106(defcustom smtpmail-local-domain nil
24975917
RS
107 "*Local domain name without a host name.
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
GM
113(defcustom smtpmail-sendto-domain nil
114 "*Local domain name without a host name.
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>
124 501 <someone>: recipient address must contain a domain
125"
126 :type '(choice (const nil) string)
127 :group 'smtpmail)
128
00ed33e7 129(defcustom smtpmail-debug-info nil
9056f1c9
RS
130 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
131See also `smtpmail-debug-verb' which determines if the SMTP protocol should
132be verbose as well."
133 :type 'boolean
134 :group 'smtpmail)
135
136(defcustom smtpmail-debug-verb nil
137 "Whether this library sends the SMTP VERB command or not.
138The commands enables verbose information from the SMTP server."
00ed33e7
RS
139 :type 'boolean
140 :group 'smtpmail)
141
142(defcustom smtpmail-code-conv-from nil ;; *junet*
143 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
144 :type 'boolean
145 :group 'smtpmail)
24975917 146
a8ba4429 147(defcustom smtpmail-queue-mail nil
308bc5d8
RS
148 "*Specify if mail is queued (if t) or sent immediately (if nil).
149If queued, it is stored in the directory `smtpmail-queue-dir'
150and sent with `smtpmail-send-queued-mail'."
151 :type 'boolean
152 :group 'smtpmail)
153
154(defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
155 "*Directory where `smtpmail.el' stores queued mail."
156 :type 'directory
157 :group 'smtpmail)
158
60ed3fa5
RS
159(defcustom smtpmail-auth-credentials "~/.authinfo"
160 "Specify username and password for servers, directly or via .netrc file.
161This variable can either be a filename pointing to a file in netrc(5)
162format, or list of four-element lists that contain, in order,
9056f1c9 163`servername' (a string), `port' (an integer), `user' (a string) and
60ed3fa5
RS
164`password' (a string, or nil to query the user when needed). If you
165need to enter a `realm' too, add it to the user string, so that it
166looks like `user@realm'."
167 :type '(choice file
168 (repeat (list (string :tag "Server")
9056f1c9
RS
169 (integer :tag "Port")
170 (string :tag "Username")
171 (choice (const :tag "Query when needed" nil)
60ed3fa5 172 (string :tag "Password")))))
1a8e727b 173 :version "21.4"
9056f1c9
RS
174 :group 'smtpmail)
175
176(defcustom smtpmail-starttls-credentials '(("" 25 "" ""))
177 "Specify STARTTLS keys and certificates for servers.
178This is a list of four-element list with `servername' (a string),
179`port' (an integer), `key' (a filename) and `certificate' (a filename)."
180 :type '(repeat (list (string :tag "Server")
181 (integer :tag "Port")
182 (file :tag "Key")
183 (file :tag "Certificate")))
184 :version "21.1"
185 :group 'smtpmail)
186
1b62b062
GM
187(defcustom smtpmail-warn-about-unknown-extensions nil
188 "*If set, print warnings about unknown SMTP extensions.
189This is mainly useful for development purposes, to learn about
190new SMTP extensions that might be useful to support."
191 :type 'boolean
fa87f673 192 :version "21.1"
1b62b062
GM
193 :group 'smtpmail)
194
308bc5d8
RS
195(defvar smtpmail-queue-index-file "index"
196 "File name of queued mail index,
197This is relative to `smtpmail-queue-dir'.")
198
fb035bbf
RS
199(defvar smtpmail-address-buffer)
200(defvar smtpmail-recipient-address-list)
201
08b10dd4
RS
202(defvar smtpmail-queue-counter 0)
203
fb035bbf
RS
204;; Buffer-local variable.
205(defvar smtpmail-read-point)
206
308bc5d8
RS
207(defvar smtpmail-queue-index (concat smtpmail-queue-dir
208 smtpmail-queue-index-file))
209
9056f1c9
RS
210(defconst smtpmail-auth-supported '(cram-md5 login)
211 "List of supported SMTP AUTH mechanisms.")
212
24975917
RS
213;;;
214;;;
215;;;
216
850ed7b3 217(defvar smtpmail-mail-address nil
6b61353c 218 "Value to use for envelope-from address for mail from ambient buffer.")
850ed7b3 219
f38d3514 220;;;###autoload
24975917
RS
221(defun smtpmail-send-it ()
222 (let ((errbuf (if mail-interactive
223 (generate-new-buffer " smtpmail errors")
224 0))
225 (tembuf (generate-new-buffer " smtpmail temp"))
226 (case-fold-search nil)
24975917 227 delimline
83af570e 228 (mailbuf (current-buffer))
6b61353c
KH
229 ;; Examine this variable now, so that
230 ;; local binding in the mail buffer will take effect.
231 (smtpmail-mail-address
232 (or (and mail-specify-envelope-from (mail-envelope-from))
233 user-mail-address))
83af570e
KH
234 (smtpmail-code-conv-from
235 (if enable-multibyte-characters
236 (let ((sendmail-coding-system smtpmail-code-conv-from))
237 (select-message-coding-system)))))
24975917
RS
238 (unwind-protect
239 (save-excursion
240 (set-buffer tembuf)
241 (erase-buffer)
242 (insert-buffer-substring mailbuf)
243 (goto-char (point-max))
244 ;; require one newline at the end.
245 (or (= (preceding-char) ?\n)
246 (insert ?\n))
247 ;; Change header-delimiter to be what sendmail expects.
92a3f23d 248 (mail-sendmail-undelimit-header)
24975917 249 (setq delimline (point-marker))
e2f7c221 250;; (sendmail-synch-aliases)
24975917
RS
251 (if mail-aliases
252 (expand-mail-aliases (point-min) delimline))
253 (goto-char (point-min))
254 ;; ignore any blank lines in the header
255 (while (and (re-search-forward "\n\n\n*" delimline t)
256 (< (point) delimline))
257 (replace-match "\n"))
258 (let ((case-fold-search t))
5feeeae2
RS
259 ;; We used to process Resent-... headers here,
260 ;; but it was not done properly, and the job
261 ;; is done correctly in smtpmail-deduce-address-list.
24975917
RS
262 ;; Don't send out a blank subject line
263 (goto-char (point-min))
5feeeae2
RS
264 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
265 (replace-match "")
266 ;; This one matches a Subject just before the header delimiter.
267 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
268 (= (match-end 0) delimline))
269 (replace-match "")))
0e2701ca
RS
270 ;; Put the "From:" field in unless for some odd reason
271 ;; they put one in themselves.
272 (goto-char (point-min))
273 (if (not (re-search-forward "^From:" delimline t))
850ed7b3 274 (let* ((login smtpmail-mail-address)
0e2701ca
RS
275 (fullname (user-full-name)))
276 (cond ((eq mail-from-style 'angles)
277 (insert "From: " fullname)
278 (let ((fullname-start (+ (point-min) 6))
279 (fullname-end (point-marker)))
280 (goto-char fullname-start)
281 ;; Look for a character that cannot appear unquoted
282 ;; according to RFC 822.
283 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
284 fullname-end 1)
285 (progn
286 ;; Quote fullname, escaping specials.
287 (goto-char fullname-start)
288 (insert "\"")
289 (while (re-search-forward "[\"\\]"
290 fullname-end 1)
291 (replace-match "\\\\\\&" t))
292 (insert "\""))))
293 (insert " <" login ">\n"))
294 ((eq mail-from-style 'parens)
295 (insert "From: " login " (")
296 (let ((fullname-start (point)))
297 (insert fullname)
298 (let ((fullname-end (point-marker)))
299 (goto-char fullname-start)
300 ;; RFC 822 says \ and nonmatching parentheses
301 ;; must be escaped in comments.
302 ;; Escape every instance of ()\ ...
303 (while (re-search-forward "[()\\]" fullname-end 1)
304 (replace-match "\\\\\\&" t))
305 ;; ... then undo escaping of matching parentheses,
306 ;; including matching nested parentheses.
307 (goto-char fullname-start)
a8ba4429 308 (while (re-search-forward
0e2701ca
RS
309 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
310 fullname-end 1)
311 (replace-match "\\1(\\3)" t)
312 (goto-char fullname-start))))
313 (insert ")\n"))
314 ((null mail-from-style)
315 (insert "From: " login "\n")))))
21bf0d6c
SM
316 ;; Insert a `Message-Id:' field if there isn't one yet.
317 (goto-char (point-min))
318 (unless (re-search-forward "^Message-Id:" delimline t)
319 (insert "Message-Id: " (message-make-message-id) "\n"))
320 ;; Insert a `Date:' field if there isn't one yet.
321 (goto-char (point-min))
322 (unless (re-search-forward "^Date:" delimline t)
323 (insert "Date: " (message-make-date) "\n"))
24975917
RS
324 ;; Insert an extra newline if we need it to work around
325 ;; Sun's bug that swallows newlines.
326 (goto-char (1+ delimline))
327 (if (eval mail-mailer-swallows-blank-line)
328 (newline))
0e2701ca
RS
329 ;; Find and handle any FCC fields.
330 (goto-char (point-min))
331 (if (re-search-forward "^FCC:" delimline t)
332 (mail-do-fcc delimline))
24975917 333 (if mail-interactive
21bf0d6c 334 (with-current-buffer errbuf
24975917
RS
335 (erase-buffer))))
336 ;;
337 ;;
338 ;;
339 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
340 (setq smtpmail-recipient-address-list
5feeeae2 341 (smtpmail-deduce-address-list tembuf (point-min) delimline))
24975917 342 (kill-buffer smtpmail-address-buffer)
a8ba4429 343
24975917 344 (smtpmail-do-bcc delimline)
308bc5d8
RS
345 ; Send or queue
346 (if (not smtpmail-queue-mail)
347 (if (not (null smtpmail-recipient-address-list))
a8ba4429 348 (if (not (smtpmail-via-smtp
308bc5d8
RS
349 smtpmail-recipient-address-list tembuf))
350 (error "Sending failed; SMTP protocol error"))
351 (error "Sending failed; no recipients"))
bf6e31bc
SJ
352 (let* ((file-data
353 (expand-file-name
354 (format "%s_%i"
355 (format-time-string "%Y-%m-%d_%H:%M:%S")
356 (setq smtpmail-queue-counter
357 (1+ smtpmail-queue-counter)))
358 smtpmail-queue-dir))
359 (file-data (convert-standard-filename file-data))
360 (file-elisp (concat file-data ".el"))
308bc5d8
RS
361 (buffer-data (create-file-buffer file-data))
362 (buffer-elisp (create-file-buffer file-elisp))
363 (buffer-scratch "*queue-mail*"))
2fbc1118
JB
364 (unless (file-exists-p smtpmail-queue-dir)
365 (make-directory smtpmail-queue-dir t))
21bf0d6c 366 (with-current-buffer buffer-data
308bc5d8
RS
367 (erase-buffer)
368 (insert-buffer tembuf)
369 (write-file file-data)
370 (set-buffer buffer-elisp)
371 (erase-buffer)
372 (insert (concat
373 "(setq smtpmail-recipient-address-list '"
374 (prin1-to-string smtpmail-recipient-address-list)
a8ba4429 375 ")\n"))
308bc5d8
RS
376 (write-file file-elisp)
377 (set-buffer (generate-new-buffer buffer-scratch))
378 (insert (concat file-data "\n"))
a8ba4429
SS
379 (append-to-file (point-min)
380 (point-max)
308bc5d8
RS
381 smtpmail-queue-index)
382 )
383 (kill-buffer buffer-scratch)
384 (kill-buffer buffer-data)
385 (kill-buffer buffer-elisp))))
24975917
RS
386 (kill-buffer tembuf)
387 (if (bufferp errbuf)
388 (kill-buffer errbuf)))))
389
74f39ce9 390;;;###autoload
308bc5d8
RS
391(defun smtpmail-send-queued-mail ()
392 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
393 (interactive)
7e3fa9f5
EZ
394 (with-temp-buffer
395 ;;; Get index, get first mail, send it, update index, get second
396 ;;; mail, send it, etc...
397 (let ((file-msg ""))
398 (insert-file-contents smtpmail-queue-index)
308bc5d8
RS
399 (beginning-of-buffer)
400 (while (not (eobp))
21bf0d6c 401 (setq file-msg (buffer-substring (point) (line-end-position)))
308bc5d8 402 (load file-msg)
19a0baf7
EZ
403 ;; Insert the message literally: it is already encoded as per
404 ;; the MIME headers, and code conversions might guess the
405 ;; encoding wrongly.
7e3fa9f5
EZ
406 (with-temp-buffer
407 (let ((coding-system-for-read 'no-conversion))
408 (insert-file-contents file-msg))
6b61353c
KH
409 (let ((smtpmail-mail-address
410 (or (and mail-specify-envelope-from (mail-envelope-from))
411 user-mail-address)))
412 (if (not (null smtpmail-recipient-address-list))
413 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
414 (current-buffer)))
415 (error "Sending failed; SMTP protocol error"))
416 (error "Sending failed; no recipients"))))
308bc5d8
RS
417 (delete-file file-msg)
418 (delete-file (concat file-msg ".el"))
95754b9f 419 (delete-region (point-at-bol) (point-at-bol 2)))
7e3fa9f5 420 (write-region (point-min) (point-max) smtpmail-queue-index))))
24975917
RS
421
422;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
423
424(defun smtpmail-fqdn ()
425 (if smtpmail-local-domain
426 (concat (system-name) "." smtpmail-local-domain)
427 (system-name)))
428
9056f1c9
RS
429(defsubst smtpmail-cred-server (cred)
430 (nth 0 cred))
431
432(defsubst smtpmail-cred-port (cred)
433 (nth 1 cred))
434
435(defsubst smtpmail-cred-key (cred)
436 (nth 2 cred))
437
438(defsubst smtpmail-cred-user (cred)
439 (nth 2 cred))
440
441(defsubst smtpmail-cred-cert (cred)
442 (nth 3 cred))
443
444(defsubst smtpmail-cred-passwd (cred)
445 (nth 3 cred))
446
447(defun smtpmail-find-credentials (cred server port)
448 (catch 'done
449 (let ((l cred) el)
450 (while (setq el (pop l))
451 (when (and (equal server (smtpmail-cred-server el))
452 (equal port (smtpmail-cred-port el)))
453 (throw 'done el))))))
454
2b5c7e03
GM
455(defun smtpmail-maybe-append-domain (recipient)
456 (if (or (not smtpmail-sendto-domain)
457 (string-match "@" recipient))
458 recipient
459 (concat recipient "@" smtpmail-sendto-domain)))
460
9056f1c9
RS
461(defun smtpmail-intersection (list1 list2)
462 (let ((result nil))
463 (dolist (el2 list2)
464 (when (memq el2 list1)
465 (push el2 result)))
466 (nreverse result)))
467
468(defun smtpmail-open-stream (process-buffer host port)
469 (let ((cred (smtpmail-find-credentials
470 smtpmail-starttls-credentials host port)))
471 (if (null (and cred (condition-case ()
11c10f3b
SJ
472 (progn
473 (require 'starttls)
474 (call-process starttls-program))
9056f1c9
RS
475 (error nil))))
476 ;; The normal case.
477 (open-network-stream "SMTP" process-buffer host port)
478 (let* ((cred-key (smtpmail-cred-key cred))
479 (cred-cert (smtpmail-cred-cert cred))
480 (starttls-extra-args
481 (when (and (stringp cred-key) (stringp cred-cert)
482 (file-regular-p
483 (setq cred-key (expand-file-name cred-key)))
484 (file-regular-p
485 (setq cred-cert (expand-file-name cred-cert))))
486 (list "--key-file" cred-key "--cert-file" cred-cert))))
487 (starttls-open-stream "SMTP" process-buffer host port)))))
488
489(defun smtpmail-try-auth-methods (process supported-extensions host port)
490 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
491 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs)))
60ed3fa5
RS
492 (cred (if (stringp smtpmail-auth-credentials)
493 (let* ((netrc (netrc-parse smtpmail-auth-credentials))
6b61353c
KH
494 (port-name (format "%s" (or port "smtp")))
495 (hostentry (netrc-machine netrc host port-name
496 port-name)))
a8ba4429
SS
497 (when hostentry
498 (list host port
499 (netrc-get hostentry "login")
500 (netrc-get hostentry "password"))))
60ed3fa5
RS
501 (smtpmail-find-credentials
502 smtpmail-auth-credentials host port)))
9056f1c9
RS
503 (passwd (when cred
504 (or (smtpmail-cred-passwd cred)
505 (read-passwd
506 (format "SMTP password for %s:%s: "
507 (smtpmail-cred-server cred)
508 (smtpmail-cred-port cred))))))
509 ret)
6b61353c 510 (when (and cred mech)
9056f1c9
RS
511 (cond
512 ((eq mech 'cram-md5)
513 (smtpmail-send-command process (format "AUTH %s" mech))
514 (if (or (null (car (setq ret (smtpmail-read-response process))))
515 (not (integerp (car ret)))
516 (>= (car ret) 400))
517 (throw 'done nil))
518 (when (eq (car ret) 334)
519 (let* ((challenge (substring (cadr ret) 4))
520 (decoded (base64-decode-string challenge))
521 (hash (rfc2104-hash 'md5 64 16 passwd decoded))
522 (response (concat (smtpmail-cred-user cred) " " hash))
523 (encoded (base64-encode-string response)))
524 (smtpmail-send-command process (format "%s" encoded))
525 (if (or (null (car (setq ret (smtpmail-read-response process))))
526 (not (integerp (car ret)))
527 (>= (car ret) 400))
528 (throw 'done nil)))))
529 ((eq mech 'login)
530 (smtpmail-send-command process "AUTH LOGIN")
531 (if (or (null (car (setq ret (smtpmail-read-response process))))
532 (not (integerp (car ret)))
533 (>= (car ret) 400))
534 (throw 'done nil))
535 (smtpmail-send-command
536 process (base64-encode-string (smtpmail-cred-user cred)))
537 (if (or (null (car (setq ret (smtpmail-read-response process))))
538 (not (integerp (car ret)))
539 (>= (car ret) 400))
540 (throw 'done nil))
541 (smtpmail-send-command process (base64-encode-string passwd))
542 (if (or (null (car (setq ret (smtpmail-read-response process))))
543 (not (integerp (car ret)))
544 (>= (car ret) 400))
545 (throw 'done nil)))
546 (t
a8ba4429 547 (error "Mechanism %s not implemented" mech)))
9056f1c9 548 ;; Remember the password.
60ed3fa5
RS
549 (when (and (not (stringp smtpmail-auth-credentials))
550 (null (smtpmail-cred-passwd cred)))
9056f1c9
RS
551 (setcar (cdr (cdr (cdr cred))) passwd)))))
552
24975917
RS
553(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
554 (let ((process nil)
f38d3514
KH
555 (host (or smtpmail-smtp-server
556 (error "`smtpmail-smtp-server' not defined")))
8805249b 557 (port smtpmail-smtp-service)
6b61353c
KH
558 ;; smtpmail-mail-address should be set to the appropriate
559 ;; buffer-local value by the caller, but in case not:
560 (envelope-from (or smtpmail-mail-address
561 (and mail-specify-envelope-from
562 (mail-envelope-from))
563 user-mail-address))
24975917 564 response-code
8805249b 565 greeting
c50d5ce0
RS
566 process-buffer
567 (supported-extensions '()))
24975917
RS
568 (unwind-protect
569 (catch 'done
570 ;; get or create the trace buffer
571 (setq process-buffer
572 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
573
574 ;; clear the trace buffer of old output
21bf0d6c 575 (with-current-buffer process-buffer
24975917
RS
576 (erase-buffer))
577
578 ;; open the connection to the server
9056f1c9 579 (setq process (smtpmail-open-stream process-buffer host port))
24975917
RS
580 (and (null process) (throw 'done nil))
581
582 ;; set the send-filter
583 (set-process-filter process 'smtpmail-process-filter)
584
21bf0d6c 585 (with-current-buffer process-buffer
4b876894 586 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
24975917
RS
587 (make-local-variable 'smtpmail-read-point)
588 (setq smtpmail-read-point (point-min))
589
a8ba4429 590
24975917
RS
591 (if (or (null (car (setq greeting (smtpmail-read-response process))))
592 (not (integerp (car greeting)))
593 (>= (car greeting) 400))
594 (throw 'done nil)
595 )
596
9056f1c9
RS
597 (let ((do-ehlo t)
598 (do-starttls t))
599 (while do-ehlo
c50d5ce0
RS
600 ;; EHLO
601 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
24975917 602
9056f1c9
RS
603 (if (or (null (car (setq response-code
604 (smtpmail-read-response process))))
24975917
RS
605 (not (integerp (car response-code)))
606 (>= (car response-code) 400))
c50d5ce0
RS
607 (progn
608 ;; HELO
9056f1c9
RS
609 (smtpmail-send-command
610 process (format "HELO %s" (smtpmail-fqdn)))
c50d5ce0 611
9056f1c9
RS
612 (if (or (null (car (setq response-code
613 (smtpmail-read-response process))))
c50d5ce0
RS
614 (not (integerp (car response-code)))
615 (>= (car response-code) 400))
616 (throw 'done nil)))
9056f1c9
RS
617 (dolist (line (cdr (cdr response-code)))
618 (let ((name (mapcar (lambda (s) (intern (downcase s)))
619 (split-string (substring line 4) "[ ]"))))
620 (and (eq (length name) 1)
621 (setq name (car name)))
c50d5ce0 622 (and name
9056f1c9
RS
623 (cond ((memq (if (consp name) (car name) name)
624 '(verb xvrb 8bitmime onex xone
c50d5ce0 625 expn size dsn etrn
9056f1c9
RS
626 enhancedstatuscodes
627 help xusr
628 auth=login auth starttls))
c50d5ce0
RS
629 (setq supported-extensions
630 (cons name supported-extensions)))
1b62b062 631 (smtpmail-warn-about-unknown-extensions
9056f1c9
RS
632 (message "Unknown extension %s" name)))))))
633
634 (if (and do-starttls
635 (smtpmail-find-credentials smtpmail-starttls-credentials host port)
636 (member 'starttls supported-extensions)
f029e7b0 637 (numberp (process-id process)))
9056f1c9
RS
638 (progn
639 (smtpmail-send-command process (format "STARTTLS"))
640 (if (or (null (car (setq response-code (smtpmail-read-response process))))
641 (not (integerp (car response-code)))
642 (>= (car response-code) 400))
643 (throw 'done nil))
644 (starttls-negotiate process)
645 (setq do-starttls nil))
646 (setq do-ehlo nil))))
a8ba4429 647
9056f1c9 648 (smtpmail-try-auth-methods process supported-extensions host port)
c50d5ce0
RS
649
650 (if (or (member 'onex supported-extensions)
651 (member 'xone supported-extensions))
652 (progn
653 (smtpmail-send-command process (format "ONEX"))
654 (if (or (null (car (setq response-code (smtpmail-read-response process))))
655 (not (integerp (car response-code)))
656 (>= (car response-code) 400))
657 (throw 'done nil))))
658
9056f1c9 659 (if (and smtpmail-debug-verb
c50d5ce0
RS
660 (or (member 'verb supported-extensions)
661 (member 'xvrb supported-extensions)))
662 (progn
663 (smtpmail-send-command process (format "VERB"))
664 (if (or (null (car (setq response-code (smtpmail-read-response process))))
665 (not (integerp (car response-code)))
666 (>= (car response-code) 400))
667 (throw 'done nil))))
668
669 (if (member 'xusr supported-extensions)
670 (progn
671 (smtpmail-send-command process (format "XUSR"))
672 (if (or (null (car (setq response-code (smtpmail-read-response process))))
673 (not (integerp (car response-code)))
674 (>= (car response-code) 400))
675 (throw 'done nil))))
24975917 676
6b61353c 677 ;; MAIL FROM:<sender>
c50d5ce0 678 (let ((size-part
9056f1c9
RS
679 (if (or (member 'size supported-extensions)
680 (assoc 'size supported-extensions))
c50d5ce0 681 (format " SIZE=%d"
21bf0d6c 682 (with-current-buffer smtpmail-text-buffer
c50d5ce0
RS
683 ;; size estimate:
684 (+ (- (point-max) (point-min))
685 ;; Add one byte for each change-of-line
6b61353c
KH
686 ;; because of CR-LF representation:
687 (count-lines (point-min) (point-max)))))
c50d5ce0
RS
688 ""))
689 (body-part
690 (if (member '8bitmime supported-extensions)
691 ;; FIXME:
692 ;; Code should be added here that transforms
693 ;; the contents of the message buffer into
694 ;; something the receiving SMTP can handle.
695 ;; For a receiver that supports 8BITMIME, this
696 ;; may mean converting BINARY to BASE64, or
697 ;; adding Content-Transfer-Encoding and the
698 ;; other MIME headers. The code should also
699 ;; return an indication of what encoding the
700 ;; message buffer is now, i.e. ASCII or
701 ;; 8BITMIME.
702 (if nil
703 " BODY=8BITMIME"
704 "")
705 "")))
706; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
6b61353c
KH
707 (smtpmail-send-command process (format "MAIL FROM:<%s>%s%s"
708 envelope-from
c50d5ce0
RS
709 size-part
710 body-part))
a1506d29 711
c50d5ce0
RS
712 (if (or (null (car (setq response-code (smtpmail-read-response process))))
713 (not (integerp (car response-code)))
714 (>= (car response-code) 400))
715 (throw 'done nil)
716 ))
a8ba4429 717
6b61353c 718 ;; RCPT TO:<recipient>
8805249b
RS
719 (let ((n 0))
720 (while (not (null (nth n recipient)))
6b61353c 721 (smtpmail-send-command process (format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient))))
8805249b
RS
722 (setq n (1+ n))
723
c50d5ce0
RS
724 (setq response-code (smtpmail-read-response process))
725 (if (or (null (car response-code))
8805249b
RS
726 (not (integerp (car response-code)))
727 (>= (car response-code) 400))
728 (throw 'done nil)
729 )
730 ))
a8ba4429 731
24975917
RS
732 ;; DATA
733 (smtpmail-send-command process "DATA")
734
735 (if (or (null (car (setq response-code (smtpmail-read-response process))))
736 (not (integerp (car response-code)))
737 (>= (car response-code) 400))
738 (throw 'done nil)
739 )
740
741 ;; Mail contents
742 (smtpmail-send-data process smtpmail-text-buffer)
743
744 ;;DATA end "."
745 (smtpmail-send-command process ".")
746
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
753 ;;QUIT
754; (smtpmail-send-command process "QUIT")
755; (and (null (car (smtpmail-read-response process)))
756; (throw 'done nil))
757 t ))
758 (if process
21bf0d6c 759 (with-current-buffer (process-buffer process)
24975917
RS
760 (smtpmail-send-command process "QUIT")
761 (smtpmail-read-response process)
762
763; (if (or (null (car (setq response-code (smtpmail-read-response process))))
764; (not (integerp (car response-code)))
765; (>= (car response-code) 400))
766; (throw 'done nil)
767; )
1121afb4
SJ
768 (delete-process process)
769 (unless smtpmail-debug-info
770 (kill-buffer process-buffer)))))))
24975917
RS
771
772
24975917 773(defun smtpmail-process-filter (process output)
21bf0d6c 774 (with-current-buffer (process-buffer process)
24975917
RS
775 (goto-char (point-max))
776 (insert output)))
777
24975917
RS
778(defun smtpmail-read-response (process)
779 (let ((case-fold-search nil)
c50d5ce0 780 (response-strings nil)
24975917 781 (response-continue t)
c50d5ce0 782 (return-value '(nil ()))
24975917 783 match-end)
6b61353c
KH
784 (catch 'done
785 (while response-continue
786 (goto-char smtpmail-read-point)
787 (while (not (search-forward "\r\n" nil t))
788 (unless (memq (process-status process) '(open run))
789 (throw 'done nil))
790 (accept-process-output process)
791 (goto-char smtpmail-read-point))
792
793 (setq match-end (point))
794 (setq response-strings
795 (cons (buffer-substring smtpmail-read-point (- match-end 2))
796 response-strings))
797
798 (goto-char smtpmail-read-point)
799 (if (looking-at "[0-9]+ ")
800 (let ((begin (match-beginning 0))
801 (end (match-end 0)))
802 (if smtpmail-debug-info
803 (message "%s" (car response-strings)))
804
805 (setq smtpmail-read-point match-end)
806
807 ;; ignore lines that start with "0"
808 (if (looking-at "0[0-9]+ ")
809 nil
810 (setq response-continue nil)
811 (setq return-value
812 (cons (string-to-int
813 (buffer-substring begin end))
814 (nreverse response-strings)))))
815
816 (if (looking-at "[0-9]+-")
817 (progn (if smtpmail-debug-info
818 (message "%s" (car response-strings)))
819 (setq smtpmail-read-point match-end)
820 (setq response-continue t))
821 (progn
822 (setq smtpmail-read-point match-end)
c50d5ce0
RS
823 (setq response-continue nil)
824 (setq return-value
6b61353c
KH
825 (cons nil (nreverse response-strings)))))))
826 (setq smtpmail-read-point match-end))
24975917
RS
827 return-value))
828
829
24975917
RS
830(defun smtpmail-send-command (process command)
831 (goto-char (point-max))
832 (if (= (aref command 0) ?P)
833 (insert "PASS <omitted>\r\n")
834 (insert command "\r\n"))
835 (setq smtpmail-read-point (point))
836 (process-send-string process command)
837 (process-send-string process "\r\n"))
838
24975917
RS
839(defun smtpmail-send-data-1 (process data)
840 (goto-char (point-max))
841
83af570e
KH
842 (if (and (multibyte-string-p data)
843 smtpmail-code-conv-from)
844 (setq data (string-as-multibyte
845 (encode-coding-string data smtpmail-code-conv-from))))
a8ba4429 846
24975917
RS
847 (if smtpmail-debug-info
848 (insert data "\r\n"))
849
850 (setq smtpmail-read-point (point))
57810560
KH
851 ;; Escape "." at start of a line
852 (if (eq (string-to-char data) ?.)
24975917 853 (process-send-string process "."))
57810560 854 (process-send-string process data)
24975917
RS
855 (process-send-string process "\r\n")
856 )
857
858(defun smtpmail-send-data (process buffer)
6b61353c 859 (let ((data-continue t) sending-data)
21bf0d6c 860 (with-current-buffer buffer
24975917 861 (goto-char (point-min)))
24975917 862 (while data-continue
21bf0d6c 863 (with-current-buffer buffer
6b61353c
KH
864 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
865 (end-of-line 2)
866 (setq data-continue (not (eobp))))
867 (smtpmail-send-data-1 process sending-data))))
24975917
RS
868
869(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
870 "Get address list suitable for smtp RCPT TO: <address>."
1188dd37 871 (unwind-protect
21bf0d6c
SM
872 (with-current-buffer smtpmail-address-buffer
873 (erase-buffer)
1188dd37
AI
874 (let
875 ((case-fold-search t)
876 (simple-address-list "")
877 this-line
878 this-line-end
879 addr-regexp)
24975917
RS
880 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
881 (goto-char (point-min))
13f1d088
KH
882 ;; RESENT-* fields should stop processing of regular fields.
883 (save-excursion
21bf0d6c
SM
884 (setq addr-regexp
885 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
886 header-end t)
887 "^Resent-\\(to\\|cc\\|bcc\\):"
888 "^\\(To:\\|Cc:\\|Bcc:\\)")))
13f1d088
KH
889
890 (while (re-search-forward addr-regexp header-end t)
24975917
RS
891 (replace-match "")
892 (setq this-line (match-beginning 0))
893 (forward-line 1)
894 ;; get any continuation lines
895 (while (and (looking-at "^[ \t]+") (< (point) header-end))
896 (forward-line 1))
897 (setq this-line-end (point-marker))
898 (setq simple-address-list
899 (concat simple-address-list " "
900 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
901 )
902 (erase-buffer)
92dfd10c 903 (insert " " simple-address-list "\n")
24975917
RS
904 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
905 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
906 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
907
908 (goto-char (point-min))
909 ;; tidyness in case hook is not robust when it looks at this
910 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
911
912 (goto-char (point-min))
8805249b 913 (let (recipient-address-list)
e2f7c221 914 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
8805249b 915 (backward-char 1)
e2f7c221
RS
916 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
917 recipient-address-list))
8805249b
RS
918 )
919 (setq smtpmail-recipient-address-list recipient-address-list))
24975917
RS
920
921 )
1188dd37 922 )
24975917
RS
923 )
924 )
925
926
927(defun smtpmail-do-bcc (header-end)
5feeeae2 928 "Delete [Resent-]BCC: and their continuation lines from the header area.
24975917
RS
929There may be multiple BCC: lines, and each may have arbitrarily
930many continuation lines."
931 (let ((case-fold-search t))
067427f5
KH
932 (save-excursion
933 (goto-char (point-min))
934 ;; iterate over all BCC: lines
935 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
936 (delete-region (match-beginning 0)
937 (progn (forward-line 1) (point)))
938 ;; get rid of any continuation lines
939 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
940 (replace-match ""))))))
24975917
RS
941
942
943(provide 'smtpmail)
944
6b61353c 945;;; arch-tag: a76992df-6d71-43b7-9e72-4bacc6c05466
092af6d8 946;;; smtpmail.el ends here