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-local-domain nil
90 "Local domain name without a host name.
91 If the function `system-name' returns the full internet address,
92 don't define this value."
93 :type
'(choice (const nil
) string
)
96 (defcustom smtpmail-stream-type nil
97 "Connection type SMTP connections.
98 This may be either nil (possibly upgraded to STARTTLS if
99 possible), or `starttls' (refuse to send if STARTTLS isn't
100 available), or `plain' (never use STARTTLS).."
103 :type
'(choice (const :tag
"Possibly upgrade to STARTTLS" nil
)
104 (const :tag
"Always use STARTTLS" starttls
)
105 (const :tag
"Never use STARTTLS" plain
)))
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.)
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
)
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
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."
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. "
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'."
145 (defcustom smtpmail-queue-dir
"~/Mail/queued-mail/"
146 "Directory where `smtpmail.el' stores queued mail."
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."
158 (defcustom smtpmail-queue-index-file
"index"
159 "File name of queued mail index.
160 This is relative to `smtpmail-queue-dir'."
164 ;; End of customizable variables.
167 (defvar smtpmail-address-buffer
)
168 (defvar smtpmail-recipient-address-list
)
170 (defvar smtpmail-queue-counter
0)
172 ;; Buffer-local variable.
173 (defvar smtpmail-read-point
)
175 (defconst smtpmail-auth-supported
'(cram-md5 plain login
)
176 "List of supported SMTP AUTH mechanisms.
177 The list is in preference order.")
179 (defvar smtpmail-mail-address nil
180 "Value to use for envelope-from address for mail from ambient buffer.")
183 (defun smtpmail-send-it ()
184 (let ((errbuf (if mail-interactive
185 (generate-new-buffer " smtpmail errors")
187 (tembuf (generate-new-buffer " smtpmail temp"))
188 (case-fold-search nil
)
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))
197 (smtpmail-code-conv-from
198 (if enable-multibyte-characters
199 (let ((sendmail-coding-system smtpmail-code-conv-from
))
200 (select-message-coding-system)))))
202 (with-current-buffer tembuf
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
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)
214 ;; Change header-delimiter to be what sendmail expects.
215 (mail-sendmail-undelimit-header)
216 (setq delimline
(point-marker))
217 ;; (sendmail-synch-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
)
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
))
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^-~]"
253 ;; Quote fullname, escaping specials.
254 (goto-char fullname-start
)
256 (while (re-search-forward "[\"\\]"
258 (replace-match "\\\\\\&" t
))
260 (insert " <" login
">\n"))
261 ((eq mail-from-style
'parens
)
262 (insert "From: " login
" (")
263 (let ((fullname-start (point)))
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 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
278 (replace-match "\\1(\\3)" t
)
279 (goto-char fullname-start
))))
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
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
300 (coding-system-get smtpmail-code-conv-from
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
)
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
)))
320 (with-current-buffer errbuf
322 ;; Encode the header according to RFC2047.
323 (mail-encode-header (point-min) delimline
)
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
)
330 (smtpmail-do-bcc delimline
)
332 (if (not smtpmail-queue-mail
)
333 (if (not (null smtpmail-recipient-address-list
))
336 smtpmail-recipient-address-list tembuf
))
337 (error "Sending failed: %s" result
))
338 (error "Sending failed; no recipients"))
342 (format-time-string "%Y-%m-%d_%H:%M:%S")
343 (setq smtpmail-queue-counter
344 (1+ smtpmail-queue-counter
)))
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
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
359 (coding-system-change-eol-conversion
360 (or smtpmail-code-conv-from
'undecided
)
363 (insert-buffer-substring tembuf
)
364 (write-file file-data
)
365 (set-buffer buffer-elisp
)
368 "(setq smtpmail-recipient-address-list '"
369 (prin1-to-string smtpmail-recipient-address-list
)
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)
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
))))
383 (kill-buffer errbuf
)))))
386 (defun smtpmail-send-queued-mail ()
387 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
390 ;; Get index, get first mail, send it, update index, get second
391 ;; mail, send it, etc...
393 (qfile (expand-file-name smtpmail-queue-index-file
396 (insert-file-contents qfile
)
397 (goto-char (point-min))
399 (setq file-msg
(buffer-substring (point) (line-end-position)))
401 ;; Insert the message literally: it is already encoded as per
402 ;; the MIME headers, and code conversions might guess the
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))
410 (if (not (null smtpmail-recipient-address-list
))
411 (when (setq result
(smtpmail-via-smtp
412 smtpmail-recipient-address-list
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
))))
421 (defun smtpmail-fqdn ()
422 (if smtpmail-local-domain
423 (concat (system-name) "." smtpmail-local-domain
)
426 (defsubst smtpmail-cred-server
(cred)
429 (defsubst smtpmail-cred-port
(cred)
432 (defsubst smtpmail-cred-key
(cred)
435 (defsubst smtpmail-cred-user
(cred)
438 (defsubst smtpmail-cred-cert
(cred)
441 (defsubst smtpmail-cred-passwd
(cred)
444 (defun smtpmail-find-credentials (cred server port
)
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
))))))
452 (defun smtpmail-maybe-append-domain (recipient)
453 (if (or (not smtpmail-sendto-domain
)
454 (string-match "@" recipient
))
456 (concat recipient
"@" smtpmail-sendto-domain
)))
458 (defun smtpmail-intersection (list1 list2
)
461 (when (memq el2 list1
)
465 ;; `password-read' autoloads password-cache.
466 (declare-function password-cache-add
"password-cache" (key password
))
468 (defun smtpmail-command-or-throw (process string
&optional code
)
470 (smtpmail-send-command process string
)
471 (unless (smtpmail-ok-p (setq ret
(smtpmail-read-response process
))
473 (throw 'done
(format "%s in response to %s"
474 (smtpmail-response-text ret
)
478 (defun smtpmail-try-auth-methods (process supported-extensions host port
479 &optional ask-for-password
)
484 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions
)))
485 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported
)))
486 (auth-source-creation-prompts
487 '((user .
"SMTP user at %h: ")
488 (secret .
"SMTP password for %u@%h: ")))
494 :require
(and ask-for-password
496 :create ask-for-password
)))
497 (user (plist-get auth-info
:user
))
498 (password (plist-get auth-info
:secret
))
499 (save-function (and ask-for-password
500 (plist-get auth-info
:save-function
)))
504 ;; The user has stored the user name, but not the password, so
505 ;; ask for the password, even if we're not forcing that through
506 ;; `ask-for-password'.
513 :require
'(:user
:secret
)
515 password
(plist-get auth-info
:secret
)))
516 (when (functionp password
)
517 (setq password
(funcall password
)))
522 ;; No mechanism, or no credentials.
525 (setq ret
(smtpmail-command-or-throw process
"AUTH CRAM-MD5"))
526 (when (eq (car ret
) 334)
527 (let* ((challenge (substring (cadr ret
) 4))
528 (decoded (base64-decode-string challenge
))
529 (hash (rfc2104-hash 'md5
64 16 password decoded
))
530 (response (concat user
" " hash
))
531 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
532 ;; SMTP auth fails because the SMTP server identifies
533 ;; only the first part of the string (delimited by
534 ;; new line characters) as a response from the
535 ;; client, and the rest as distinct commands.
537 ;; In my case, the response string is 80 characters
538 ;; long. Without the no-line-break option for
539 ;; `base64-encode-string', only the first 76 characters
540 ;; are taken as a response to the server, and the
541 ;; authentication fails.
542 (encoded (base64-encode-string response t
)))
543 (smtpmail-command-or-throw process encoded
)
545 (funcall save-function
)))))
547 (smtpmail-command-or-throw process
"AUTH LOGIN")
548 (smtpmail-command-or-throw
549 process
(base64-encode-string user t
))
550 (smtpmail-command-or-throw process
(base64-encode-string password t
))
552 (funcall save-function
)))
554 ;; We used to send an empty initial request, and wait for an
555 ;; empty response, and then send the password, but this
556 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
557 ;; is not sent if the server did not advertise AUTH PLAIN in
558 ;; the EHLO response. See RFC 2554 for more info.
559 (smtpmail-command-or-throw
561 (concat "AUTH PLAIN "
562 (base64-encode-string (concat "\0" user
"\0" password
) t
))
565 (funcall save-function
)))
567 (error "Mechanism %s not implemented" mech
)))))
569 (defun smtpmail-response-code (string)
573 (goto-char (point-min))
574 (and (re-search-forward "^\\([0-9]+\\) " nil t
)
575 (string-to-number (match-string 1))))))
577 (defun smtpmail-ok-p (response &optional code
)
579 (integerp (car response
))
580 (< (car response
) 400)
582 (= code
(car response
)))))
584 (defun smtpmail-response-text (response)
585 (mapconcat 'identity
(cdr response
) "\n"))
587 (defun smtpmail-query-smtp-server ()
588 (let ((server (read-string "Outgoing SMTP mail server: "))
589 (ports '("smtp" 587))
591 (when (and smtpmail-smtp-server
592 (not (member smtpmail-smtp-server ports
)))
593 (push smtpmail-smtp-server ports
))
594 (while (and (not smtpmail-smtp-server
)
595 (setq port
(pop ports
)))
596 (when (setq stream
(ignore-errors
597 (open-network-stream "smtp" nil server port
)))
598 (customize-save-variable 'smtpmail-smtp-server server
)
599 (customize-save-variable 'smtpmail-smtp-service port
)
600 (delete-process stream
)))
601 (unless smtpmail-smtp-server
602 (error "Couldn't contact an SMTP server"))))
604 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
605 &optional ask-for-password
)
606 (unless smtpmail-smtp-server
607 (smtpmail-query-smtp-server))
609 (host (or smtpmail-smtp-server
610 (error "`smtpmail-smtp-server' not defined")))
611 (port smtpmail-smtp-service
)
612 ;; `smtpmail-mail-address' should be set to the appropriate
613 ;; buffer-local value by the caller, but in case not:
614 (envelope-from (or smtpmail-mail-address
615 (and mail-specify-envelope-from
616 (mail-envelope-from))
618 (coding-system-for-read 'binary
)
619 (coding-system-for-write 'binary
)
624 (supported-extensions '()))
627 ;; get or create the trace buffer
630 (format "*trace of SMTP session to %s*" host
)))
632 ;; clear the trace buffer of old output
633 (with-current-buffer process-buffer
634 (setq buffer-undo-list t
)
637 ;; open the connection to the server
640 "smtpmail" process-buffer host port
641 :type smtpmail-stream-type
643 :capability-command
(format "EHLO %s\r\n" (smtpmail-fqdn))
644 :end-of-command
"^[0-9]+ .*\r\n"
646 :always-query-capabilities t
648 (lambda (capabilities)
649 (and (string-match "-STARTTLS" capabilities
)
651 :client-certificate t
652 :use-starttls-if-possible t
))
654 ;; If we couldn't access the server at all, we give up.
655 (unless (setq process
(car result
))
656 (throw 'done
(if (plist-get (cdr result
) :error
)
657 (plist-get (cdr result
) :error
)
658 "Unable to contact server")))
660 ;; set the send-filter
661 (set-process-filter process
'smtpmail-process-filter
)
663 (let* ((greeting (plist-get (cdr result
) :greeting
))
664 (code (smtpmail-response-code greeting
)))
666 (throw 'done
(format "No greeting: %s" greeting
)))
668 (throw 'done
(format "Connection not allowed: %s" greeting
))))
670 (with-current-buffer process-buffer
671 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
672 (make-local-variable 'smtpmail-read-point
)
673 (setq smtpmail-read-point
(point-min))
675 (let* ((capabilities (plist-get (cdr result
) :capabilities
))
676 (code (smtpmail-response-code capabilities
)))
679 ;; The server didn't accept EHLO, so we fall back on HELO.
680 (smtpmail-command-or-throw
681 process
(format "HELO %s" (smtpmail-fqdn)))
682 ;; EHLO was successful, so we parse the extensions.
683 (dolist (line (delete
686 (plist-get (cdr result
) :capabilities
)
689 (with-case-table ascii-case-table
690 (mapcar (lambda (s) (intern (downcase s
)))
691 (split-string (substring line
4) "[ ]")))))
692 (when (= (length name
) 1)
693 (setq name
(car name
)))
695 (cond ((memq (if (consp name
) (car name
) name
)
696 '(verb xvrb
8bitmime onex xone
700 auth
=login auth starttls
))
701 (setq supported-extensions
702 (cons name supported-extensions
)))
703 (smtpmail-warn-about-unknown-extensions
704 (message "Unknown extension %s" name
))))))))
706 (setq auth-mechanisms
707 (smtpmail-try-auth-methods
708 process supported-extensions host port
711 (when (or (member 'onex supported-extensions
)
712 (member 'xone supported-extensions
))
713 (smtpmail-command-or-throw process
(format "ONEX")))
715 (when (and smtpmail-debug-verb
716 (or (member 'verb supported-extensions
)
717 (member 'xvrb supported-extensions
)))
718 (smtpmail-command-or-throw process
(format "VERB")))
720 (when (member 'xusr supported-extensions
)
721 (smtpmail-command-or-throw process
(format "XUSR")))
723 ;; MAIL FROM:<sender>
725 (if (or (member 'size supported-extensions
)
726 (assoc 'size supported-extensions
))
728 (with-current-buffer smtpmail-text-buffer
730 (+ (- (point-max) (point-min))
731 ;; Add one byte for each change-of-line
732 ;; because of CR-LF representation:
733 (count-lines (point-min) (point-max)))))
736 (if (member '8bitmime supported-extensions
)
738 ;; Code should be added here that transforms
739 ;; the contents of the message buffer into
740 ;; something the receiving SMTP can handle.
741 ;; For a receiver that supports 8BITMIME, this
742 ;; may mean converting BINARY to BASE64, or
743 ;; adding Content-Transfer-Encoding and the
744 ;; other MIME headers. The code should also
745 ;; return an indication of what encoding the
746 ;; message buffer is now, i.e. ASCII or
752 (smtpmail-send-command
753 process
(format "MAIL FROM:<%s>%s%s"
754 envelope-from size-part body-part
))
756 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
759 ((and auth-mechanisms
760 (not ask-for-password
)
761 (= (car result
) 530))
762 ;; We got a "530 auth required", so we close and try
763 ;; again, this time asking the user for a password.
764 (smtpmail-send-command process
"QUIT")
765 (smtpmail-read-response process
)
766 (delete-process process
)
769 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
771 ;; Return the error code.
773 (smtpmail-response-text result
)))))
775 ;; RCPT TO:<recipient>
777 (while (not (null (nth n recipient
)))
778 (smtpmail-send-command
779 process
(format "RCPT TO:<%s>"
780 (smtpmail-maybe-append-domain
783 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
786 ((and auth-mechanisms
787 (not ask-for-password
)
788 (>= (car result
) 550)
789 (<= (car result
) 554))
790 ;; We got a "550 relay not permitted" (or the like),
791 ;; and the server accepts credentials, so we try
792 ;; again, but ask for a password first.
793 (smtpmail-send-command process
"QUIT")
794 (smtpmail-read-response process
)
795 (delete-process process
)
798 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
800 ;; Return the error code.
802 (smtpmail-response-text result
))))
805 ;; Send the contents.
806 (smtpmail-command-or-throw process
"DATA")
807 (smtpmail-send-data process smtpmail-text-buffer
)
809 (smtpmail-command-or-throw process
".")
813 (buffer-live-p process-buffer
))
814 (with-current-buffer (process-buffer process
)
815 (smtpmail-send-command process
"QUIT")
816 (smtpmail-read-response process
)
817 (delete-process process
)
818 (unless smtpmail-debug-info
819 (kill-buffer process-buffer
)))))))
822 (defun smtpmail-process-filter (process output
)
823 (with-current-buffer (process-buffer process
)
824 (goto-char (point-max))
827 (defun smtpmail-read-response (process)
828 (let ((case-fold-search nil
)
829 (response-strings nil
)
830 (response-continue t
)
831 (return-value '(nil ()))
834 (while response-continue
835 (goto-char smtpmail-read-point
)
836 (while (not (search-forward "\r\n" nil t
))
837 (unless (memq (process-status process
) '(open run
))
839 (accept-process-output process
)
840 (goto-char smtpmail-read-point
))
842 (setq match-end
(point))
843 (setq response-strings
844 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
847 (goto-char smtpmail-read-point
)
848 (if (looking-at "[0-9]+ ")
849 (let ((begin (match-beginning 0))
851 (if smtpmail-debug-info
852 (message "%s" (car response-strings
)))
854 (setq smtpmail-read-point match-end
)
856 ;; ignore lines that start with "0"
857 (if (looking-at "0[0-9]+ ")
859 (setq response-continue nil
)
861 (cons (string-to-number
862 (buffer-substring begin end
))
863 (nreverse response-strings
)))))
865 (if (looking-at "[0-9]+-")
866 (progn (if smtpmail-debug-info
867 (message "%s" (car response-strings
)))
868 (setq smtpmail-read-point match-end
)
869 (setq response-continue t
))
871 (setq smtpmail-read-point match-end
)
872 (setq response-continue nil
)
874 (cons nil
(nreverse response-strings
)))))))
875 (setq smtpmail-read-point match-end
))
879 (defun smtpmail-send-command (process command
)
880 (goto-char (point-max))
881 (if (= (aref command
0) ?P
)
882 (insert "PASS <omitted>\r\n")
883 (insert command
"\r\n"))
884 (setq smtpmail-read-point
(point))
885 (process-send-string process command
)
886 (process-send-string process
"\r\n"))
888 (defun smtpmail-send-data-1 (process data
)
889 (goto-char (point-max))
891 (if (and (multibyte-string-p data
)
892 smtpmail-code-conv-from
)
893 (setq data
(string-as-multibyte
894 (encode-coding-string data smtpmail-code-conv-from
))))
896 (if smtpmail-debug-info
897 (insert data
"\r\n"))
899 (setq smtpmail-read-point
(point))
900 ;; Escape "." at start of a line
901 (if (eq (string-to-char data
) ?.
)
902 (process-send-string process
"."))
903 (process-send-string process data
)
904 (process-send-string process
"\r\n"))
906 (defun smtpmail-send-data (process buffer
)
907 (let ((data-continue t
) sending-data
908 (pr (with-current-buffer buffer
909 (make-progress-reporter "Sending email"
910 (point-min) (point-max)))))
911 (with-current-buffer buffer
912 (goto-char (point-min)))
914 (with-current-buffer buffer
915 (progress-reporter-update pr
(point))
916 (setq sending-data
(buffer-substring (point-at-bol) (point-at-eol)))
918 (setq data-continue
(not (eobp))))
919 (smtpmail-send-data-1 process sending-data
))
920 (progress-reporter-done pr
)))
922 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
923 "Get address list suitable for smtp RCPT TO: <address>."
925 (with-current-buffer smtpmail-address-buffer
927 (let ((case-fold-search t
)
928 (simple-address-list "")
932 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
933 (goto-char (point-min))
934 ;; RESENT-* fields should stop processing of regular fields.
937 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
939 "^Resent-\\(to\\|cc\\|bcc\\):"
940 "^\\(To:\\|Cc:\\|Bcc:\\)")))
942 (while (re-search-forward addr-regexp header-end t
)
944 (setq this-line
(match-beginning 0))
946 ;; get any continuation lines
947 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
949 (setq this-line-end
(point-marker))
950 (setq simple-address-list
951 (concat simple-address-list
" "
952 (mail-strip-quoted-names (buffer-substring this-line this-line-end
)))))
954 (insert " " simple-address-list
"\n")
955 (subst-char-in-region (point-min) (point-max) 10 ? t
) ; newline --> blank
956 (subst-char-in-region (point-min) (point-max) ?
, ? t
) ; comma --> blank
957 (subst-char-in-region (point-min) (point-max) 9 ? t
) ; tab --> blank
959 (goto-char (point-min))
960 ;; tidyness in case hook is not robust when it looks at this
961 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
963 (goto-char (point-min))
964 (let (recipient-address-list)
965 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
967 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
968 recipient-address-list
)))
969 (setq smtpmail-recipient-address-list recipient-address-list
))))))
971 (defun smtpmail-do-bcc (header-end)
972 "Delete [Resent-]BCC: and their continuation lines from the header area.
973 There may be multiple BCC: lines, and each may have arbitrarily
974 many continuation lines."
975 (let ((case-fold-search t
))
977 (goto-char (point-min))
978 ;; iterate over all BCC: lines
979 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t
)
980 (delete-region (match-beginning 0)
981 (progn (forward-line 1) (point)))
982 ;; get rid of any continuation lines
983 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
984 (replace-match ""))))))
988 ;;; smtpmail.el ends here