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 (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")
69 (defgroup smtpmail nil
70 "SMTP protocol for sending mail."
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.")
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
)
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"))
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
)
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
104 :type
'(choice (const :tag
"Plain" nil
)
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
)
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: ")))
492 :require
(and ask-for-password
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
)))
500 (when (functionp password
)
501 (setq password
(funcall password
)))
506 ;; No mechanism, or no credentials.
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.
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
)
529 (funcall save-function
)))))
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
))
536 (funcall save-function
)))
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
545 (concat "AUTH PLAIN "
546 (base64-encode-string (concat "\0" user
"\0" password
) t
))
549 (funcall save-function
)))
551 (error "Mechanism %s not implemented" mech
)))))
553 (defun smtpmail-response-code (string)
557 (goto-char (point-min))
558 (and (re-search-forward "^\\([0-9]+\\) " nil t
)
559 (string-to-number (match-string 1))))))
561 (defun smtpmail-ok-p (response &optional code
)
563 (integerp (car response
))
564 (< (car response
) 400)
566 (= code
(car response
)))))
568 (defun smtpmail-response-text (response)
569 (mapconcat 'identity
(cdr response
) "\n"))
571 (defun smtpmail-query-smtp-server ()
572 (let ((server (read-string "Outgoing SMTP mail server: "))
573 (ports '(587 "smtp"))
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"))))
588 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
589 &optional ask-for-password
)
590 (unless smtpmail-smtp-server
591 (smtpmail-query-smtp-server))
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))
606 (supported-extensions '()))
609 ;; get or create the trace buffer
612 (format "*trace of SMTP session to %s*" host
)))
614 ;; clear the trace buffer of old output
615 (with-current-buffer process-buffer
616 (setq buffer-undo-list t
)
619 ;; open the connection to the server
622 "smtpmail" process-buffer host port
623 :type smtpmail-stream-type
625 :capability-command
(format "EHLO %s\r\n" (smtpmail-fqdn))
626 :end-of-command
"^[0-9]+ .*\r\n"
628 :always-query-capabilities t
630 (lambda (capabilities)
631 (and (string-match "-STARTTLS" capabilities
)
633 :client-certificate t
634 :use-starttls-if-possible t
))
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"))
640 ;; set the send-filter
641 (set-process-filter process
'smtpmail-process-filter
)
643 (let* ((greeting (plist-get (cdr result
) :greeting
))
644 (code (smtpmail-response-code greeting
)))
646 (throw 'done
(format "No greeting: %s" greeting
)))
648 (throw 'done
(format "Connection not allowed: %s" greeting
))))
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))
655 (let* ((capabilities (plist-get (cdr result
) :capabilities
))
656 (code (smtpmail-response-code capabilities
)))
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
666 (plist-get (cdr result
) :capabilities
)
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
)))
675 (cond ((memq (if (consp name
) (car name
) name
)
676 '(verb xvrb
8bitmime onex xone
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
))))))))
686 (setq auth-mechanisms
687 (smtpmail-try-auth-methods
688 process supported-extensions host port
691 (when (or (member 'onex supported-extensions
)
692 (member 'xone supported-extensions
))
693 (smtpmail-command-or-throw process
(format "ONEX")))
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")))
700 (when (member 'xusr supported-extensions
)
701 (smtpmail-command-or-throw process
(format "XUSR")))
703 ;; MAIL FROM:<sender>
705 (if (or (member 'size supported-extensions
)
706 (assoc 'size supported-extensions
))
708 (with-current-buffer smtpmail-text-buffer
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)))))
716 (if (member '8bitmime supported-extensions
)
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
732 (smtpmail-send-command
733 process
(format "MAIL FROM:<%s>%s%s"
734 envelope-from size-part body-part
))
736 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
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
)
749 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
751 ;; Return the error code.
753 (smtpmail-response-text result
)))))
755 ;; RCPT TO:<recipient>
757 (while (not (null (nth n recipient
)))
758 (smtpmail-send-command
759 process
(format "RCPT TO:<%s>"
760 (smtpmail-maybe-append-domain
763 ((smtpmail-ok-p (setq result
(smtpmail-read-response process
)))
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
772 (smtpmail-send-command process
"QUIT")
773 (smtpmail-read-response process
)
774 (delete-process process
)
777 (smtpmail-via-smtp recipient smtpmail-text-buffer t
)))
779 ;; Return the error code.
781 (smtpmail-response-text result
))))
784 ;; Send the contents.
785 (smtpmail-command-or-throw process
"DATA")
786 (smtpmail-send-data process smtpmail-text-buffer
)
788 (smtpmail-command-or-throw 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
)))))))
801 (defun smtpmail-process-filter (process output
)
802 (with-current-buffer (process-buffer process
)
803 (goto-char (point-max))
806 (defun smtpmail-read-response (process)
807 (let ((case-fold-search nil
)
808 (response-strings nil
)
809 (response-continue t
)
810 (return-value '(nil ()))
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
))
818 (accept-process-output process
)
819 (goto-char smtpmail-read-point
))
821 (setq match-end
(point))
822 (setq response-strings
823 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
826 (goto-char smtpmail-read-point
)
827 (if (looking-at "[0-9]+ ")
828 (let ((begin (match-beginning 0))
830 (if smtpmail-debug-info
831 (message "%s" (car response-strings
)))
833 (setq smtpmail-read-point match-end
)
835 ;; ignore lines that start with "0"
836 (if (looking-at "0[0-9]+ ")
838 (setq response-continue nil
)
840 (cons (string-to-number
841 (buffer-substring begin end
))
842 (nreverse response-strings
)))))
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
))
850 (setq smtpmail-read-point match-end
)
851 (setq response-continue nil
)
853 (cons nil
(nreverse response-strings
)))))))
854 (setq smtpmail-read-point match-end
))
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"))
867 (defun smtpmail-send-data-1 (process data
)
868 (goto-char (point-max))
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
))))
875 (if smtpmail-debug-info
876 (insert data
"\r\n"))
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"))
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)))
893 (with-current-buffer buffer
894 (progress-reporter-update pr
(point))
895 (setq sending-data
(buffer-substring (point-at-bol) (point-at-eol)))
897 (setq data-continue
(not (eobp))))
898 (smtpmail-send-data-1 process sending-data
))
899 (progress-reporter-done pr
)))
901 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
902 "Get address list suitable for smtp RCPT TO: <address>."
904 (with-current-buffer smtpmail-address-buffer
906 (let ((case-fold-search t
)
907 (simple-address-list "")
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.
916 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
918 "^Resent-\\(to\\|cc\\|bcc\\):"
919 "^\\(To:\\|Cc:\\|Bcc:\\)")))
921 (while (re-search-forward addr-regexp header-end t
)
923 (setq this-line
(match-beginning 0))
925 ;; get any continuation lines
926 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
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
)))))
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
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 " "))
942 (goto-char (point-min))
943 (let (recipient-address-list)
944 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
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
))))))
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
))
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 ""))))))
967 ;;; smtpmail.el ends here