1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996, 2001, 2002, 2003 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 2, or (at your option)
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; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
33 ;; Send Mail to smtp host from smtpmail temp buffer.
35 ;; Please add these lines in your .emacs(_emacs) or use customize.
37 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
38 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
39 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
40 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
41 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
42 ;;(setq smtpmail-debug-info t) ; only to debug problems
43 ;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
44 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
45 ;;(setq smtpmail-starttls-credentials
46 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
48 ;; To queue mail, set smtpmail-queue-mail to t and use
49 ;; smtpmail-send-queued-mail to send.
51 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
52 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
53 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
54 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
57 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
58 ;; Authentication by the AUTH mechanism.
59 ;; See http://www.ietf.org/rfc/rfc2554.txt
61 ;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
62 ;; STARTTLS. Requires external program
63 ;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
64 ;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
69 (autoload 'starttls-open-stream
"starttls")
70 (autoload 'starttls-negotiate
"starttls")
71 (autoload 'mail-strip-quoted-names
"mail-utils")
72 (autoload 'message-make-date
"message")
73 (autoload 'message-make-message-id
"message")
74 (autoload 'rfc2104-hash
"rfc2104")
75 (autoload 'netrc-parse
"netrc")
76 (autoload 'netrc-machine
"netrc")
77 (autoload 'netrc-get
"netrc")
80 (defgroup smtpmail nil
81 "SMTP protocol for sending mail."
85 (defcustom smtpmail-default-smtp-server nil
86 "*Specify default SMTP server.
87 This only has effect if you specify it before loading the smtpmail library."
88 :type
'(choice (const nil
) string
)
91 (defcustom smtpmail-smtp-server
92 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server
)
93 "*The name of the host running SMTP server."
94 :type
'(choice (const nil
) string
)
97 (defcustom smtpmail-smtp-service
25
98 "*SMTP service port number.
99 The default value would be \"smtp\" or 25 ."
100 :type
'(choice (integer :tag
"Port") (string :tag
"Service"))
103 (defcustom smtpmail-local-domain nil
104 "*Local domain name without a host name.
105 If the function (system-name) returns the full internet address,
106 don't define this value."
107 :type
'(choice (const nil
) string
)
110 (defcustom smtpmail-sendto-domain nil
111 "*Local domain name without a host name.
112 This is appended (with an @-sign) to any specified recipients which do
113 not include an @-sign, so that each RCPT TO address is fully qualified.
114 \(Some configurations of sendmail require this.)
116 Don't bother to set this unless you have get an error like:
117 Sending failed; SMTP protocol error
118 when sending mail, and the *trace of SMTP session to <somewhere>*
119 buffer includes an exchange like:
121 501 <someone>: recipient address must contain a domain
123 :type
'(choice (const nil
) string
)
126 (defcustom smtpmail-debug-info nil
127 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
128 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
133 (defcustom smtpmail-debug-verb nil
134 "Whether this library sends the SMTP VERB command or not.
135 The commands enables verbose information from the SMTP server."
139 (defcustom smtpmail-code-conv-from nil
;; *junet*
140 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
144 (defcustom smtpmail-queue-mail nil
145 "*Specify if mail is queued (if t) or sent immediately (if nil).
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-auth-credentials
"~/.authinfo"
157 "Specify username and password for servers, directly or via .netrc file.
158 This variable can either be a filename pointing to a file in netrc(5)
159 format, or list of four-element lists that contain, in order,
160 `servername' (a string), `port' (an integer), `user' (a string) and
161 `password' (a string, or nil to query the user when needed). If you
162 need to enter a `realm' too, add it to the user string, so that it
163 looks like `user@realm'."
165 (repeat (list (string :tag
"Server")
166 (integer :tag
"Port")
167 (string :tag
"Username")
168 (choice (const :tag
"Query when needed" nil
)
169 (string :tag
"Password")))))
173 (defcustom smtpmail-starttls-credentials
'(("" 25 "" ""))
174 "Specify STARTTLS keys and certificates for servers.
175 This is a list of four-element list with `servername' (a string),
176 `port' (an integer), `key' (a filename) and `certificate' (a filename)."
177 :type
'(repeat (list (string :tag
"Server")
178 (integer :tag
"Port")
180 (file :tag
"Certificate")))
184 (defcustom smtpmail-warn-about-unknown-extensions nil
185 "*If set, print warnings about unknown SMTP extensions.
186 This is mainly useful for development purposes, to learn about
187 new SMTP extensions that might be useful to support."
192 (defvar smtpmail-queue-index-file
"index"
193 "File name of queued mail index,
194 This is relative to `smtpmail-queue-dir'.")
196 (defvar smtpmail-address-buffer
)
197 (defvar smtpmail-recipient-address-list
)
199 (defvar smtpmail-queue-counter
0)
201 ;; Buffer-local variable.
202 (defvar smtpmail-read-point
)
204 (defvar smtpmail-queue-index
(concat smtpmail-queue-dir
205 smtpmail-queue-index-file
))
207 (defconst smtpmail-auth-supported
'(cram-md5 login
)
208 "List of supported SMTP AUTH mechanisms.")
214 (defvar smtpmail-mail-address nil
215 "Value to use for envelope-from address for mail from ambient buffer.")
218 (defun smtpmail-send-it ()
219 (let ((errbuf (if mail-interactive
220 (generate-new-buffer " smtpmail errors")
222 (tembuf (generate-new-buffer " smtpmail temp"))
223 (case-fold-search nil
)
225 (mailbuf (current-buffer))
226 ;; Examine this variable now, so that
227 ;; local binding in the mail buffer will take effect.
228 (smtpmail-mail-address
229 (or (and mail-specify-envelope-from
(mail-envelope-from))
231 (smtpmail-code-conv-from
232 (if enable-multibyte-characters
233 (let ((sendmail-coding-system smtpmail-code-conv-from
))
234 (select-message-coding-system)))))
239 (insert-buffer-substring mailbuf
)
240 (goto-char (point-max))
241 ;; require one newline at the end.
242 (or (= (preceding-char) ?
\n)
244 ;; Change header-delimiter to be what sendmail expects.
245 (mail-sendmail-undelimit-header)
246 (setq delimline
(point-marker))
247 ;; (sendmail-synch-aliases)
249 (expand-mail-aliases (point-min) delimline
))
250 (goto-char (point-min))
251 ;; ignore any blank lines in the header
252 (while (and (re-search-forward "\n\n\n*" delimline t
)
253 (< (point) delimline
))
254 (replace-match "\n"))
255 (let ((case-fold-search t
))
256 ;; We used to process Resent-... headers here,
257 ;; but it was not done properly, and the job
258 ;; is done correctly in smtpmail-deduce-address-list.
259 ;; Don't send out a blank subject line
260 (goto-char (point-min))
261 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
263 ;; This one matches a Subject just before the header delimiter.
264 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
265 (= (match-end 0) delimline
))
267 ;; Put the "From:" field in unless for some odd reason
268 ;; they put one in themselves.
269 (goto-char (point-min))
270 (if (not (re-search-forward "^From:" delimline t
))
271 (let* ((login smtpmail-mail-address
)
272 (fullname (user-full-name)))
273 (cond ((eq mail-from-style
'angles
)
274 (insert "From: " fullname
)
275 (let ((fullname-start (+ (point-min) 6))
276 (fullname-end (point-marker)))
277 (goto-char fullname-start
)
278 ;; Look for a character that cannot appear unquoted
279 ;; according to RFC 822.
280 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
283 ;; Quote fullname, escaping specials.
284 (goto-char fullname-start
)
286 (while (re-search-forward "[\"\\]"
288 (replace-match "\\\\\\&" t
))
290 (insert " <" login
">\n"))
291 ((eq mail-from-style
'parens
)
292 (insert "From: " login
" (")
293 (let ((fullname-start (point)))
295 (let ((fullname-end (point-marker)))
296 (goto-char fullname-start
)
297 ;; RFC 822 says \ and nonmatching parentheses
298 ;; must be escaped in comments.
299 ;; Escape every instance of ()\ ...
300 (while (re-search-forward "[()\\]" fullname-end
1)
301 (replace-match "\\\\\\&" t
))
302 ;; ... then undo escaping of matching parentheses,
303 ;; including matching nested parentheses.
304 (goto-char fullname-start
)
305 (while (re-search-forward
306 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
308 (replace-match "\\1(\\3)" t
)
309 (goto-char fullname-start
))))
311 ((null mail-from-style
)
312 (insert "From: " login
"\n")))))
313 ;; Insert a `Message-Id:' field if there isn't one yet.
314 (goto-char (point-min))
315 (unless (re-search-forward "^Message-Id:" delimline t
)
316 (insert "Message-Id: " (message-make-message-id) "\n"))
317 ;; Insert a `Date:' field if there isn't one yet.
318 (goto-char (point-min))
319 (unless (re-search-forward "^Date:" delimline t
)
320 (insert "Date: " (message-make-date) "\n"))
321 ;; Insert an extra newline if we need it to work around
322 ;; Sun's bug that swallows newlines.
323 (goto-char (1+ delimline
))
324 (if (eval mail-mailer-swallows-blank-line
)
326 ;; Find and handle any FCC fields.
327 (goto-char (point-min))
328 (if (re-search-forward "^FCC:" delimline t
)
329 (mail-do-fcc delimline
))
331 (with-current-buffer errbuf
336 (setq smtpmail-address-buffer
(generate-new-buffer "*smtp-mail*"))
337 (setq smtpmail-recipient-address-list
338 (smtpmail-deduce-address-list tembuf
(point-min) delimline
))
339 (kill-buffer smtpmail-address-buffer
)
341 (smtpmail-do-bcc delimline
)
343 (if (not smtpmail-queue-mail
)
344 (if (not (null smtpmail-recipient-address-list
))
345 (if (not (smtpmail-via-smtp
346 smtpmail-recipient-address-list tembuf
))
347 (error "Sending failed; SMTP protocol error"))
348 (error "Sending failed; no recipients"))
352 (format-time-string "%Y-%m-%d_%H:%M:%S")
353 (setq smtpmail-queue-counter
354 (1+ smtpmail-queue-counter
)))
356 (file-data (convert-standard-filename file-data
))
357 (file-elisp (concat file-data
".el"))
358 (buffer-data (create-file-buffer file-data
))
359 (buffer-elisp (create-file-buffer file-elisp
))
360 (buffer-scratch "*queue-mail*"))
361 (unless (file-exists-p smtpmail-queue-dir
)
362 (make-directory smtpmail-queue-dir t
))
363 (with-current-buffer buffer-data
365 (insert-buffer tembuf
)
366 (write-file file-data
)
367 (set-buffer buffer-elisp
)
370 "(setq smtpmail-recipient-address-list '"
371 (prin1-to-string smtpmail-recipient-address-list
)
373 (write-file file-elisp
)
374 (set-buffer (generate-new-buffer buffer-scratch
))
375 (insert (concat file-data
"\n"))
376 (append-to-file (point-min)
378 smtpmail-queue-index
)
380 (kill-buffer buffer-scratch
)
381 (kill-buffer buffer-data
)
382 (kill-buffer buffer-elisp
))))
385 (kill-buffer errbuf
)))))
388 (defun smtpmail-send-queued-mail ()
389 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
392 ;;; Get index, get first mail, send it, update index, get second
393 ;;; mail, send it, etc...
395 (insert-file-contents smtpmail-queue-index
)
396 (beginning-of-buffer)
398 (setq file-msg
(buffer-substring (point) (line-end-position)))
400 ;; Insert the message literally: it is already encoded as per
401 ;; the MIME headers, and code conversions might guess the
404 (let ((coding-system-for-read 'no-conversion
))
405 (insert-file-contents file-msg
))
406 (let ((smtpmail-mail-address
407 (or (and mail-specify-envelope-from
(mail-envelope-from))
409 (if (not (null smtpmail-recipient-address-list
))
410 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
412 (error "Sending failed; SMTP protocol error"))
413 (error "Sending failed; no recipients"))))
414 (delete-file file-msg
)
415 (delete-file (concat file-msg
".el"))
416 (delete-region (point-at-bol) (point-at-bol 2)))
417 (write-region (point-min) (point-max) smtpmail-queue-index
))))
419 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
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 (defun smtpmail-open-stream (process-buffer host port
)
466 (let ((cred (smtpmail-find-credentials
467 smtpmail-starttls-credentials host port
)))
468 (if (null (and cred
(condition-case ()
471 (call-process starttls-program
))
474 (open-network-stream "SMTP" process-buffer host port
)
475 (let* ((cred-key (smtpmail-cred-key cred
))
476 (cred-cert (smtpmail-cred-cert cred
))
478 (when (and (stringp cred-key
) (stringp cred-cert
)
480 (setq cred-key
(expand-file-name cred-key
)))
482 (setq cred-cert
(expand-file-name cred-cert
))))
483 (list "--key-file" cred-key
"--cert-file" cred-cert
))))
484 (starttls-open-stream "SMTP" process-buffer host port
)))))
486 (defun smtpmail-try-auth-methods (process supported-extensions host port
)
487 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions
)))
488 (mech (car (smtpmail-intersection smtpmail-auth-supported mechs
)))
489 (cred (if (stringp smtpmail-auth-credentials
)
490 (let* ((netrc (netrc-parse smtpmail-auth-credentials
))
491 (hostentry (netrc-machine
492 netrc host
(format "%s" (or port
"smtp"))
496 (netrc-get hostentry
"login")
497 (netrc-get hostentry
"password"))))
498 (smtpmail-find-credentials
499 smtpmail-auth-credentials host port
)))
501 (or (smtpmail-cred-passwd cred
)
503 (format "SMTP password for %s:%s: "
504 (smtpmail-cred-server cred
)
505 (smtpmail-cred-port cred
))))))
510 (smtpmail-send-command process
(format "AUTH %s" mech
))
511 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
512 (not (integerp (car ret
)))
515 (when (eq (car ret
) 334)
516 (let* ((challenge (substring (cadr ret
) 4))
517 (decoded (base64-decode-string challenge
))
518 (hash (rfc2104-hash 'md5
64 16 passwd decoded
))
519 (response (concat (smtpmail-cred-user cred
) " " hash
))
520 (encoded (base64-encode-string response
)))
521 (smtpmail-send-command process
(format "%s" encoded
))
522 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
523 (not (integerp (car ret
)))
525 (throw 'done nil
)))))
527 (smtpmail-send-command process
"AUTH LOGIN")
528 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
529 (not (integerp (car ret
)))
532 (smtpmail-send-command
533 process
(base64-encode-string (smtpmail-cred-user cred
)))
534 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
535 (not (integerp (car ret
)))
538 (smtpmail-send-command process
(base64-encode-string passwd
))
539 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
540 (not (integerp (car ret
)))
544 (error "Mechanism %s not implemented" mech
)))
545 ;; Remember the password.
546 (when (and (not (stringp smtpmail-auth-credentials
))
547 (null (smtpmail-cred-passwd cred
)))
548 (setcar (cdr (cdr (cdr cred
))) passwd
)))))
550 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
)
552 (host (or smtpmail-smtp-server
553 (error "`smtpmail-smtp-server' not defined")))
554 (port smtpmail-smtp-service
)
555 ;; smtpmail-mail-address should be set to the appropriate
556 ;; buffer-local value by the caller, but in case not:
557 (envelope-from (or smtpmail-mail-address
558 (and mail-specify-envelope-from
559 (mail-envelope-from))
564 (supported-extensions '()))
567 ;; get or create the trace buffer
569 (get-buffer-create (format "*trace of SMTP session to %s*" host
)))
571 ;; clear the trace buffer of old output
572 (with-current-buffer process-buffer
575 ;; open the connection to the server
576 (setq process
(smtpmail-open-stream process-buffer host port
))
577 (and (null process
) (throw 'done nil
))
579 ;; set the send-filter
580 (set-process-filter process
'smtpmail-process-filter
)
582 (with-current-buffer process-buffer
583 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
584 (make-local-variable 'smtpmail-read-point
)
585 (setq smtpmail-read-point
(point-min))
588 (if (or (null (car (setq greeting
(smtpmail-read-response process
))))
589 (not (integerp (car greeting
)))
590 (>= (car greeting
) 400))
598 (smtpmail-send-command process
(format "EHLO %s" (smtpmail-fqdn)))
600 (if (or (null (car (setq response-code
601 (smtpmail-read-response process
))))
602 (not (integerp (car response-code
)))
603 (>= (car response-code
) 400))
606 (smtpmail-send-command
607 process
(format "HELO %s" (smtpmail-fqdn)))
609 (if (or (null (car (setq response-code
610 (smtpmail-read-response process
))))
611 (not (integerp (car response-code
)))
612 (>= (car response-code
) 400))
614 (dolist (line (cdr (cdr response-code
)))
615 (let ((name (mapcar (lambda (s) (intern (downcase s
)))
616 (split-string (substring line
4) "[ ]"))))
617 (and (eq (length name
) 1)
618 (setq name
(car name
)))
620 (cond ((memq (if (consp name
) (car name
) name
)
621 '(verb xvrb
8bitmime onex xone
625 auth
=login auth starttls
))
626 (setq supported-extensions
627 (cons name supported-extensions
)))
628 (smtpmail-warn-about-unknown-extensions
629 (message "Unknown extension %s" name
)))))))
632 (smtpmail-find-credentials smtpmail-starttls-credentials host port
)
633 (member 'starttls supported-extensions
)
634 (numberp (process-id process
)))
636 (smtpmail-send-command process
(format "STARTTLS"))
637 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
638 (not (integerp (car response-code
)))
639 (>= (car response-code
) 400))
641 (starttls-negotiate process
)
642 (setq do-starttls nil
))
643 (setq do-ehlo nil
))))
645 (smtpmail-try-auth-methods process supported-extensions host port
)
647 (if (or (member 'onex supported-extensions
)
648 (member 'xone supported-extensions
))
650 (smtpmail-send-command process
(format "ONEX"))
651 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
652 (not (integerp (car response-code
)))
653 (>= (car response-code
) 400))
656 (if (and smtpmail-debug-verb
657 (or (member 'verb supported-extensions
)
658 (member 'xvrb supported-extensions
)))
660 (smtpmail-send-command process
(format "VERB"))
661 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
662 (not (integerp (car response-code
)))
663 (>= (car response-code
) 400))
666 (if (member 'xusr supported-extensions
)
668 (smtpmail-send-command process
(format "XUSR"))
669 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
670 (not (integerp (car response-code
)))
671 (>= (car response-code
) 400))
674 ;; MAIL FROM: <sender>
676 (if (or (member 'size supported-extensions
)
677 (assoc 'size supported-extensions
))
679 (with-current-buffer smtpmail-text-buffer
681 (+ (- (point-max) (point-min))
682 ;; Add one byte for each change-of-line
683 ;; because or CR-LF representation:
684 (count-lines (point-min) (point-max))
685 ;; For some reason, an empty line is
686 ;; added to the message. Maybe this
687 ;; is a bug, but it can't hurt to add
688 ;; those two bytes anyway:
692 (if (member '8bitmime supported-extensions
)
694 ;; Code should be added here that transforms
695 ;; the contents of the message buffer into
696 ;; something the receiving SMTP can handle.
697 ;; For a receiver that supports 8BITMIME, this
698 ;; may mean converting BINARY to BASE64, or
699 ;; adding Content-Transfer-Encoding and the
700 ;; other MIME headers. The code should also
701 ;; return an indication of what encoding the
702 ;; message buffer is now, i.e. ASCII or
708 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
709 (smtpmail-send-command process
(format "MAIL FROM: <%s>%s%s"
714 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
715 (not (integerp (car response-code
)))
716 (>= (car response-code
) 400))
720 ;; RCPT TO: <recipient>
722 (while (not (null (nth n recipient
)))
723 (smtpmail-send-command process
(format "RCPT TO: <%s>" (smtpmail-maybe-append-domain (nth n recipient
))))
726 (setq response-code
(smtpmail-read-response process
))
727 (if (or (null (car response-code
))
728 (not (integerp (car response-code
)))
729 (>= (car response-code
) 400))
735 (smtpmail-send-command process
"DATA")
737 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
738 (not (integerp (car response-code
)))
739 (>= (car response-code
) 400))
744 (smtpmail-send-data process smtpmail-text-buffer
)
747 (smtpmail-send-command process
".")
749 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
750 (not (integerp (car response-code
)))
751 (>= (car response-code
) 400))
756 ; (smtpmail-send-command process "QUIT")
757 ; (and (null (car (smtpmail-read-response process)))
761 (with-current-buffer (process-buffer process
)
762 (smtpmail-send-command process
"QUIT")
763 (smtpmail-read-response process
)
765 ; (if (or (null (car (setq response-code (smtpmail-read-response process))))
766 ; (not (integerp (car response-code)))
767 ; (>= (car response-code) 400))
770 (delete-process process
)
771 (unless smtpmail-debug-info
772 (kill-buffer process-buffer
)))))))
775 (defun smtpmail-process-filter (process output
)
776 (with-current-buffer (process-buffer process
)
777 (goto-char (point-max))
780 (defun smtpmail-read-response (process)
781 (let ((case-fold-search nil
)
782 (response-strings nil
)
783 (response-continue t
)
784 (return-value '(nil ()))
787 (while response-continue
788 (goto-char smtpmail-read-point
)
789 (while (not (search-forward "\r\n" nil t
))
790 (accept-process-output process
)
791 (goto-char smtpmail-read-point
))
793 (setq match-end
(point))
794 (setq response-strings
795 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
798 (goto-char smtpmail-read-point
)
799 (if (looking-at "[0-9]+ ")
800 (let ((begin (match-beginning 0))
802 (if smtpmail-debug-info
803 (message "%s" (car response-strings
)))
805 (setq smtpmail-read-point match-end
)
807 ;; ignore lines that start with "0"
808 (if (looking-at "0[0-9]+ ")
810 (setq response-continue nil
)
813 (buffer-substring begin end
))
814 (nreverse response-strings
)))))
816 (if (looking-at "[0-9]+-")
817 (progn (if smtpmail-debug-info
818 (message "%s" (car response-strings
)))
819 (setq smtpmail-read-point match-end
)
820 (setq response-continue t
))
822 (setq smtpmail-read-point match-end
)
823 (setq response-continue nil
)
825 (cons nil
(nreverse response-strings
)))
828 (setq smtpmail-read-point match-end
)
832 (defun smtpmail-send-command (process command
)
833 (goto-char (point-max))
834 (if (= (aref command
0) ?P
)
835 (insert "PASS <omitted>\r\n")
836 (insert command
"\r\n"))
837 (setq smtpmail-read-point
(point))
838 (process-send-string process command
)
839 (process-send-string process
"\r\n"))
841 (defun smtpmail-send-data-1 (process data
)
842 (goto-char (point-max))
844 (if (and (multibyte-string-p data
)
845 smtpmail-code-conv-from
)
846 (setq data
(string-as-multibyte
847 (encode-coding-string data smtpmail-code-conv-from
))))
849 (if smtpmail-debug-info
850 (insert data
"\r\n"))
852 (setq smtpmail-read-point
(point))
853 ;; Escape "." at start of a line
854 (if (eq (string-to-char data
) ?.
)
855 (process-send-string process
"."))
856 (process-send-string process data
)
857 (process-send-string process
"\r\n")
860 (defun smtpmail-send-data (process buffer
)
867 (with-current-buffer buffer
868 (goto-char (point-min)))
871 (with-current-buffer buffer
873 (setq this-line
(point))
875 (setq this-line-end
(point))
876 (setq sending-data nil
)
877 (setq sending-data
(buffer-substring this-line this-line-end
))
878 (if (/= (forward-line 1) 0)
879 (setq data-continue nil
)))
881 (smtpmail-send-data-1 process sending-data
)
887 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
888 "Get address list suitable for smtp RCPT TO: <address>."
890 (with-current-buffer smtpmail-address-buffer
893 ((case-fold-search t
)
894 (simple-address-list "")
898 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
899 (goto-char (point-min))
900 ;; RESENT-* fields should stop processing of regular fields.
903 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
905 "^Resent-\\(to\\|cc\\|bcc\\):"
906 "^\\(To:\\|Cc:\\|Bcc:\\)")))
908 (while (re-search-forward addr-regexp header-end t
)
910 (setq this-line
(match-beginning 0))
912 ;; get any continuation lines
913 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
915 (setq this-line-end
(point-marker))
916 (setq simple-address-list
917 (concat simple-address-list
" "
918 (mail-strip-quoted-names (buffer-substring this-line this-line-end
))))
921 (insert " " simple-address-list
"\n")
922 (subst-char-in-region (point-min) (point-max) 10 ? t
);; newline --> blank
923 (subst-char-in-region (point-min) (point-max) ?
, ? t
);; comma --> blank
924 (subst-char-in-region (point-min) (point-max) 9 ? t
);; tab --> blank
926 (goto-char (point-min))
927 ;; tidyness in case hook is not robust when it looks at this
928 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
930 (goto-char (point-min))
931 (let (recipient-address-list)
932 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
934 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
935 recipient-address-list
))
937 (setq smtpmail-recipient-address-list recipient-address-list
))
945 (defun smtpmail-do-bcc (header-end)
946 "Delete [Resent-]BCC: and their continuation lines from the header area.
947 There may be multiple BCC: lines, and each may have arbitrarily
948 many continuation lines."
949 (let ((case-fold-search t
))
951 (goto-char (point-min))
952 ;; iterate over all BCC: lines
953 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t
)
954 (delete-region (match-beginning 0)
955 (progn (forward-line 1) (point)))
956 ;; get rid of any continuation lines
957 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
958 (replace-match ""))))))
963 ;;; smtpmail.el ends here