Merge: Fixes for GLYPH_DEBUG found by GCC 4.6.0 static checking.
[bpt/emacs.git] / lisp / mail / smtpmail.el
1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2
3 ;; Copyright (C) 1995-1996, 2001-2011 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 3 of the License, or
19 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
28
29 ;;; Commentary:
30
31 ;; Send Mail to smtp host from smtpmail temp buffer.
32
33 ;; Please add these lines in your .emacs(_emacs) or use customize.
34 ;;
35 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
36 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
37 ;;(setq smtpmail-smtp-server "YOUR SMTP HOST")
38 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
39 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
40 ;;(setq smtpmail-debug-info t) ; only to debug problems
41
42 ;; To queue mail, set `smtpmail-queue-mail' to t and use
43 ;; `smtpmail-send-queued-mail' to send.
44
45 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
46 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
47 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
48 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
49 ;; support below.
50
51 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
52 ;; Authentication by the AUTH mechanism.
53 ;; See http://www.ietf.org/rfc/rfc2554.txt
54
55 ;;; Code:
56
57 (require 'sendmail)
58 (autoload 'mail-strip-quoted-names "mail-utils")
59 (autoload 'message-make-date "message")
60 (autoload 'message-make-message-id "message")
61 (autoload 'rfc2104-hash "rfc2104")
62 (autoload 'netrc-parse "netrc")
63 (autoload 'netrc-machine "netrc")
64 (autoload 'netrc-get "netrc")
65 (autoload 'password-read "password-cache")
66 (autoload 'auth-source-search "auth-source")
67
68 ;;;
69 (defgroup smtpmail nil
70 "SMTP protocol for sending mail."
71 :group 'mail)
72
73
74 (defvar smtpmail-default-smtp-server nil
75 "Specify default SMTP server.
76 This only has effect if you specify it before loading the smtpmail library.")
77
78 (defcustom smtpmail-smtp-server
79 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
80 "The name of the host running SMTP server."
81 :type '(choice (const nil) string)
82 :group 'smtpmail)
83
84 (defcustom smtpmail-smtp-service 25
85 "SMTP service port number.
86 The default value would be \"smtp\" or 25."
87 :type '(choice (integer :tag "Port") (string :tag "Service"))
88 :group 'smtpmail)
89
90 (defcustom smtpmail-local-domain nil
91 "Local domain name without a host name.
92 If the function `system-name' returns the full internet address,
93 don't define this value."
94 :type '(choice (const nil) string)
95 :group 'smtpmail)
96
97 (defcustom smtpmail-stream-type nil
98 "Connection type SMTP connections.
99 This may be either nil (plain connection) or `starttls' (use the
100 starttls mechanism to turn on TLS security after opening the
101 stream)."
102 :version "24.1"
103 :group 'smtpmail
104 :type '(choice (const :tag "Plain" nil)
105 (const starttls)))
106
107 (defcustom smtpmail-sendto-domain nil
108 "Local domain name without a host name.
109 This is appended (with an @-sign) to any specified recipients which do
110 not include an @-sign, so that each RCPT TO address is fully qualified.
111 \(Some configurations of sendmail require this.)
112
113 Don't bother to set this unless you have get an error like:
114 Sending failed; 501 <someone>: recipient address must contain a domain."
115 :type '(choice (const nil) string)
116 :group 'smtpmail)
117
118 (defcustom smtpmail-debug-info nil
119 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
120 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
121 be verbose as well."
122 :type 'boolean
123 :group 'smtpmail)
124
125 (defcustom smtpmail-debug-verb nil
126 "Whether this library sends the SMTP VERB command or not.
127 The commands enables verbose information from the SMTP server."
128 :type 'boolean
129 :group 'smtpmail)
130
131 (defcustom smtpmail-code-conv-from nil
132 "Coding system for encoding outgoing mail.
133 Used for the value of `sendmail-coding-system' when
134 `select-message-coding-system' is called. "
135 :type 'coding-system
136 :group 'smtpmail)
137
138 (defcustom smtpmail-queue-mail nil
139 "Non-nil means mail is queued; otherwise it is sent immediately.
140 If queued, it is stored in the directory `smtpmail-queue-dir'
141 and sent with `smtpmail-send-queued-mail'."
142 :type 'boolean
143 :group 'smtpmail)
144
145 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
146 "Directory where `smtpmail.el' stores queued mail."
147 :type 'directory
148 :group 'smtpmail)
149
150 (defcustom smtpmail-warn-about-unknown-extensions nil
151 "If set, print warnings about unknown SMTP extensions.
152 This is mainly useful for development purposes, to learn about
153 new SMTP extensions that might be useful to support."
154 :type 'boolean
155 :version "21.1"
156 :group 'smtpmail)
157
158 (defcustom smtpmail-queue-index-file "index"
159 "File name of queued mail index.
160 This is relative to `smtpmail-queue-dir'."
161 :type 'string
162 :group 'smtpmail)
163
164 ;; End of customizable variables.
165
166
167 (defvar smtpmail-address-buffer)
168 (defvar smtpmail-recipient-address-list)
169
170 (defvar smtpmail-queue-counter 0)
171
172 ;; Buffer-local variable.
173 (defvar smtpmail-read-point)
174
175 (defconst smtpmail-auth-supported '(cram-md5 plain login)
176 "List of supported SMTP AUTH mechanisms.
177 The list is in preference order.")
178
179 (defvar smtpmail-mail-address nil
180 "Value to use for envelope-from address for mail from ambient buffer.")
181
182 ;;;###autoload
183 (defun smtpmail-send-it ()
184 (let ((errbuf (if mail-interactive
185 (generate-new-buffer " smtpmail errors")
186 0))
187 (tembuf (generate-new-buffer " smtpmail temp"))
188 (case-fold-search nil)
189 delimline
190 result
191 (mailbuf (current-buffer))
192 ;; Examine this variable now, so that
193 ;; local binding in the mail buffer will take effect.
194 (smtpmail-mail-address
195 (or (and mail-specify-envelope-from (mail-envelope-from))
196 user-mail-address))
197 (smtpmail-code-conv-from
198 (if enable-multibyte-characters
199 (let ((sendmail-coding-system smtpmail-code-conv-from))
200 (select-message-coding-system)))))
201 (unwind-protect
202 (with-current-buffer tembuf
203 (erase-buffer)
204 ;; Use the same `buffer-file-coding-system' as in the mail
205 ;; buffer, otherwise any `write-region' invocations (e.g., in
206 ;; mail-do-fcc below) will annoy with asking for a suitable
207 ;; encoding.
208 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
209 (insert-buffer-substring mailbuf)
210 (goto-char (point-max))
211 ;; require one newline at the end.
212 (or (= (preceding-char) ?\n)
213 (insert ?\n))
214 ;; Change header-delimiter to be what sendmail expects.
215 (mail-sendmail-undelimit-header)
216 (setq delimline (point-marker))
217 ;; (sendmail-synch-aliases)
218 (if mail-aliases
219 (expand-mail-aliases (point-min) delimline))
220 (goto-char (point-min))
221 ;; ignore any blank lines in the header
222 (while (and (re-search-forward "\n\n\n*" delimline t)
223 (< (point) delimline))
224 (replace-match "\n"))
225 (let ((case-fold-search t))
226 ;; We used to process Resent-... headers here,
227 ;; but it was not done properly, and the job
228 ;; is done correctly in `smtpmail-deduce-address-list'.
229 ;; Don't send out a blank subject line
230 (goto-char (point-min))
231 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
232 (replace-match "")
233 ;; This one matches a Subject just before the header delimiter.
234 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
235 (= (match-end 0) delimline))
236 (replace-match "")))
237 ;; Put the "From:" field in unless for some odd reason
238 ;; they put one in themselves.
239 (goto-char (point-min))
240 (if (not (re-search-forward "^From:" delimline t))
241 (let* ((login smtpmail-mail-address)
242 (fullname (user-full-name)))
243 (cond ((eq mail-from-style 'angles)
244 (insert "From: " fullname)
245 (let ((fullname-start (+ (point-min) 6))
246 (fullname-end (point-marker)))
247 (goto-char fullname-start)
248 ;; Look for a character that cannot appear unquoted
249 ;; according to RFC 822.
250 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
251 fullname-end 1)
252 (progn
253 ;; Quote fullname, escaping specials.
254 (goto-char fullname-start)
255 (insert "\"")
256 (while (re-search-forward "[\"\\]"
257 fullname-end 1)
258 (replace-match "\\\\\\&" t))
259 (insert "\""))))
260 (insert " <" login ">\n"))
261 ((eq mail-from-style 'parens)
262 (insert "From: " login " (")
263 (let ((fullname-start (point)))
264 (insert fullname)
265 (let ((fullname-end (point-marker)))
266 (goto-char fullname-start)
267 ;; RFC 822 says \ and nonmatching parentheses
268 ;; must be escaped in comments.
269 ;; Escape every instance of ()\ ...
270 (while (re-search-forward "[()\\]" fullname-end 1)
271 (replace-match "\\\\\\&" t))
272 ;; ... then undo escaping of matching parentheses,
273 ;; including matching nested parentheses.
274 (goto-char fullname-start)
275 (while (re-search-forward
276 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
277 fullname-end 1)
278 (replace-match "\\1(\\3)" t)
279 (goto-char fullname-start))))
280 (insert ")\n"))
281 ((null mail-from-style)
282 (insert "From: " login "\n")))))
283 ;; Insert a `Message-Id:' field if there isn't one yet.
284 (goto-char (point-min))
285 (unless (re-search-forward "^Message-Id:" delimline t)
286 (insert "Message-Id: " (message-make-message-id) "\n"))
287 ;; Insert a `Date:' field if there isn't one yet.
288 (goto-char (point-min))
289 (unless (re-search-forward "^Date:" delimline t)
290 (insert "Date: " (message-make-date) "\n"))
291 ;; Possibly add a MIME header for the current coding system
292 (let (charset)
293 (goto-char (point-min))
294 (and (eq mail-send-nonascii 'mime)
295 (not (re-search-forward "^MIME-version:" delimline t))
296 (progn (skip-chars-forward "\0-\177")
297 (/= (point) (point-max)))
298 smtpmail-code-conv-from
299 (setq charset
300 (coding-system-get smtpmail-code-conv-from
301 'mime-charset))
302 (goto-char delimline)
303 (insert "MIME-version: 1.0\n"
304 "Content-type: text/plain; charset="
305 (symbol-name charset)
306 "\nContent-Transfer-Encoding: 8bit\n")))
307 ;; Insert an extra newline if we need it to work around
308 ;; Sun's bug that swallows newlines.
309 (goto-char (1+ delimline))
310 (if (eval mail-mailer-swallows-blank-line)
311 (newline))
312 ;; Find and handle any FCC fields.
313 (goto-char (point-min))
314 (if (re-search-forward "^FCC:" delimline t)
315 ;; Force `mail-do-fcc' to use the encoding of the mail
316 ;; buffer to encode outgoing messages on FCC files.
317 (let ((coding-system-for-write smtpmail-code-conv-from))
318 (mail-do-fcc delimline)))
319 (if mail-interactive
320 (with-current-buffer errbuf
321 (erase-buffer))))
322 ;; Encode the header according to RFC2047.
323 (mail-encode-header (point-min) delimline)
324 ;;
325 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
326 (setq smtpmail-recipient-address-list
327 (smtpmail-deduce-address-list tembuf (point-min) delimline))
328 (kill-buffer smtpmail-address-buffer)
329
330 (smtpmail-do-bcc delimline)
331 ;; Send or queue
332 (if (not smtpmail-queue-mail)
333 (if (not (null smtpmail-recipient-address-list))
334 (when (setq result
335 (smtpmail-via-smtp
336 smtpmail-recipient-address-list tembuf))
337 (error "Sending failed: %s" result))
338 (error "Sending failed; no recipients"))
339 (let* ((file-data
340 (expand-file-name
341 (format "%s_%i"
342 (format-time-string "%Y-%m-%d_%H:%M:%S")
343 (setq smtpmail-queue-counter
344 (1+ smtpmail-queue-counter)))
345 smtpmail-queue-dir))
346 (file-data (convert-standard-filename file-data))
347 (file-elisp (concat file-data ".el"))
348 (buffer-data (create-file-buffer file-data))
349 (buffer-elisp (create-file-buffer file-elisp))
350 (buffer-scratch "*queue-mail*"))
351 (unless (file-exists-p smtpmail-queue-dir)
352 (make-directory smtpmail-queue-dir t))
353 (with-current-buffer buffer-data
354 (erase-buffer)
355 (set-buffer-file-coding-system
356 ;; We will be reading the file with no-conversion in
357 ;; smtpmail-send-queued-mail below, so write it out
358 ;; with Unix EOLs.
359 (coding-system-change-eol-conversion
360 (or smtpmail-code-conv-from 'undecided)
361 'unix)
362 nil t)
363 (insert-buffer-substring tembuf)
364 (write-file file-data)
365 (set-buffer buffer-elisp)
366 (erase-buffer)
367 (insert (concat
368 "(setq smtpmail-recipient-address-list '"
369 (prin1-to-string smtpmail-recipient-address-list)
370 ")\n"))
371 (write-file file-elisp)
372 (set-buffer (generate-new-buffer buffer-scratch))
373 (insert (concat file-data "\n"))
374 (append-to-file (point-min)
375 (point-max)
376 (expand-file-name smtpmail-queue-index-file
377 smtpmail-queue-dir)))
378 (kill-buffer buffer-scratch)
379 (kill-buffer buffer-data)
380 (kill-buffer buffer-elisp))))
381 (kill-buffer tembuf)
382 (if (bufferp errbuf)
383 (kill-buffer errbuf)))))
384
385 ;;;###autoload
386 (defun smtpmail-send-queued-mail ()
387 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
388 (interactive)
389 (with-temp-buffer
390 ;; Get index, get first mail, send it, update index, get second
391 ;; mail, send it, etc...
392 (let ((file-msg "")
393 (qfile (expand-file-name smtpmail-queue-index-file
394 smtpmail-queue-dir))
395 result)
396 (insert-file-contents qfile)
397 (goto-char (point-min))
398 (while (not (eobp))
399 (setq file-msg (buffer-substring (point) (line-end-position)))
400 (load file-msg)
401 ;; Insert the message literally: it is already encoded as per
402 ;; the MIME headers, and code conversions might guess the
403 ;; encoding wrongly.
404 (with-temp-buffer
405 (let ((coding-system-for-read 'no-conversion))
406 (insert-file-contents file-msg))
407 (let ((smtpmail-mail-address
408 (or (and mail-specify-envelope-from (mail-envelope-from))
409 user-mail-address)))
410 (if (not (null smtpmail-recipient-address-list))
411 (when (setq result (smtpmail-via-smtp
412 smtpmail-recipient-address-list
413 (current-buffer)))
414 (error "Sending failed: %s" result))
415 (error "Sending failed; no recipients"))))
416 (delete-file file-msg)
417 (delete-file (concat file-msg ".el"))
418 (delete-region (point-at-bol) (point-at-bol 2)))
419 (write-region (point-min) (point-max) qfile))))
420
421 (defun smtpmail-fqdn ()
422 (if smtpmail-local-domain
423 (concat (system-name) "." smtpmail-local-domain)
424 (system-name)))
425
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
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
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 ;; `password-read' autoloads password-cache.
466 (declare-function password-cache-add "password-cache" (key password))
467
468 (defun smtpmail-command-or-throw (process string &optional code)
469 (let (ret)
470 (smtpmail-send-command process string)
471 (unless (smtpmail-ok-p (setq ret (smtpmail-read-response process))
472 code)
473 (throw 'done (format "%s in response to %s"
474 (smtpmail-response-text ret)
475 string)))
476 ret))
477
478 (defun smtpmail-try-auth-methods (process supported-extensions host port
479 &optional ask-for-password)
480 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
481 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported)))
482 (auth-source-creation-prompts
483 '((user . "SMTP user at %h: ")
484 (secret . "SMTP password for %u@%h: ")))
485 (auth-info (car
486 (auth-source-search
487 :max 1
488 :host host
489 :port (if port
490 (format "%s" port)
491 "smtp")
492 :require (and ask-for-password
493 '(:user :secret))
494 :create ask-for-password)))
495 (user (plist-get auth-info :user))
496 (password (plist-get auth-info :secret))
497 (save-function (and ask-for-password
498 (plist-get auth-info :save-function)))
499 ret)
500 (when (functionp password)
501 (setq password (funcall password)))
502 (cond
503 ((or (not mech)
504 (not user)
505 (not password))
506 ;; No mechanism, or no credentials.
507 mech)
508 ((eq mech 'cram-md5)
509 (setq ret (smtpmail-command-or-throw process "AUTH CRAM-MD5"))
510 (when (eq (car ret) 334)
511 (let* ((challenge (substring (cadr ret) 4))
512 (decoded (base64-decode-string challenge))
513 (hash (rfc2104-hash 'md5 64 16 password decoded))
514 (response (concat user " " hash))
515 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
516 ;; SMTP auth fails because the SMTP server identifies
517 ;; only the first part of the string (delimited by
518 ;; new line characters) as a response from the
519 ;; client, and the rest as distinct commands.
520
521 ;; In my case, the response string is 80 characters
522 ;; long. Without the no-line-break option for
523 ;; `base64-encode-string', only the first 76 characters
524 ;; are taken as a response to the server, and the
525 ;; authentication fails.
526 (encoded (base64-encode-string response t)))
527 (smtpmail-command-or-throw process encoded)
528 (when save-function
529 (funcall save-function)))))
530 ((eq mech 'login)
531 (smtpmail-command-or-throw process "AUTH LOGIN")
532 (smtpmail-command-or-throw
533 process (base64-encode-string user t))
534 (smtpmail-command-or-throw process (base64-encode-string password t))
535 (when save-function
536 (funcall save-function)))
537 ((eq mech 'plain)
538 ;; We used to send an empty initial request, and wait for an
539 ;; empty response, and then send the password, but this
540 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
541 ;; is not sent if the server did not advertise AUTH PLAIN in
542 ;; the EHLO response. See RFC 2554 for more info.
543 (smtpmail-command-or-throw
544 process
545 (concat "AUTH PLAIN "
546 (base64-encode-string (concat "\0" user "\0" password) t))
547 235)
548 (when save-function
549 (funcall save-function)))
550 (t
551 (error "Mechanism %s not implemented" mech)))))
552
553 (defun smtpmail-response-code (string)
554 (when string
555 (with-temp-buffer
556 (insert string)
557 (goto-char (point-min))
558 (and (re-search-forward "^\\([0-9]+\\) " nil t)
559 (string-to-number (match-string 1))))))
560
561 (defun smtpmail-ok-p (response &optional code)
562 (and (car response)
563 (integerp (car response))
564 (< (car response) 400)
565 (or (null code)
566 (= code (car response)))))
567
568 (defun smtpmail-response-text (response)
569 (mapconcat 'identity (cdr response) "\n"))
570
571 (defun smtpmail-query-smtp-server ()
572 (let ((server (read-string "Outgoing SMTP mail server: "))
573 (ports '(587 "smtp"))
574 stream port)
575 (when (and smtpmail-smtp-server
576 (not (member smtpmail-smtp-server ports)))
577 (push smtpmail-smtp-server ports))
578 (while (and (not smtpmail-smtp-server)
579 (setq port (pop ports)))
580 (when (setq stream (ignore-errors
581 (open-network-stream "smtp" nil server port)))
582 (customize-save-variable 'smtpmail-smtp-server server)
583 (customize-save-variable 'smtpmail-smtp-service port)
584 (delete-process stream)))
585 (unless smtpmail-smtp-server
586 (error "Couldn't contact an SMTP server"))))
587
588 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
589 &optional ask-for-password)
590 (unless smtpmail-smtp-server
591 (smtpmail-query-smtp-server))
592 (let ((process nil)
593 (host (or smtpmail-smtp-server
594 (error "`smtpmail-smtp-server' not defined")))
595 (port smtpmail-smtp-service)
596 ;; `smtpmail-mail-address' should be set to the appropriate
597 ;; buffer-local value by the caller, but in case not:
598 (envelope-from (or smtpmail-mail-address
599 (and mail-specify-envelope-from
600 (mail-envelope-from))
601 user-mail-address))
602 response-code
603 process-buffer
604 result
605 auth-mechanisms
606 (supported-extensions '()))
607 (unwind-protect
608 (catch 'done
609 ;; get or create the trace buffer
610 (setq process-buffer
611 (get-buffer-create
612 (format "*trace of SMTP session to %s*" host)))
613
614 ;; clear the trace buffer of old output
615 (with-current-buffer process-buffer
616 (setq buffer-undo-list t)
617 (erase-buffer))
618
619 ;; open the connection to the server
620 (setq result
621 (open-network-stream
622 "smtpmail" process-buffer host port
623 :type smtpmail-stream-type
624 :return-list t
625 :capability-command (format "EHLO %s\r\n" (smtpmail-fqdn))
626 :end-of-command "^[0-9]+ .*\r\n"
627 :success "^2.*\n"
628 :always-query-capabilities t
629 :starttls-function
630 (lambda (capabilities)
631 (and (string-match "-STARTTLS" capabilities)
632 "STARTTLS\r\n"))
633 :client-certificate t
634 :use-starttls-if-possible t))
635
636 ;; If we couldn't access the server at all, we give up.
637 (unless (setq process (car result))
638 (throw 'done "Unable to contact server"))
639
640 ;; set the send-filter
641 (set-process-filter process 'smtpmail-process-filter)
642
643 (let* ((greeting (plist-get (cdr result) :greeting))
644 (code (smtpmail-response-code greeting)))
645 (unless code
646 (throw 'done (format "No greeting: %s" greeting)))
647 (when (>= code 400)
648 (throw 'done (format "Connection not allowed: %s" greeting))))
649
650 (with-current-buffer process-buffer
651 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
652 (make-local-variable 'smtpmail-read-point)
653 (setq smtpmail-read-point (point-min))
654
655 (let* ((capabilities (plist-get (cdr result) :capabilities))
656 (code (smtpmail-response-code capabilities)))
657 (if (or (null code)
658 (>= code 400))
659 ;; The server didn't accept EHLO, so we fall back on HELO.
660 (smtpmail-command-or-throw
661 process (format "HELO %s" (smtpmail-fqdn)))
662 ;; EHLO was successful, so we parse the extensions.
663 (dolist (line (delete
664 ""
665 (split-string
666 (plist-get (cdr result) :capabilities)
667 "\r\n")))
668 (let ((name
669 (with-case-table ascii-case-table
670 (mapcar (lambda (s) (intern (downcase s)))
671 (split-string (substring line 4) "[ ]")))))
672 (when (= (length name) 1)
673 (setq name (car name)))
674 (when name
675 (cond ((memq (if (consp name) (car name) name)
676 '(verb xvrb 8bitmime onex xone
677 expn size dsn etrn
678 enhancedstatuscodes
679 help xusr
680 auth=login auth starttls))
681 (setq supported-extensions
682 (cons name supported-extensions)))
683 (smtpmail-warn-about-unknown-extensions
684 (message "Unknown extension %s" name))))))))
685
686 (setq auth-mechanisms
687 (smtpmail-try-auth-methods
688 process supported-extensions host port
689 ask-for-password))
690
691 (when (or (member 'onex supported-extensions)
692 (member 'xone supported-extensions))
693 (smtpmail-command-or-throw process (format "ONEX")))
694
695 (when (and smtpmail-debug-verb
696 (or (member 'verb supported-extensions)
697 (member 'xvrb supported-extensions)))
698 (smtpmail-command-or-throw process (format "VERB")))
699
700 (when (member 'xusr supported-extensions)
701 (smtpmail-command-or-throw process (format "XUSR")))
702
703 ;; MAIL FROM:<sender>
704 (let ((size-part
705 (if (or (member 'size supported-extensions)
706 (assoc 'size supported-extensions))
707 (format " SIZE=%d"
708 (with-current-buffer smtpmail-text-buffer
709 ;; size estimate:
710 (+ (- (point-max) (point-min))
711 ;; Add one byte for each change-of-line
712 ;; because of CR-LF representation:
713 (count-lines (point-min) (point-max)))))
714 ""))
715 (body-part
716 (if (member '8bitmime supported-extensions)
717 ;; FIXME:
718 ;; Code should be added here that transforms
719 ;; the contents of the message buffer into
720 ;; something the receiving SMTP can handle.
721 ;; For a receiver that supports 8BITMIME, this
722 ;; may mean converting BINARY to BASE64, or
723 ;; adding Content-Transfer-Encoding and the
724 ;; other MIME headers. The code should also
725 ;; return an indication of what encoding the
726 ;; message buffer is now, i.e. ASCII or
727 ;; 8BITMIME.
728 (if nil
729 " BODY=8BITMIME"
730 "")
731 "")))
732 (smtpmail-send-command
733 process (format "MAIL FROM:<%s>%s%s"
734 envelope-from size-part body-part))
735 (cond
736 ((smtpmail-ok-p (setq result (smtpmail-read-response process)))
737 ;; Success.
738 )
739 ((and auth-mechanisms
740 (not ask-for-password)
741 (= (car result) 530))
742 ;; We got a "530 auth required", so we close and try
743 ;; again, this time asking the user for a password.
744 (smtpmail-send-command process "QUIT")
745 (smtpmail-read-response process)
746 (delete-process process)
747 (setq process nil)
748 (throw 'done
749 (smtpmail-via-smtp recipient smtpmail-text-buffer t)))
750 (t
751 ;; Return the error code.
752 (throw 'done
753 (smtpmail-response-text result)))))
754
755 ;; RCPT TO:<recipient>
756 (let ((n 0))
757 (while (not (null (nth n recipient)))
758 (smtpmail-send-command
759 process (format "RCPT TO:<%s>"
760 (smtpmail-maybe-append-domain
761 (nth n recipient))))
762 (cond
763 ((smtpmail-ok-p (setq result (smtpmail-read-response process)))
764 ;; Success.
765 nil)
766 ((and auth-mechanisms
767 (not ask-for-password)
768 (= (car result) 550))
769 ;; We got a "550 relay not permitted", and the server
770 ;; accepts credentials, so we try again, but ask for a
771 ;; password first.
772 (smtpmail-send-command process "QUIT")
773 (smtpmail-read-response process)
774 (delete-process process)
775 (setq process nil)
776 (throw 'done
777 (smtpmail-via-smtp recipient smtpmail-text-buffer t)))
778 (t
779 ;; Return the error code.
780 (throw 'done
781 (smtpmail-response-text result))))
782 (setq n (1+ n))))
783
784 ;; Send the contents.
785 (smtpmail-command-or-throw process "DATA")
786 (smtpmail-send-data process smtpmail-text-buffer)
787 ;; DATA end "."
788 (smtpmail-command-or-throw process ".")
789 ;; Return success.
790 nil))
791 (when (and process
792 (buffer-live-p process-buffer))
793 (with-current-buffer (process-buffer process)
794 (smtpmail-send-command process "QUIT")
795 (smtpmail-read-response process)
796 (delete-process process)
797 (unless smtpmail-debug-info
798 (kill-buffer process-buffer)))))))
799
800
801 (defun smtpmail-process-filter (process output)
802 (with-current-buffer (process-buffer process)
803 (goto-char (point-max))
804 (insert output)))
805
806 (defun smtpmail-read-response (process)
807 (let ((case-fold-search nil)
808 (response-strings nil)
809 (response-continue t)
810 (return-value '(nil ()))
811 match-end)
812 (catch 'done
813 (while response-continue
814 (goto-char smtpmail-read-point)
815 (while (not (search-forward "\r\n" nil t))
816 (unless (memq (process-status process) '(open run))
817 (throw 'done nil))
818 (accept-process-output process)
819 (goto-char smtpmail-read-point))
820
821 (setq match-end (point))
822 (setq response-strings
823 (cons (buffer-substring smtpmail-read-point (- match-end 2))
824 response-strings))
825
826 (goto-char smtpmail-read-point)
827 (if (looking-at "[0-9]+ ")
828 (let ((begin (match-beginning 0))
829 (end (match-end 0)))
830 (if smtpmail-debug-info
831 (message "%s" (car response-strings)))
832
833 (setq smtpmail-read-point match-end)
834
835 ;; ignore lines that start with "0"
836 (if (looking-at "0[0-9]+ ")
837 nil
838 (setq response-continue nil)
839 (setq return-value
840 (cons (string-to-number
841 (buffer-substring begin end))
842 (nreverse response-strings)))))
843
844 (if (looking-at "[0-9]+-")
845 (progn (if smtpmail-debug-info
846 (message "%s" (car response-strings)))
847 (setq smtpmail-read-point match-end)
848 (setq response-continue t))
849 (progn
850 (setq smtpmail-read-point match-end)
851 (setq response-continue nil)
852 (setq return-value
853 (cons nil (nreverse response-strings)))))))
854 (setq smtpmail-read-point match-end))
855 return-value))
856
857
858 (defun smtpmail-send-command (process command)
859 (goto-char (point-max))
860 (if (= (aref command 0) ?P)
861 (insert "PASS <omitted>\r\n")
862 (insert command "\r\n"))
863 (setq smtpmail-read-point (point))
864 (process-send-string process command)
865 (process-send-string process "\r\n"))
866
867 (defun smtpmail-send-data-1 (process data)
868 (goto-char (point-max))
869
870 (if (and (multibyte-string-p data)
871 smtpmail-code-conv-from)
872 (setq data (string-as-multibyte
873 (encode-coding-string data smtpmail-code-conv-from))))
874
875 (if smtpmail-debug-info
876 (insert data "\r\n"))
877
878 (setq smtpmail-read-point (point))
879 ;; Escape "." at start of a line
880 (if (eq (string-to-char data) ?.)
881 (process-send-string process "."))
882 (process-send-string process data)
883 (process-send-string process "\r\n"))
884
885 (defun smtpmail-send-data (process buffer)
886 (let ((data-continue t) sending-data
887 (pr (with-current-buffer buffer
888 (make-progress-reporter "Sending email"
889 (point-min) (point-max)))))
890 (with-current-buffer buffer
891 (goto-char (point-min)))
892 (while data-continue
893 (with-current-buffer buffer
894 (progress-reporter-update pr (point))
895 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
896 (end-of-line 2)
897 (setq data-continue (not (eobp))))
898 (smtpmail-send-data-1 process sending-data))
899 (progress-reporter-done pr)))
900
901 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
902 "Get address list suitable for smtp RCPT TO: <address>."
903 (unwind-protect
904 (with-current-buffer smtpmail-address-buffer
905 (erase-buffer)
906 (let ((case-fold-search t)
907 (simple-address-list "")
908 this-line
909 this-line-end
910 addr-regexp)
911 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
912 (goto-char (point-min))
913 ;; RESENT-* fields should stop processing of regular fields.
914 (save-excursion
915 (setq addr-regexp
916 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
917 header-end t)
918 "^Resent-\\(to\\|cc\\|bcc\\):"
919 "^\\(To:\\|Cc:\\|Bcc:\\)")))
920
921 (while (re-search-forward addr-regexp header-end t)
922 (replace-match "")
923 (setq this-line (match-beginning 0))
924 (forward-line 1)
925 ;; get any continuation lines
926 (while (and (looking-at "^[ \t]+") (< (point) header-end))
927 (forward-line 1))
928 (setq this-line-end (point-marker))
929 (setq simple-address-list
930 (concat simple-address-list " "
931 (mail-strip-quoted-names (buffer-substring this-line this-line-end)))))
932 (erase-buffer)
933 (insert " " simple-address-list "\n")
934 (subst-char-in-region (point-min) (point-max) 10 ? t) ; newline --> blank
935 (subst-char-in-region (point-min) (point-max) ?, ? t) ; comma --> blank
936 (subst-char-in-region (point-min) (point-max) 9 ? t) ; tab --> blank
937
938 (goto-char (point-min))
939 ;; tidyness in case hook is not robust when it looks at this
940 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
941
942 (goto-char (point-min))
943 (let (recipient-address-list)
944 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
945 (backward-char 1)
946 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
947 recipient-address-list)))
948 (setq smtpmail-recipient-address-list recipient-address-list))))))
949
950 (defun smtpmail-do-bcc (header-end)
951 "Delete [Resent-]BCC: and their continuation lines from the header area.
952 There may be multiple BCC: lines, and each may have arbitrarily
953 many continuation lines."
954 (let ((case-fold-search t))
955 (save-excursion
956 (goto-char (point-min))
957 ;; iterate over all BCC: lines
958 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
959 (delete-region (match-beginning 0)
960 (progn (forward-line 1) (point)))
961 ;; get rid of any continuation lines
962 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
963 (replace-match ""))))))
964
965 (provide 'smtpmail)
966
967 ;;; smtpmail.el ends here