1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995-1996, 2001-2012 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")
65 (defgroup smtpmail nil
66 "SMTP protocol for sending mail."
70 (defcustom smtpmail-default-smtp-server nil
71 "Specify default SMTP server.
72 This only has effect if you specify it before loading the smtpmail library."
73 :type
'(choice (const nil
) string
)
76 (defcustom smtpmail-smtp-server
77 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server
)
78 "The name of the host running SMTP server."
79 :type
'(choice (const nil
) string
)
82 (defcustom smtpmail-smtp-service
25
83 "SMTP service port number.
84 The default value would be \"smtp\" or 25."
85 :type
'(choice (integer :tag
"Port") (string :tag
"Service"))
88 (defcustom smtpmail-smtp-user nil
89 "User name to use when looking up credentials."
91 :type
'(choice (const nil
) string
)
94 (defcustom smtpmail-local-domain nil
95 "Local domain name without a host name.
96 If the function `system-name' returns the full internet address,
97 don't define this value."
98 :type
'(choice (const nil
) string
)
101 (defcustom smtpmail-stream-type nil
102 "Connection type SMTP connections.
103 This may be either nil (possibly upgraded to STARTTLS if
104 possible), or `starttls' (refuse to send if STARTTLS isn't
105 available), or `plain' (never use STARTTLS), or `ssl' (to use
109 :type
'(choice (const :tag
"Possibly upgrade to STARTTLS" nil
)
110 (const :tag
"Always use STARTTLS" starttls
)
111 (const :tag
"Never use STARTTLS" plain
)
112 (const :tag
"Use TLS/SSL" ssl
)))
114 (defcustom smtpmail-sendto-domain nil
115 "Local domain name without a host name.
116 This is appended (with an @-sign) to any specified recipients which do
117 not include an @-sign, so that each RCPT TO address is fully qualified.
118 \(Some configurations of sendmail require this.)
120 Don't bother to set this unless you have get an error like:
121 Sending failed; 501 <someone>: recipient address must contain a domain."
122 :type
'(choice (const nil
) string
)
125 (defcustom smtpmail-debug-info nil
126 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
127 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
132 (defcustom smtpmail-debug-verb nil
133 "Whether this library sends the SMTP VERB command or not.
134 The commands enables verbose information from the SMTP server."
138 (defcustom smtpmail-code-conv-from nil
139 "Coding system for encoding outgoing mail.
140 Used for the value of `sendmail-coding-system' when
141 `select-message-coding-system' is called. "
145 (defcustom smtpmail-queue-mail nil
146 "Non-nil means mail is queued; otherwise it is sent immediately.
147 If queued, it is stored in the directory `smtpmail-queue-dir'
148 and sent with `smtpmail-send-queued-mail'."
152 (defcustom smtpmail-queue-dir
"~/Mail/queued-mail/"
153 "Directory where `smtpmail.el' stores queued mail."
157 (defcustom smtpmail-warn-about-unknown-extensions nil
158 "If set, print warnings about unknown SMTP extensions.
159 This is mainly useful for development purposes, to learn about
160 new SMTP extensions that might be useful to support."
165 (defcustom smtpmail-queue-index-file
"index"
166 "File name of queued mail index.
167 This is relative to `smtpmail-queue-dir'."
171 ;; End of customizable variables.
174 (defvar smtpmail-address-buffer
)
175 (defvar smtpmail-recipient-address-list
)
177 (defvar smtpmail-queue-counter
0)
179 ;; Buffer-local variable.
180 (defvar smtpmail-read-point
)
182 (defconst smtpmail-auth-supported
'(cram-md5 plain login
)
183 "List of supported SMTP AUTH mechanisms.
184 The list is in preference order.")
186 (defvar smtpmail-mail-address nil
187 "Value to use for envelope-from address for mail from ambient buffer.")
190 (defun smtpmail-send-it ()
191 (let ((errbuf (if mail-interactive
192 (generate-new-buffer " smtpmail errors")
194 (tembuf (generate-new-buffer " smtpmail temp"))
195 (case-fold-search nil
)
198 (mailbuf (current-buffer))
199 ;; Examine this variable now, so that
200 ;; local binding in the mail buffer will take effect.
201 (smtpmail-mail-address
202 (or (and mail-specify-envelope-from
(mail-envelope-from))
204 (smtpmail-code-conv-from
205 (if enable-multibyte-characters
206 (let ((sendmail-coding-system smtpmail-code-conv-from
))
207 (select-message-coding-system)))))
209 (with-current-buffer tembuf
211 ;; Use the same `buffer-file-coding-system' as in the mail
212 ;; buffer, otherwise any `write-region' invocations (e.g., in
213 ;; mail-do-fcc below) will annoy with asking for a suitable
215 (set-buffer-file-coding-system smtpmail-code-conv-from nil t
)
216 (insert-buffer-substring mailbuf
)
217 (goto-char (point-max))
218 ;; require one newline at the end.
219 (or (= (preceding-char) ?
\n)
221 ;; Change header-delimiter to be what sendmail expects.
222 (mail-sendmail-undelimit-header)
223 (setq delimline
(point-marker))
224 ;; (sendmail-synch-aliases)
226 (expand-mail-aliases (point-min) delimline
))
227 (goto-char (point-min))
228 ;; ignore any blank lines in the header
229 (while (and (re-search-forward "\n\n\n*" delimline t
)
230 (< (point) delimline
))
231 (replace-match "\n"))
232 (let ((case-fold-search t
))
233 ;; We used to process Resent-... headers here,
234 ;; but it was not done properly, and the job
235 ;; is done correctly in `smtpmail-deduce-address-list'.
236 ;; Don't send out a blank subject line
237 (goto-char (point-min))
238 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
240 ;; This one matches a Subject just before the header delimiter.
241 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
242 (= (match-end 0) delimline
))
244 ;; Put the "From:" field in unless for some odd reason
245 ;; they put one in themselves.
246 (goto-char (point-min))
247 (if (not (re-search-forward "^From:" delimline t
))
248 (let* ((login smtpmail-mail-address
)
249 (fullname (user-full-name)))
250 (cond ((eq mail-from-style
'angles
)
251 (insert "From: " fullname
)
252 (let ((fullname-start (+ (point-min) 6))
253 (fullname-end (point-marker)))
254 (goto-char fullname-start
)
255 ;; Look for a character that cannot appear unquoted
256 ;; according to RFC 822.
257 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
260 ;; Quote fullname, escaping specials.
261 (goto-char fullname-start
)
263 (while (re-search-forward "[\"\\]"
265 (replace-match "\\\\\\&" t
))
267 (insert " <" login
">\n"))
268 ((eq mail-from-style
'parens
)
269 (insert "From: " login
" (")
270 (let ((fullname-start (point)))
272 (let ((fullname-end (point-marker)))
273 (goto-char fullname-start
)
274 ;; RFC 822 says \ and nonmatching parentheses
275 ;; must be escaped in comments.
276 ;; Escape every instance of ()\ ...
277 (while (re-search-forward "[()\\]" fullname-end
1)
278 (replace-match "\\\\\\&" t
))
279 ;; ... then undo escaping of matching parentheses,
280 ;; including matching nested parentheses.
281 (goto-char fullname-start
)
282 (while (re-search-forward
283 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
285 (replace-match "\\1(\\3)" t
)
286 (goto-char fullname-start
))))
288 ((null mail-from-style
)
289 (insert "From: " login
"\n")))))
290 ;; Insert a `Message-Id:' field if there isn't one yet.
291 (goto-char (point-min))
292 (unless (re-search-forward "^Message-Id:" delimline t
)
293 (insert "Message-Id: " (message-make-message-id) "\n"))
294 ;; Insert a `Date:' field if there isn't one yet.
295 (goto-char (point-min))
296 (unless (re-search-forward "^Date:" delimline t
)
297 (insert "Date: " (message-make-date) "\n"))
298 ;; Possibly add a MIME header for the current coding system
300 (goto-char (point-min))
301 (and (eq mail-send-nonascii
'mime
)
302 (not (re-search-forward "^MIME-version:" delimline t
))
303 (progn (skip-chars-forward "\0-\177")
304 (/= (point) (point-max)))
305 smtpmail-code-conv-from
307 (coding-system-get smtpmail-code-conv-from
309 (goto-char delimline
)
310 (insert "MIME-version: 1.0\n"
311 "Content-type: text/plain; charset="
312 (symbol-name charset
)
313 "\nContent-Transfer-Encoding: 8bit\n")))
314 ;; Insert an extra newline if we need it to work around
315 ;; Sun's bug that swallows newlines.
316 (goto-char (1+ delimline
))
317 (if (eval mail-mailer-swallows-blank-line
)
319 ;; Find and handle any FCC fields.
320 (goto-char (point-min))
321 (if (re-search-forward "^FCC:" delimline t
)
322 ;; Force `mail-do-fcc' to use the encoding of the mail
323 ;; buffer to encode outgoing messages on FCC files.
324 (let ((coding-system-for-write smtpmail-code-conv-from
))
325 (mail-do-fcc delimline
)))
327 (with-current-buffer errbuf
329 ;; Encode the header according to RFC2047.
330 (mail-encode-header (point-min) delimline
)
332 (setq smtpmail-address-buffer
(generate-new-buffer "*smtp-mail*"))
333 (setq smtpmail-recipient-address-list
334 (smtpmail-deduce-address-list tembuf
(point-min) delimline
))
335 (kill-buffer smtpmail-address-buffer
)
337 (smtpmail-do-bcc delimline
)
339 (if (not smtpmail-queue-mail
)
340 (if (not (null smtpmail-recipient-address-list
))
343 smtpmail-recipient-address-list tembuf
))
344 (error "Sending failed: %s" result
))
345 (error "Sending failed; no recipients"))
349 (format-time-string "%Y-%m-%d_%H:%M:%S")
350 (setq smtpmail-queue-counter
351 (1+ smtpmail-queue-counter
)))
353 (file-data (convert-standard-filename file-data
))
354 (file-elisp (concat file-data
".el"))
355 (buffer-data (create-file-buffer file-data
))
356 (buffer-elisp (create-file-buffer file-elisp
))
357 (buffer-scratch "*queue-mail*"))
358 (unless (file-exists-p smtpmail-queue-dir
)
359 (make-directory smtpmail-queue-dir t
))
360 (with-current-buffer buffer-data
362 (set-buffer-file-coding-system
363 ;; We will be reading the file with no-conversion in
364 ;; smtpmail-send-queued-mail below, so write it out
366 (coding-system-change-eol-conversion
367 (or smtpmail-code-conv-from
'undecided
)
370 (insert-buffer-substring tembuf
)
371 (write-file file-data
)
372 (set-buffer buffer-elisp
)
375 "(setq smtpmail-recipient-address-list '"
376 (prin1-to-string smtpmail-recipient-address-list
)
378 (write-file file-elisp
)
379 (set-buffer (generate-new-buffer buffer-scratch
))
380 (insert (concat file-data
"\n"))
381 (append-to-file (point-min)
383 (expand-file-name smtpmail-queue-index-file
384 smtpmail-queue-dir
)))
385 (kill-buffer buffer-scratch
)
386 (kill-buffer buffer-data
)
387 (kill-buffer buffer-elisp
))))
390 (kill-buffer errbuf
)))))
393 (defun smtpmail-send-queued-mail ()
394 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
397 ;; Get index, get first mail, send it, update index, get second
398 ;; mail, send it, etc...
400 (qfile (expand-file-name smtpmail-queue-index-file
403 (insert-file-contents qfile
)
404 (goto-char (point-min))
406 (setq file-msg
(buffer-substring (point) (line-end-position)))
408 ;; Insert the message literally: it is already encoded as per
409 ;; the MIME headers, and code conversions might guess the
412 (let ((coding-system-for-read 'no-conversion
))
413 (insert-file-contents file-msg
))
414 (let ((smtpmail-mail-address
415 (or (and mail-specify-envelope-from
(mail-envelope-from))
417 (if (not (null smtpmail-recipient-address-list
))
418 (when (setq result
(smtpmail-via-smtp
419 smtpmail-recipient-address-list
421 (error "Sending failed: %s" result
))
422 (error "Sending failed; no recipients"))))
423 (delete-file file-msg
)
424 (delete-file (concat file-msg
".el"))
425 (delete-region (point-at-bol) (point-at-bol 2)))
426 (write-region (point-min) (point-max) qfile
))))
428 (defun smtpmail-fqdn ()
429 (if smtpmail-local-domain
430 (concat (system-name) "." smtpmail-local-domain
)
433 (defsubst smtpmail-cred-server
(cred)
436 (defsubst smtpmail-cred-port
(cred)
439 (defsubst smtpmail-cred-key
(cred)
442 (defsubst smtpmail-cred-user
(cred)
445 (defsubst smtpmail-cred-cert
(cred)
448 (defsubst smtpmail-cred-passwd
(cred)
451 (defun smtpmail-find-credentials (cred server port
)
454 (while (setq el
(pop l
))
455 (when (and (equal server
(smtpmail-cred-server el
))
456 (equal port
(smtpmail-cred-port el
)))
457 (throw 'done el
))))))
459 (defun smtpmail-maybe-append-domain (recipient)
460 (if (or (not smtpmail-sendto-domain
)
461 (string-match "@" recipient
))
463 (concat recipient
"@" smtpmail-sendto-domain
)))
465 (defun smtpmail-intersection (list1 list2
)
468 (when (memq el2 list1
)
472 (defun smtpmail-command-or-throw (process string
&optional code
)
474 (smtpmail-send-command process string
)
475 (unless (smtpmail-ok-p (setq ret
(smtpmail-read-response process
))
477 (throw 'done
(format "%s in response to %s"
478 (smtpmail-response-text ret
)
482 (defun smtpmail-try-auth-methods (process supported-extensions host port
483 &optional ask-for-password
)
488 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions
)))
489 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported
)))
490 (auth-source-creation-prompts
491 '((user .
"SMTP user name for %h: ")
492 (secret .
"SMTP password for %u@%h: ")))
497 :user smtpmail-smtp-user
499 :require
(and ask-for-password
501 :create ask-for-password
)))
502 (user (plist-get auth-info
:user
))
503 (password (plist-get auth-info
:secret
))
504 (save-function (and ask-for-password
505 (plist-get auth-info
:save-function
)))
507 (when (functionp password
)
508 (setq password
(funcall password
)))
511 ;; The user has stored the user name, but not the password, so
512 ;; ask for the password, even if we're not forcing that through
513 ;; `ask-for-password'.
520 :user smtpmail-smtp-user
521 :require
'(:user
:secret
)
523 password
(plist-get auth-info
:secret
)))
524 (when (functionp password
)
525 (setq password
(funcall password
)))
530 ;; No mechanism, or no credentials.
533 (setq ret
(smtpmail-command-or-throw process
"AUTH CRAM-MD5"))
534 (when (eq (car ret
) 334)
535 (let* ((challenge (substring (cadr ret
) 4))
536 (decoded (base64-decode-string challenge
))
537 (hash (rfc2104-hash 'md5
64 16 password decoded
))
538 (response (concat user
" " hash
))
539 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
540 ;; SMTP auth fails because the SMTP server identifies
541 ;; only the first part of the string (delimited by
542 ;; new line characters) as a response from the
543 ;; client, and the rest as distinct commands.
545 ;; In my case, the response string is 80 characters
546 ;; long. Without the no-line-break option for
547 ;; `base64-encode-string', only the first 76 characters
548 ;; are taken as a response to the server, and the
549 ;; authentication fails.
550 (encoded (base64-encode-string response t
)))
551 (smtpmail-command-or-throw process encoded
)
553 (funcall save-function
)))))
555 (smtpmail-command-or-throw process
"AUTH LOGIN")
556 (smtpmail-command-or-throw
557 process
(base64-encode-string user t
))
558 (smtpmail-command-or-throw process
(base64-encode-string password t
))
560 (funcall save-function
)))
562 ;; We used to send an empty initial request, and wait for an
563 ;; empty response, and then send the password, but this
564 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
565 ;; is not sent if the server did not advertise AUTH PLAIN in
566 ;; the EHLO response. See RFC 2554 for more info.
567 (smtpmail-command-or-throw
569 (concat "AUTH PLAIN "
570 (base64-encode-string (concat "\0" user
"\0" password
) t
))
573 (funcall save-function
)))
575 (error "Mechanism %s not implemented" mech
)))))
577 (defun smtpmail-response-code (string)
581 (goto-char (point-min))
582 (and (re-search-forward "^\\([0-9]+\\) " nil t
)
583 (string-to-number (match-string 1))))))
585 (defun smtpmail-ok-p (response &optional code
)
587 (integerp (car response
))
588 (< (car response
) 400)
590 (= code
(car response
)))))
592 (defun smtpmail-response-text (response)
593 (mapconcat 'identity
(cdr response
) "\n"))
595 (defun smtpmail-query-smtp-server ()
596 (let ((server (read-string "Outgoing SMTP mail server: "))
597 (ports '("smtp" 587))
599 (when (and smtpmail-smtp-server
600 (not (member smtpmail-smtp-server ports
)))
601 (push smtpmail-smtp-server ports
))
602 (while (and (not smtpmail-smtp-server
)
603 (setq port
(pop ports
)))
604 (when (setq stream
(condition-case ()
605 (open-network-stream "smtp" nil server port
)
608 (customize-save-variable 'smtpmail-smtp-server server
)
609 (customize-save-variable 'smtpmail-smtp-service port
)
610 (delete-process stream
)))
611 (unless smtpmail-smtp-server
612 (error "Couldn't contact an SMTP server"))))
614 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
615 &optional ask-for-password
)
616 (unless smtpmail-smtp-server
617 (smtpmail-query-smtp-server))
619 (host (or smtpmail-smtp-server
620 (error "`smtpmail-smtp-server' not defined")))
621 (port smtpmail-smtp-service
)
622 ;; `smtpmail-mail-address' should be set to the appropriate
623 ;; buffer-local value by the caller, but in case not:
624 (envelope-from (or smtpmail-mail-address
625 (and mail-specify-envelope-from
626 (mail-envelope-from))
632 (supported-extensions '()))
635 ;; get or create the trace buffer
638 (format "*trace of SMTP session to %s*" host
)))
640 ;; clear the trace buffer of old output
641 (with-current-buffer process-buffer
642 (setq buffer-undo-list t
)
645 ;; open the connection to the server
646 (let ((coding-system-for-read 'binary
)
647 (coding-system-for-write 'binary
))
650 "smtpmail" process-buffer host port
651 :type smtpmail-stream-type
653 :capability-command
(format "EHLO %s\r\n" (smtpmail-fqdn))
654 :end-of-command
"^[0-9]+ .*\r\n"
656 :always-query-capabilities t
658 (lambda (capabilities)
659 (and (string-match "[ -]STARTTLS" capabilities
)
661 :client-certificate t
662 :use-starttls-if-possible t
)))
664 ;; If we couldn't access the server at all, we give up.
665 (unless (setq process
(car result
))
666 (throw 'done
(if (plist-get (cdr result
) :error
)
667 (plist-get (cdr result
) :error
)
668 "Unable to contact server")))
670 ;; set the send-filter
671 (set-process-filter process
'smtpmail-process-filter
)
673 (let* ((greeting (plist-get (cdr result
) :greeting
))
674 (code (smtpmail-response-code greeting
)))
676 (throw 'done
(format "No greeting: %s" greeting
)))
678 (throw 'done
(format "Connection not allowed: %s" greeting
))))
680 (with-current-buffer process-buffer
681 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
682 (make-local-variable 'smtpmail-read-point
)
683 (setq smtpmail-read-point
(point-min))
685 (let* ((capabilities (plist-get (cdr result
) :capabilities
))
686 (code (smtpmail-response-code capabilities
)))
689 ;; The server didn't accept EHLO, so we fall back on HELO.
690 (smtpmail-command-or-throw
691 process
(format "HELO %s" (smtpmail-fqdn)))
692 ;; EHLO was successful, so we parse the extensions.
693 (dolist (line (delete
696 (plist-get (cdr result
) :capabilities
)
699 (with-case-table ascii-case-table
700 (mapcar (lambda (s) (intern (downcase s
)))
701 (split-string (substring line
4) "[ ]")))))
702 (when (= (length name
) 1)
703 (setq name
(car name
)))
705 (cond ((memq (if (consp name
) (car name
) name
)
706 '(verb xvrb
8bitmime onex xone
710 auth
=login auth starttls
))
711 (setq supported-extensions
712 (cons name supported-extensions
)))
713 (smtpmail-warn-about-unknown-extensions
714 (message "Unknown extension %s" name
))))))))
716 (setq auth-mechanisms
717 (smtpmail-try-auth-methods
718 process supported-extensions host port
721 (when (or (member 'onex supported-extensions
)
722 (member 'xone supported-extensions
))
723 (smtpmail-command-or-throw process
(format "ONEX")))
725 (when (and smtpmail-debug-verb
726 (or (member 'verb supported-extensions
)
727 (member 'xvrb supported-extensions
)))
728 (smtpmail-command-or-throw process
(format "VERB")))
730 (when (member 'xusr supported-extensions
)
731 (smtpmail-command-or-throw process
(format "XUSR")))
733 ;; MAIL FROM:<sender>
735 (if (or (member 'size supported-extensions
)
736 (assoc 'size supported-extensions
))
738 (with-current-buffer smtpmail-text-buffer
740 (+ (- (point-max) (point-min))
741 ;; Add one byte for each change-of-line
742 ;; because of CR-LF representation:
743 (count-lines (point-min) (point-max)))))
746 (if (member '8bitmime supported-extensions
)
748 ;; Code should be added here that transforms
749 ;; the contents of the message buffer into
750 ;; something the receiving SMTP can handle.
751 ;; For a receiver that supports 8BITMIME, this
752 ;; may mean converting BINARY to BASE64, or
753 ;; adding Content-Transfer-Encoding and the
754 ;; other MIME headers. The code should also
755 ;; return an indication of what encoding the
756 ;; message buffer is now, i.e. ASCII or
762 (smtpmail-send-command
763 process
(format "MAIL FROM:<%s>%s%s"
764 envelope-from size-part body-part
))
766 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
769 ((and auth-mechanisms
770 (not ask-for-password
)
771 (eq (car result
) 530))
772 ;; We got a "530 auth required", so we close and try
773 ;; again, this time asking the user for a password.
774 ;; We ignore any errors here, because some MTAs just
775 ;; close the connection immediately after giving the
778 (smtpmail-send-command process
"QUIT")
779 (smtpmail-read-response process
))
780 (delete-process process
)
783 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
785 ;; Return the error code.
787 (smtpmail-response-text result
)))))
789 ;; RCPT TO:<recipient>
791 (while (not (null (nth n recipient
)))
792 (smtpmail-send-command
793 process
(format "RCPT TO:<%s>"
794 (smtpmail-maybe-append-domain
797 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
800 ((and auth-mechanisms
801 (not ask-for-password
)
802 (integerp (car result
))
803 (>= (car result
) 550)
804 (<= (car result
) 554))
805 ;; We got a "550 relay not permitted" (or the like),
806 ;; and the server accepts credentials, so we try
807 ;; again, but ask for a password first.
808 (smtpmail-send-command process
"QUIT")
809 (smtpmail-read-response process
)
810 (delete-process process
)
813 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
815 ;; Return the error code.
817 (smtpmail-response-text result
))))
820 ;; Send the contents.
821 (smtpmail-command-or-throw process
"DATA")
822 (smtpmail-send-data process smtpmail-text-buffer
)
824 (smtpmail-command-or-throw process
".")
828 (buffer-live-p process-buffer
))
829 (with-current-buffer (process-buffer process
)
830 (smtpmail-send-command process
"QUIT")
831 (smtpmail-read-response process
)
832 (delete-process process
)
833 (unless smtpmail-debug-info
834 (kill-buffer process-buffer
)))))))
837 (defun smtpmail-process-filter (process output
)
838 (with-current-buffer (process-buffer process
)
839 (goto-char (point-max))
841 (set-marker (process-mark process
) (point))))
843 (defun smtpmail-read-response (process)
844 (let ((case-fold-search nil
)
845 (response-strings nil
)
846 (response-continue t
)
847 (return-value '(nil ()))
850 (while response-continue
851 (goto-char smtpmail-read-point
)
852 (while (not (search-forward "\r\n" nil t
))
853 (unless (memq (process-status process
) '(open run
))
855 (accept-process-output process
)
856 (goto-char smtpmail-read-point
))
858 (setq match-end
(point))
859 (setq response-strings
860 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
863 (goto-char smtpmail-read-point
)
864 (if (looking-at "[0-9]+ ")
865 (let ((begin (match-beginning 0))
867 (if smtpmail-debug-info
868 (message "%s" (car response-strings
)))
870 (setq smtpmail-read-point match-end
)
872 ;; ignore lines that start with "0"
873 (if (looking-at "0[0-9]+ ")
875 (setq response-continue nil
)
877 (cons (string-to-number
878 (buffer-substring begin end
))
879 (nreverse response-strings
)))))
881 (if (looking-at "[0-9]+-")
882 (progn (if smtpmail-debug-info
883 (message "%s" (car response-strings
)))
884 (setq smtpmail-read-point match-end
)
885 (setq response-continue t
))
887 (setq smtpmail-read-point match-end
)
888 (setq response-continue nil
)
890 (cons nil
(nreverse response-strings
)))))))
891 (setq smtpmail-read-point match-end
))
895 (defun smtpmail-send-command (process command
)
896 (goto-char (point-max))
897 (if (string-match "\\`AUTH [A-Z]+ " command
)
898 (insert (match-string 0 command
) "<omitted>\r\n")
899 (insert command
"\r\n"))
900 (setq smtpmail-read-point
(point))
901 (process-send-string process command
)
902 (process-send-string process
"\r\n"))
904 (defun smtpmail-send-data-1 (process data
)
905 (goto-char (point-max))
907 (if (and (multibyte-string-p data
)
908 smtpmail-code-conv-from
)
909 (setq data
(string-as-multibyte
910 (encode-coding-string data smtpmail-code-conv-from
))))
912 (if smtpmail-debug-info
913 (insert data
"\r\n"))
915 (setq smtpmail-read-point
(point))
916 ;; Escape "." at start of a line
917 (if (eq (string-to-char data
) ?.
)
918 (process-send-string process
"."))
919 (process-send-string process data
)
920 (process-send-string process
"\r\n"))
922 (defun smtpmail-send-data (process buffer
)
923 (let ((data-continue t
) sending-data
924 (pr (with-current-buffer buffer
925 (make-progress-reporter "Sending email "
926 (point-min) (point-max)))))
927 (with-current-buffer buffer
928 (goto-char (point-min)))
930 (with-current-buffer buffer
931 (progress-reporter-update pr
(point))
932 (setq sending-data
(buffer-substring (point-at-bol) (point-at-eol)))
934 (setq data-continue
(not (eobp))))
935 (smtpmail-send-data-1 process sending-data
))
936 (progress-reporter-done pr
)))
938 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
939 "Get address list suitable for smtp RCPT TO: <address>."
941 (with-current-buffer smtpmail-address-buffer
943 (let ((case-fold-search t
)
944 (simple-address-list "")
948 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
949 (goto-char (point-min))
950 ;; RESENT-* fields should stop processing of regular fields.
953 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
955 "^Resent-\\(to\\|cc\\|bcc\\):"
956 "^\\(To:\\|Cc:\\|Bcc:\\)")))
958 (while (re-search-forward addr-regexp header-end t
)
960 (setq this-line
(match-beginning 0))
962 ;; get any continuation lines
963 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
965 (setq this-line-end
(point-marker))
966 (setq simple-address-list
967 (concat simple-address-list
" "
968 (mail-strip-quoted-names (buffer-substring this-line this-line-end
)))))
970 (insert " " simple-address-list
"\n")
971 (subst-char-in-region (point-min) (point-max) 10 ? t
) ; newline --> blank
972 (subst-char-in-region (point-min) (point-max) ?
, ? t
) ; comma --> blank
973 (subst-char-in-region (point-min) (point-max) 9 ? t
) ; tab --> blank
975 (goto-char (point-min))
976 ;; tidiness in case hook is not robust when it looks at this
977 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
979 (goto-char (point-min))
980 (let (recipient-address-list)
981 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
983 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
984 recipient-address-list
)))
985 (setq smtpmail-recipient-address-list recipient-address-list
))))))
987 (defun smtpmail-do-bcc (header-end)
988 "Delete [Resent-]BCC: and their continuation lines from the header area.
989 There may be multiple BCC: lines, and each may have arbitrarily
990 many continuation lines."
991 (let ((case-fold-search t
))
993 (goto-char (point-min))
994 ;; iterate over all BCC: lines
995 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t
)
996 (delete-region (match-beginning 0)
997 (progn (forward-line 1) (point)))
998 ;; get rid of any continuation lines
999 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
1000 (replace-match ""))))))
1004 ;;; smtpmail.el ends here