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