1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995-1996, 2001-2011 Free Software Foundation, Inc.
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>
14 ;; This file is part of GNU Emacs.
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.
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.
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/>.
31 ;; Send Mail to smtp host from smtpmail temp buffer.
33 ;; Please add these lines in your .emacs(_emacs) or use customize.
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
42 ;; To queue mail, set `smtpmail-queue-mail' to t and use
43 ;; `smtpmail-send-queued-mail' to send.
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
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
58 (require 'auth-source)
59 (autoload 'mail-strip-quoted-names "mail-utils")
60 (autoload 'message-make-date "message")
61 (autoload 'message-make-message-id "message")
62 (autoload 'rfc2104-hash "rfc2104")
63 (autoload 'password-read "password-cache")
66 (defgroup smtpmail nil
67 "SMTP protocol for sending mail."
71 (defcustom smtpmail-default-smtp-server nil
72 "Specify default SMTP server.
73 This only has effect if you specify it before loading the smtpmail library."
74 :type '(choice (const nil) string)
77 (defcustom smtpmail-smtp-server
78 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
79 "The name of the host running SMTP server."
80 :type '(choice (const nil) string)
83 (defcustom smtpmail-smtp-service 25
84 "SMTP service port number.
85 The default value would be \"smtp\" or 25."
86 :type '(choice (integer :tag "Port") (string :tag "Service"))
89 (defcustom smtpmail-smtp-user nil
90 "User name to use when looking up credentials."
92 :type '(choice (const nil) string)
95 (defcustom smtpmail-local-domain nil
96 "Local domain name without a host name.
97 If the function `system-name' returns the full internet address,
98 don't define this value."
99 :type '(choice (const nil) string)
102 (defcustom smtpmail-stream-type nil
103 "Connection type SMTP connections.
104 This may be either nil (possibly upgraded to STARTTLS if
105 possible), or `starttls' (refuse to send if STARTTLS isn't
106 available), or `plain' (never use STARTTLS).."
109 :type '(choice (const :tag "Possibly upgrade to STARTTLS" nil)
110 (const :tag "Always use STARTTLS" starttls)
111 (const :tag "Never use STARTTLS" plain)))
113 (defcustom smtpmail-sendto-domain nil
114 "Local domain name without a host name.
115 This is appended (with an @-sign) to any specified recipients which do
116 not include an @-sign, so that each RCPT TO address is fully qualified.
117 \(Some configurations of sendmail require this.)
119 Don't bother to set this unless you have get an error like:
120 Sending failed; 501 <someone>: recipient address must contain a domain."
121 :type '(choice (const nil) string)
124 (defcustom smtpmail-debug-info nil
125 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
126 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
131 (defcustom smtpmail-debug-verb nil
132 "Whether this library sends the SMTP VERB command or not.
133 The commands enables verbose information from the SMTP server."
137 (defcustom smtpmail-code-conv-from nil
138 "Coding system for encoding outgoing mail.
139 Used for the value of `sendmail-coding-system' when
140 `select-message-coding-system' is called. "
144 (defcustom smtpmail-queue-mail nil
145 "Non-nil means mail is queued; otherwise it is sent immediately.
146 If queued, it is stored in the directory `smtpmail-queue-dir'
147 and sent with `smtpmail-send-queued-mail'."
151 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
152 "Directory where `smtpmail.el' stores queued mail."
156 (defcustom smtpmail-warn-about-unknown-extensions nil
157 "If set, print warnings about unknown SMTP extensions.
158 This is mainly useful for development purposes, to learn about
159 new SMTP extensions that might be useful to support."
164 (defcustom smtpmail-queue-index-file "index"
165 "File name of queued mail index.
166 This is relative to `smtpmail-queue-dir'."
170 ;; End of customizable variables.
173 (defvar smtpmail-address-buffer)
174 (defvar smtpmail-recipient-address-list)
176 (defvar smtpmail-queue-counter 0)
178 ;; Buffer-local variable.
179 (defvar smtpmail-read-point)
181 (defconst smtpmail-auth-supported '(cram-md5 plain login)
182 "List of supported SMTP AUTH mechanisms.
183 The list is in preference order.")
185 (defvar smtpmail-mail-address nil
186 "Value to use for envelope-from address for mail from ambient buffer.")
189 (defun smtpmail-send-it ()
190 (let ((errbuf (if mail-interactive
191 (generate-new-buffer " smtpmail errors")
193 (tembuf (generate-new-buffer " smtpmail temp"))
194 (case-fold-search nil)
197 (mailbuf (current-buffer))
198 ;; Examine this variable now, so that
199 ;; local binding in the mail buffer will take effect.
200 (smtpmail-mail-address
201 (or (and mail-specify-envelope-from (mail-envelope-from))
203 (smtpmail-code-conv-from
204 (if enable-multibyte-characters
205 (let ((sendmail-coding-system smtpmail-code-conv-from))
206 (select-message-coding-system)))))
208 (with-current-buffer tembuf
210 ;; Use the same `buffer-file-coding-system' as in the mail
211 ;; buffer, otherwise any `write-region' invocations (e.g., in
212 ;; mail-do-fcc below) will annoy with asking for a suitable
214 (set-buffer-file-coding-system smtpmail-code-conv-from nil t)
215 (insert-buffer-substring mailbuf)
216 (goto-char (point-max))
217 ;; require one newline at the end.
218 (or (= (preceding-char) ?\n)
220 ;; Change header-delimiter to be what sendmail expects.
221 (mail-sendmail-undelimit-header)
222 (setq delimline (point-marker))
223 ;; (sendmail-synch-aliases)
225 (expand-mail-aliases (point-min) delimline))
226 (goto-char (point-min))
227 ;; ignore any blank lines in the header
228 (while (and (re-search-forward "\n\n\n*" delimline t)
229 (< (point) delimline))
230 (replace-match "\n"))
231 (let ((case-fold-search t))
232 ;; We used to process Resent-... headers here,
233 ;; but it was not done properly, and the job
234 ;; is done correctly in `smtpmail-deduce-address-list'.
235 ;; Don't send out a blank subject line
236 (goto-char (point-min))
237 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
239 ;; This one matches a Subject just before the header delimiter.
240 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
241 (= (match-end 0) delimline))
243 ;; Put the "From:" field in unless for some odd reason
244 ;; they put one in themselves.
245 (goto-char (point-min))
246 (if (not (re-search-forward "^From:" delimline t))
247 (let* ((login smtpmail-mail-address)
248 (fullname (user-full-name)))
249 (cond ((eq mail-from-style 'angles)
250 (insert "From: " fullname)
251 (let ((fullname-start (+ (point-min) 6))
252 (fullname-end (point-marker)))
253 (goto-char fullname-start)
254 ;; Look for a character that cannot appear unquoted
255 ;; according to RFC 822.
256 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
259 ;; Quote fullname, escaping specials.
260 (goto-char fullname-start)
262 (while (re-search-forward "[\"\\]"
264 (replace-match "\\\\\\&" t))
266 (insert " <" login ">\n"))
267 ((eq mail-from-style 'parens)
268 (insert "From: " login " (")
269 (let ((fullname-start (point)))
271 (let ((fullname-end (point-marker)))
272 (goto-char fullname-start)
273 ;; RFC 822 says \ and nonmatching parentheses
274 ;; must be escaped in comments.
275 ;; Escape every instance of ()\ ...
276 (while (re-search-forward "[()\\]" fullname-end 1)
277 (replace-match "\\\\\\&" t))
278 ;; ... then undo escaping of matching parentheses,
279 ;; including matching nested parentheses.
280 (goto-char fullname-start)
281 (while (re-search-forward
282 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
284 (replace-match "\\1(\\3)" t)
285 (goto-char fullname-start))))
287 ((null mail-from-style)
288 (insert "From: " login "\n")))))
289 ;; Insert a `Message-Id:' field if there isn't one yet.
290 (goto-char (point-min))
291 (unless (re-search-forward "^Message-Id:" delimline t)
292 (insert "Message-Id: " (message-make-message-id) "\n"))
293 ;; Insert a `Date:' field if there isn't one yet.
294 (goto-char (point-min))
295 (unless (re-search-forward "^Date:" delimline t)
296 (insert "Date: " (message-make-date) "\n"))
297 ;; Possibly add a MIME header for the current coding system
299 (goto-char (point-min))
300 (and (eq mail-send-nonascii 'mime)
301 (not (re-search-forward "^MIME-version:" delimline t))
302 (progn (skip-chars-forward "\0-\177")
303 (/= (point) (point-max)))
304 smtpmail-code-conv-from
306 (coding-system-get smtpmail-code-conv-from
308 (goto-char delimline)
309 (insert "MIME-version: 1.0\n"
310 "Content-type: text/plain; charset="
311 (symbol-name charset)
312 "\nContent-Transfer-Encoding: 8bit\n")))
313 ;; Insert an extra newline if we need it to work around
314 ;; Sun's bug that swallows newlines.
315 (goto-char (1+ delimline))
316 (if (eval mail-mailer-swallows-blank-line)
318 ;; Find and handle any FCC fields.
319 (goto-char (point-min))
320 (if (re-search-forward "^FCC:" delimline t)
321 ;; Force `mail-do-fcc' to use the encoding of the mail
322 ;; buffer to encode outgoing messages on FCC files.
323 (let ((coding-system-for-write smtpmail-code-conv-from))
324 (mail-do-fcc delimline)))
326 (with-current-buffer errbuf
328 ;; Encode the header according to RFC2047.
329 (mail-encode-header (point-min) delimline)
331 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
332 (setq smtpmail-recipient-address-list
333 (smtpmail-deduce-address-list tembuf (point-min) delimline))
334 (kill-buffer smtpmail-address-buffer)
336 (smtpmail-do-bcc delimline)
338 (if (not smtpmail-queue-mail)
339 (if (not (null smtpmail-recipient-address-list))
342 smtpmail-recipient-address-list tembuf))
343 (error "Sending failed: %s" result))
344 (error "Sending failed; no recipients"))
348 (format-time-string "%Y-%m-%d_%H:%M:%S")
349 (setq smtpmail-queue-counter
350 (1+ smtpmail-queue-counter)))
352 (file-data (convert-standard-filename file-data))
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 (unless (file-exists-p smtpmail-queue-dir)
358 (make-directory smtpmail-queue-dir t))
359 (with-current-buffer buffer-data
361 (set-buffer-file-coding-system
362 ;; We will be reading the file with no-conversion in
363 ;; smtpmail-send-queued-mail below, so write it out
365 (coding-system-change-eol-conversion
366 (or smtpmail-code-conv-from 'undecided)
369 (insert-buffer-substring tembuf)
370 (write-file file-data)
371 (set-buffer buffer-elisp)
374 "(setq smtpmail-recipient-address-list '"
375 (prin1-to-string smtpmail-recipient-address-list)
377 (write-file file-elisp)
378 (set-buffer (generate-new-buffer buffer-scratch))
379 (insert (concat file-data "\n"))
380 (append-to-file (point-min)
382 (expand-file-name smtpmail-queue-index-file
383 smtpmail-queue-dir)))
384 (kill-buffer buffer-scratch)
385 (kill-buffer buffer-data)
386 (kill-buffer buffer-elisp))))
389 (kill-buffer errbuf)))))
392 (defun smtpmail-send-queued-mail ()
393 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
396 ;; Get index, get first mail, send it, update index, get second
397 ;; mail, send it, etc...
399 (qfile (expand-file-name smtpmail-queue-index-file
402 (insert-file-contents qfile)
403 (goto-char (point-min))
405 (setq file-msg (buffer-substring (point) (line-end-position)))
407 ;; Insert the message literally: it is already encoded as per
408 ;; the MIME headers, and code conversions might guess the
411 (let ((coding-system-for-read 'no-conversion))
412 (insert-file-contents file-msg))
413 (let ((smtpmail-mail-address
414 (or (and mail-specify-envelope-from (mail-envelope-from))
416 (if (not (null smtpmail-recipient-address-list))
417 (when (setq result (smtpmail-via-smtp
418 smtpmail-recipient-address-list
420 (error "Sending failed: %s" result))
421 (error "Sending failed; no recipients"))))
422 (delete-file file-msg)
423 (delete-file (concat file-msg ".el"))
424 (delete-region (point-at-bol) (point-at-bol 2)))
425 (write-region (point-min) (point-max) qfile))))
427 (defun smtpmail-fqdn ()
428 (if smtpmail-local-domain
429 (concat (system-name) "." smtpmail-local-domain)
432 (defsubst smtpmail-cred-server (cred)
435 (defsubst smtpmail-cred-port (cred)
438 (defsubst smtpmail-cred-key (cred)
441 (defsubst smtpmail-cred-user (cred)
444 (defsubst smtpmail-cred-cert (cred)
447 (defsubst smtpmail-cred-passwd (cred)
450 (defun smtpmail-find-credentials (cred server port)
453 (while (setq el (pop l))
454 (when (and (equal server (smtpmail-cred-server el))
455 (equal port (smtpmail-cred-port el)))
456 (throw 'done el))))))
458 (defun smtpmail-maybe-append-domain (recipient)
459 (if (or (not smtpmail-sendto-domain)
460 (string-match "@" recipient))
462 (concat recipient "@" smtpmail-sendto-domain)))
464 (defun smtpmail-intersection (list1 list2)
467 (when (memq el2 list1)
471 ;; `password-read' autoloads password-cache.
472 (declare-function password-cache-add "password-cache" (key password))
474 (defun smtpmail-command-or-throw (process string &optional code)
476 (smtpmail-send-command process string)
477 (unless (smtpmail-ok-p (setq ret (smtpmail-read-response process))
479 (throw 'done (format "%s in response to %s"
480 (smtpmail-response-text ret)
484 (defun smtpmail-try-auth-methods (process supported-extensions host port
485 &optional ask-for-password)
490 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions)))
491 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported)))
492 (auth-source-creation-prompts
493 '((user . "SMTP user name for %h: ")
494 (secret . "SMTP password for %u@%h: ")))
499 :user smtpmail-smtp-user
501 :require (and ask-for-password
503 :create ask-for-password)))
504 (user (plist-get auth-info :user))
505 (password (plist-get auth-info :secret))
506 (save-function (and ask-for-password
507 (plist-get auth-info :save-function)))
509 (when (functionp password)
510 (setq password (funcall password)))
513 ;; The user has stored the user name, but not the password, so
514 ;; ask for the password, even if we're not forcing that through
515 ;; `ask-for-password'.
522 :user smtpmail-smtp-user
523 :require '(:user :secret)
525 password (plist-get auth-info :secret)))
526 (when (functionp password)
527 (setq password (funcall password)))
532 ;; No mechanism, or no credentials.
535 (setq ret (smtpmail-command-or-throw process "AUTH CRAM-MD5"))
536 (when (eq (car ret) 334)
537 (let* ((challenge (substring (cadr ret) 4))
538 (decoded (base64-decode-string challenge))
539 (hash (rfc2104-hash 'md5 64 16 password decoded))
540 (response (concat user " " hash))
541 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
542 ;; SMTP auth fails because the SMTP server identifies
543 ;; only the first part of the string (delimited by
544 ;; new line characters) as a response from the
545 ;; client, and the rest as distinct commands.
547 ;; In my case, the response string is 80 characters
548 ;; long. Without the no-line-break option for
549 ;; `base64-encode-string', only the first 76 characters
550 ;; are taken as a response to the server, and the
551 ;; authentication fails.
552 (encoded (base64-encode-string response t)))
553 (smtpmail-command-or-throw process encoded)
555 (funcall save-function)))))
557 (smtpmail-command-or-throw process "AUTH LOGIN")
558 (smtpmail-command-or-throw
559 process (base64-encode-string user t))
560 (smtpmail-command-or-throw process (base64-encode-string password t))
562 (funcall save-function)))
564 ;; We used to send an empty initial request, and wait for an
565 ;; empty response, and then send the password, but this
566 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
567 ;; is not sent if the server did not advertise AUTH PLAIN in
568 ;; the EHLO response. See RFC 2554 for more info.
569 (smtpmail-command-or-throw
571 (concat "AUTH PLAIN "
572 (base64-encode-string (concat "\0" user "\0" password) t))
575 (funcall save-function)))
577 (error "Mechanism %s not implemented" mech)))))
579 (defun smtpmail-response-code (string)
583 (goto-char (point-min))
584 (and (re-search-forward "^\\([0-9]+\\) " nil t)
585 (string-to-number (match-string 1))))))
587 (defun smtpmail-ok-p (response &optional code)
589 (integerp (car response))
590 (< (car response) 400)
592 (= code (car response)))))
594 (defun smtpmail-response-text (response)
595 (mapconcat 'identity (cdr response) "\n"))
597 (defun smtpmail-query-smtp-server ()
598 (let ((server (read-string "Outgoing SMTP mail server: "))
599 (ports '("smtp" 587))
601 (when (and smtpmail-smtp-server
602 (not (member smtpmail-smtp-server ports)))
603 (push smtpmail-smtp-server ports))
604 (while (and (not smtpmail-smtp-server)
605 (setq port (pop ports)))
606 (when (setq stream (condition-case ()
607 (open-network-stream "smtp" nil server port)
610 (customize-save-variable 'smtpmail-smtp-server server)
611 (customize-save-variable 'smtpmail-smtp-service port)
612 (delete-process stream)))
613 (unless smtpmail-smtp-server
614 (error "Couldn't contact an SMTP server"))))
616 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
617 &optional ask-for-password)
618 (unless smtpmail-smtp-server
619 (smtpmail-query-smtp-server))
621 (host (or smtpmail-smtp-server
622 (error "`smtpmail-smtp-server' not defined")))
623 (port smtpmail-smtp-service)
624 ;; `smtpmail-mail-address' should be set to the appropriate
625 ;; buffer-local value by the caller, but in case not:
626 (envelope-from (or smtpmail-mail-address
627 (and mail-specify-envelope-from
628 (mail-envelope-from))
634 (supported-extensions '()))
637 ;; get or create the trace buffer
640 (format "*trace of SMTP session to %s*" host)))
642 ;; clear the trace buffer of old output
643 (with-current-buffer process-buffer
644 (setq buffer-undo-list t)
647 ;; open the connection to the server
648 (let ((coding-system-for-read 'binary)
649 (coding-system-for-write 'binary))
652 "smtpmail" process-buffer host port
653 :type smtpmail-stream-type
655 :capability-command (format "EHLO %s\r\n" (smtpmail-fqdn))
656 :end-of-command "^[0-9]+ .*\r\n"
658 :always-query-capabilities t
660 (lambda (capabilities)
661 (and (string-match "-STARTTLS" capabilities)
663 :client-certificate t
664 :use-starttls-if-possible t)))
666 ;; If we couldn't access the server at all, we give up.
667 (unless (setq process (car result))
668 (throw 'done (if (plist-get (cdr result) :error)
669 (plist-get (cdr result) :error)
670 "Unable to contact server")))
672 ;; set the send-filter
673 (set-process-filter process 'smtpmail-process-filter)
675 (let* ((greeting (plist-get (cdr result) :greeting))
676 (code (smtpmail-response-code greeting)))
678 (throw 'done (format "No greeting: %s" greeting)))
680 (throw 'done (format "Connection not allowed: %s" greeting))))
682 (with-current-buffer process-buffer
683 (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
684 (make-local-variable 'smtpmail-read-point)
685 (setq smtpmail-read-point (point-min))
687 (let* ((capabilities (plist-get (cdr result) :capabilities))
688 (code (smtpmail-response-code capabilities)))
691 ;; The server didn't accept EHLO, so we fall back on HELO.
692 (smtpmail-command-or-throw
693 process (format "HELO %s" (smtpmail-fqdn)))
694 ;; EHLO was successful, so we parse the extensions.
695 (dolist (line (delete
698 (plist-get (cdr result) :capabilities)
701 (with-case-table ascii-case-table
702 (mapcar (lambda (s) (intern (downcase s)))
703 (split-string (substring line 4) "[ ]")))))
704 (when (= (length name) 1)
705 (setq name (car name)))
707 (cond ((memq (if (consp name) (car name) name)
708 '(verb xvrb 8bitmime onex xone
712 auth=login auth starttls))
713 (setq supported-extensions
714 (cons name supported-extensions)))
715 (smtpmail-warn-about-unknown-extensions
716 (message "Unknown extension %s" name))))))))
718 (setq auth-mechanisms
719 (smtpmail-try-auth-methods
720 process supported-extensions host port
723 (when (or (member 'onex supported-extensions)
724 (member 'xone supported-extensions))
725 (smtpmail-command-or-throw process (format "ONEX")))
727 (when (and smtpmail-debug-verb
728 (or (member 'verb supported-extensions)
729 (member 'xvrb supported-extensions)))
730 (smtpmail-command-or-throw process (format "VERB")))
732 (when (member 'xusr supported-extensions)
733 (smtpmail-command-or-throw process (format "XUSR")))
735 ;; MAIL FROM:<sender>
737 (if (or (member 'size supported-extensions)
738 (assoc 'size supported-extensions))
740 (with-current-buffer smtpmail-text-buffer
742 (+ (- (point-max) (point-min))
743 ;; Add one byte for each change-of-line
744 ;; because of CR-LF representation:
745 (count-lines (point-min) (point-max)))))
748 (if (member '8bitmime supported-extensions)
750 ;; Code should be added here that transforms
751 ;; the contents of the message buffer into
752 ;; something the receiving SMTP can handle.
753 ;; For a receiver that supports 8BITMIME, this
754 ;; may mean converting BINARY to BASE64, or
755 ;; adding Content-Transfer-Encoding and the
756 ;; other MIME headers. The code should also
757 ;; return an indication of what encoding the
758 ;; message buffer is now, i.e. ASCII or
764 (smtpmail-send-command
765 process (format "MAIL FROM:<%s>%s%s"
766 envelope-from size-part body-part))
768 ((smtpmail-ok-p (setq result (smtpmail-read-response process)))
771 ((and auth-mechanisms
772 (not ask-for-password)
773 (eq (car result) 530))
774 ;; We got a "530 auth required", so we close and try
775 ;; again, this time asking the user for a password.
776 (smtpmail-send-command process "QUIT")
777 (smtpmail-read-response process)
778 (delete-process process)
781 (smtpmail-via-smtp recipient smtpmail-text-buffer t)))
783 ;; Return the error code.
785 (smtpmail-response-text result)))))
787 ;; RCPT TO:<recipient>
789 (while (not (null (nth n recipient)))
790 (smtpmail-send-command
791 process (format "RCPT TO:<%s>"
792 (smtpmail-maybe-append-domain
795 ((smtpmail-ok-p (setq result (smtpmail-read-response process)))
798 ((and auth-mechanisms
799 (not ask-for-password)
800 (integerp (car result))
801 (>= (car result) 550)
802 (<= (car result) 554))
803 ;; We got a "550 relay not permitted" (or the like),
804 ;; and the server accepts credentials, so we try
805 ;; again, but ask for a password first.
806 (smtpmail-send-command process "QUIT")
807 (smtpmail-read-response process)
808 (delete-process process)
811 (smtpmail-via-smtp recipient smtpmail-text-buffer t)))
813 ;; Return the error code.
815 (smtpmail-response-text result))))
818 ;; Send the contents.
819 (smtpmail-command-or-throw process "DATA")
820 (smtpmail-send-data process smtpmail-text-buffer)
822 (smtpmail-command-or-throw process ".")
826 (buffer-live-p process-buffer))
827 (with-current-buffer (process-buffer process)
828 (smtpmail-send-command process "QUIT")
829 (smtpmail-read-response process)
830 (delete-process process)
831 (unless smtpmail-debug-info
832 (kill-buffer process-buffer)))))))
835 (defun smtpmail-process-filter (process output)
836 (with-current-buffer (process-buffer process)
837 (goto-char (point-max))
840 (defun smtpmail-read-response (process)
841 (let ((case-fold-search nil)
842 (response-strings nil)
843 (response-continue t)
844 (return-value '(nil ()))
847 (while response-continue
848 (goto-char smtpmail-read-point)
849 (while (not (search-forward "\r\n" nil t))
850 (unless (memq (process-status process) '(open run))
852 (accept-process-output process)
853 (goto-char smtpmail-read-point))
855 (setq match-end (point))
856 (setq response-strings
857 (cons (buffer-substring smtpmail-read-point (- match-end 2))
860 (goto-char smtpmail-read-point)
861 (if (looking-at "[0-9]+ ")
862 (let ((begin (match-beginning 0))
864 (if smtpmail-debug-info
865 (message "%s" (car response-strings)))
867 (setq smtpmail-read-point match-end)
869 ;; ignore lines that start with "0"
870 (if (looking-at "0[0-9]+ ")
872 (setq response-continue nil)
874 (cons (string-to-number
875 (buffer-substring begin end))
876 (nreverse response-strings)))))
878 (if (looking-at "[0-9]+-")
879 (progn (if smtpmail-debug-info
880 (message "%s" (car response-strings)))
881 (setq smtpmail-read-point match-end)
882 (setq response-continue t))
884 (setq smtpmail-read-point match-end)
885 (setq response-continue nil)
887 (cons nil (nreverse response-strings)))))))
888 (setq smtpmail-read-point match-end))
892 (defun smtpmail-send-command (process command)
893 (goto-char (point-max))
894 (if (= (aref command 0) ?P)
895 (insert "PASS <omitted>\r\n")
896 (insert command "\r\n"))
897 (setq smtpmail-read-point (point))
898 (process-send-string process command)
899 (process-send-string process "\r\n"))
901 (defun smtpmail-send-data-1 (process data)
902 (goto-char (point-max))
904 (if (and (multibyte-string-p data)
905 smtpmail-code-conv-from)
906 (setq data (string-as-multibyte
907 (encode-coding-string data smtpmail-code-conv-from))))
909 (if smtpmail-debug-info
910 (insert data "\r\n"))
912 (setq smtpmail-read-point (point))
913 ;; Escape "." at start of a line
914 (if (eq (string-to-char data) ?.)
915 (process-send-string process "."))
916 (process-send-string process data)
917 (process-send-string process "\r\n"))
919 (defun smtpmail-send-data (process buffer)
920 (let ((data-continue t) sending-data
921 (pr (with-current-buffer buffer
922 (make-progress-reporter "Sending email"
923 (point-min) (point-max)))))
924 (with-current-buffer buffer
925 (goto-char (point-min)))
927 (with-current-buffer buffer
928 (progress-reporter-update pr (point))
929 (setq sending-data (buffer-substring (point-at-bol) (point-at-eol)))
931 (setq data-continue (not (eobp))))
932 (smtpmail-send-data-1 process sending-data))
933 (progress-reporter-done pr)))
935 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
936 "Get address list suitable for smtp RCPT TO: <address>."
938 (with-current-buffer smtpmail-address-buffer
940 (let ((case-fold-search t)
941 (simple-address-list "")
945 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
946 (goto-char (point-min))
947 ;; RESENT-* fields should stop processing of regular fields.
950 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
952 "^Resent-\\(to\\|cc\\|bcc\\):"
953 "^\\(To:\\|Cc:\\|Bcc:\\)")))
955 (while (re-search-forward addr-regexp header-end t)
957 (setq this-line (match-beginning 0))
959 ;; get any continuation lines
960 (while (and (looking-at "^[ \t]+") (< (point) header-end))
962 (setq this-line-end (point-marker))
963 (setq simple-address-list
964 (concat simple-address-list " "
965 (mail-strip-quoted-names (buffer-substring this-line this-line-end)))))
967 (insert " " simple-address-list "\n")
968 (subst-char-in-region (point-min) (point-max) 10 ? t) ; newline --> blank
969 (subst-char-in-region (point-min) (point-max) ?, ? t) ; comma --> blank
970 (subst-char-in-region (point-min) (point-max) 9 ? t) ; tab --> blank
972 (goto-char (point-min))
973 ;; tidyness in case hook is not robust when it looks at this
974 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
976 (goto-char (point-min))
977 (let (recipient-address-list)
978 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
980 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
981 recipient-address-list)))
982 (setq smtpmail-recipient-address-list recipient-address-list))))))
984 (defun smtpmail-do-bcc (header-end)
985 "Delete [Resent-]BCC: and their continuation lines from the header area.
986 There may be multiple BCC: lines, and each may have arbitrarily
987 many continuation lines."
988 (let ((case-fold-search t))
990 (goto-char (point-min))
991 ;; iterate over all BCC: lines
992 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
993 (delete-region (match-beginning 0)
994 (progn (forward-line 1) (point)))
995 ;; get rid of any continuation lines
996 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
997 (replace-match ""))))))
1001 ;;; smtpmail.el ends here