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