smtpmail.el 22.7 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2 3
;;; ###	Hacked by Mike Taylor, 11th October 1999 to add support for
;;;	automatically appending a domain to RCPT TO: addresses.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

5
;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
6 7

;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
8
;; Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
9
;; ESMTP support: Simon Leinen <simon@switch.ch>
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
;; Keywords: mail

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;; Send Mail to smtp host from smtpmail temp buffer.

33
;; Please add these lines in your .emacs(_emacs) or use customize.
Richard M. Stallman's avatar
Richard M. Stallman committed
34
;;
35 36
;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use `message'
Richard M. Stallman's avatar
Richard M. Stallman committed
37 38
;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
;;(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
Richard M. Stallman's avatar
Richard M. Stallman committed
41

Richard M. Stallman's avatar
Richard M. Stallman committed
42 43 44 45
;; To queue mail, set smtpmail-queue-mail to t and use 
;; smtpmail-send-queued-mail to send.


Richard M. Stallman's avatar
Richard M. Stallman committed
46 47 48
;;; Code:

(require 'sendmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
49
(require 'time-stamp)
Richard M. Stallman's avatar
Richard M. Stallman committed
50 51

;;;
52 53 54
(defgroup smtpmail nil
  "SMTP protocol for sending mail."
  :group 'mail)
Richard M. Stallman's avatar
Richard M. Stallman committed
55

56 57 58 59 60 61 62

(defcustom smtpmail-default-smtp-server nil
  "*Specify default SMTP server."
  :type '(choice (const nil) string)
  :group 'smtpmail)

(defcustom smtpmail-smtp-server 
63
  (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
64 65 66
  "*The name of the host running SMTP server."
  :type '(choice (const nil) string)
  :group 'smtpmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
67

68 69 70 71
(defcustom smtpmail-smtp-service 25
  "*SMTP service port number. smtp or 25 ."
  :type 'integer
  :group 'smtpmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
72

73
(defcustom smtpmail-local-domain nil
Richard M. Stallman's avatar
Richard M. Stallman committed
74 75
  "*Local domain name without a host name.
If the function (system-name) returns the full internet address,
76 77 78 79
don't define this value."
  :type '(choice (const nil) string)
  :group 'smtpmail)

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
(defcustom smtpmail-sendto-domain nil
  "*Local domain name without a host name.
This is appended (with an @-sign) to any specified recipients which do
not include an @-sign, so that each RCPT TO address is fully qualified.
\(Some configurations of sendmail require this.)

Don't bother to set this unless you have get an error like:
	Sending failed; SMTP protocol error
when sending mail, and the *trace of SMTP session to <somewhere>*
buffer includes an exchange like:
	RCPT TO: <someone>
	501 <someone>: recipient address must contain a domain
"
  :type '(choice (const nil) string)
  :group 'smtpmail)

(defun maybe-append-domain (recipient)
  (if (or (not smtpmail-sendto-domain)
	  (string-match "@" recipient))
      recipient
    (concat recipient "@" smtpmail-sendto-domain)))

102 103 104 105 106 107 108 109 110
(defcustom smtpmail-debug-info nil
  "*smtpmail debug info printout. messages and process buffer."
  :type 'boolean
  :group 'smtpmail)

(defcustom smtpmail-code-conv-from nil ;; *junet*
  "*smtpmail code convert from this code to *internal*..for tiny-mime.."
  :type 'boolean
  :group 'smtpmail)
Richard M. Stallman's avatar
Richard M. Stallman committed
111

Richard M. Stallman's avatar
Richard M. Stallman committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
(defcustom smtpmail-queue-mail nil 
  "*Specify if mail is queued (if t) or sent immediately (if nil).
If queued, it is stored in the directory `smtpmail-queue-dir'
and sent with `smtpmail-send-queued-mail'."
  :type 'boolean
  :group 'smtpmail)

(defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
  "*Directory where `smtpmail.el' stores queued mail."
  :type 'directory
  :group 'smtpmail)

(defvar smtpmail-queue-index-file "index"
  "File name of queued mail index,
This is relative to `smtpmail-queue-dir'.")

128 129 130 131 132 133
(defvar smtpmail-address-buffer)
(defvar smtpmail-recipient-address-list)

;; Buffer-local variable.
(defvar smtpmail-read-point)

Richard M. Stallman's avatar
Richard M. Stallman committed
134 135 136
(defvar smtpmail-queue-index (concat smtpmail-queue-dir
				     smtpmail-queue-index-file))

Richard M. Stallman's avatar
Richard M. Stallman committed
137 138 139 140
;;;
;;;
;;;

141
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
142
(defun smtpmail-send-it ()
143
  (require 'mail-utils)
Richard M. Stallman's avatar
Richard M. Stallman committed
144 145 146 147 148 149
  (let ((errbuf (if mail-interactive
		    (generate-new-buffer " smtpmail errors")
		  0))
	(tembuf (generate-new-buffer " smtpmail temp"))
	(case-fold-search nil)
	delimline
Kenichi Handa's avatar
Kenichi Handa committed
150 151 152 153 154
	(mailbuf (current-buffer))
	(smtpmail-code-conv-from
	 (if enable-multibyte-characters
	     (let ((sendmail-coding-system smtpmail-code-conv-from))
	       (select-message-coding-system)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
155 156 157 158 159 160 161 162 163 164
    (unwind-protect
	(save-excursion
	  (set-buffer tembuf)
	  (erase-buffer)
	  (insert-buffer-substring mailbuf)
	  (goto-char (point-max))
	  ;; require one newline at the end.
	  (or (= (preceding-char) ?\n)
	      (insert ?\n))
	  ;; Change header-delimiter to be what sendmail expects.
165
	  (mail-sendmail-undelimit-header)
Richard M. Stallman's avatar
Richard M. Stallman committed
166
	  (setq delimline (point-marker))
167
;;	  (sendmail-synch-aliases)
Richard M. Stallman's avatar
Richard M. Stallman committed
168 169 170 171 172 173 174 175
	  (if mail-aliases
	      (expand-mail-aliases (point-min) delimline))
	  (goto-char (point-min))
	  ;; ignore any blank lines in the header
	  (while (and (re-search-forward "\n\n\n*" delimline t)
		      (< (point) delimline))
	    (replace-match "\n"))
	  (let ((case-fold-search t))
176 177 178
	    ;; We used to process Resent-... headers here,
	    ;; but it was not done properly, and the job
	    ;; is done correctly in smtpmail-deduce-address-list.
Richard M. Stallman's avatar
Richard M. Stallman committed
179 180
	    ;; Don't send out a blank subject line
	    (goto-char (point-min))
181 182 183 184 185 186
	    (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
		(replace-match "")
	      ;; This one matches a Subject just before the header delimiter.
	      (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
		       (= (match-end 0) delimline))
		  (replace-match "")))
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	    ;; Put the "From:" field in unless for some odd reason
	    ;; they put one in themselves.
	    (goto-char (point-min))
	    (if (not (re-search-forward "^From:" delimline t))
		(let* ((login user-mail-address)
		       (fullname (user-full-name)))
		  (cond ((eq mail-from-style 'angles)
			 (insert "From: " fullname)
			 (let ((fullname-start (+ (point-min) 6))
			       (fullname-end (point-marker)))
			   (goto-char fullname-start)
			   ;; Look for a character that cannot appear unquoted
			   ;; according to RFC 822.
			   (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
						  fullname-end 1)
			       (progn
				 ;; Quote fullname, escaping specials.
				 (goto-char fullname-start)
				 (insert "\"")
				 (while (re-search-forward "[\"\\]"
							   fullname-end 1)
				   (replace-match "\\\\\\&" t))
				 (insert "\""))))
			 (insert " <" login ">\n"))
			((eq mail-from-style 'parens)
			 (insert "From: " login " (")
			 (let ((fullname-start (point)))
			   (insert fullname)
			   (let ((fullname-end (point-marker)))
			     (goto-char fullname-start)
			     ;; RFC 822 says \ and nonmatching parentheses
			     ;; must be escaped in comments.
			     ;; Escape every instance of ()\ ...
			     (while (re-search-forward "[()\\]" fullname-end 1)
			       (replace-match "\\\\\\&" t))
			     ;; ... then undo escaping of matching parentheses,
			     ;; including matching nested parentheses.
			     (goto-char fullname-start)
			     (while (re-search-forward 
				     "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
				     fullname-end 1)
			       (replace-match "\\1(\\3)" t)
			       (goto-char fullname-start))))
			 (insert ")\n"))
			((null mail-from-style)
			 (insert "From: " login "\n")))))
Richard M. Stallman's avatar
Richard M. Stallman committed
233 234 235 236 237
	    ;; Insert an extra newline if we need it to work around
	    ;; Sun's bug that swallows newlines.
	    (goto-char (1+ delimline))
	    (if (eval mail-mailer-swallows-blank-line)
		(newline))
238 239 240 241
	    ;; Find and handle any FCC fields.
	    (goto-char (point-min))
	    (if (re-search-forward "^FCC:" delimline t)
		(mail-do-fcc delimline))
Richard M. Stallman's avatar
Richard M. Stallman committed
242 243 244 245 246 247 248 249 250
	    (if mail-interactive
		(save-excursion
		  (set-buffer errbuf)
		  (erase-buffer))))
	  ;;
	  ;;
	  ;;
	  (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
	  (setq smtpmail-recipient-address-list
251
		    (smtpmail-deduce-address-list tembuf (point-min) delimline))
Richard M. Stallman's avatar
Richard M. Stallman committed
252
	  (kill-buffer smtpmail-address-buffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
253
	  
Richard M. Stallman's avatar
Richard M. Stallman committed
254
	  (smtpmail-do-bcc delimline)
Richard M. Stallman's avatar
Richard M. Stallman committed
255 256 257 258 259 260 261 262 263
	  ; Send or queue
	  (if (not smtpmail-queue-mail)
	      (if (not (null smtpmail-recipient-address-list))
		  (if (not (smtpmail-via-smtp 
			    smtpmail-recipient-address-list tembuf))
		      (error "Sending failed; SMTP protocol error"))
		(error "Sending failed; no recipients"))
	    (let* ((file-data (concat 
			       smtpmail-queue-dir
264 265
			       (concat (time-stamp-yyyy-mm-dd)
				       "_" (time-stamp-hh:mm:ss))))
266 267
		      (file-data (convert-standard-filename file-data))
		      (file-elisp (concat file-data ".el"))
Richard M. Stallman's avatar
Richard M. Stallman committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
		   (buffer-data (create-file-buffer file-data))
		   (buffer-elisp (create-file-buffer file-elisp))
		   (buffer-scratch "*queue-mail*"))
	      (save-excursion
		(set-buffer buffer-data)
		(erase-buffer)
		(insert-buffer tembuf)
		(write-file file-data)
		(set-buffer buffer-elisp)
		(erase-buffer)
		(insert (concat
			 "(setq smtpmail-recipient-address-list '"
			 (prin1-to-string smtpmail-recipient-address-list)
			 ")\n"))	    	    
		(write-file file-elisp)
		(set-buffer (generate-new-buffer buffer-scratch))
		(insert (concat file-data "\n"))
		(append-to-file (point-min) 
				(point-max) 
				smtpmail-queue-index)
		)
	      (kill-buffer buffer-scratch)
	      (kill-buffer buffer-data)
	      (kill-buffer buffer-elisp))))
Richard M. Stallman's avatar
Richard M. Stallman committed
292 293 294 295
      (kill-buffer tembuf)
      (if (bufferp errbuf)
	  (kill-buffer errbuf)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
(defun smtpmail-send-queued-mail ()
  "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
  (interactive)
  ;;; Get index, get first mail, send it, get second mail, etc...
  (let ((buffer-index (find-file-noselect smtpmail-queue-index))
	(file-msg "")
	(tembuf nil))
    (save-excursion
      (set-buffer buffer-index)
      (beginning-of-buffer)
      (while (not (eobp))
	(setq file-msg (buffer-substring (point) (save-excursion
						   (end-of-line)
						   (point))))
	(load file-msg)
	(setq tembuf (find-file-noselect file-msg))
	(if (not (null smtpmail-recipient-address-list))
	    (if (not (smtpmail-via-smtp smtpmail-recipient-address-list 
					tembuf))
		(error "Sending failed; SMTP protocol error"))
	  (error "Sending failed; no recipients"))  
	(delete-file file-msg)
	(delete-file (concat file-msg ".el"))
	(kill-buffer tembuf)
	(kill-line 1))      
      (set-buffer buffer-index)
      (save-buffer smtpmail-queue-index)
      (kill-buffer buffer-index)
      )))
Richard M. Stallman's avatar
Richard M. Stallman committed
325 326 327 328 329 330 331 332 333 334

;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)

(defun smtpmail-fqdn ()
  (if smtpmail-local-domain
      (concat (system-name) "." smtpmail-local-domain)
    (system-name)))

(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
  (let ((process nil)
335 336
	(host (or smtpmail-smtp-server
		  (error "`smtpmail-smtp-server' not defined")))
337
	(port smtpmail-smtp-service)
Richard M. Stallman's avatar
Richard M. Stallman committed
338
	response-code
339
	greeting
340 341
	process-buffer
	(supported-extensions '()))
Richard M. Stallman's avatar
Richard M. Stallman committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
    (unwind-protect
	(catch 'done
	  ;; get or create the trace buffer
	  (setq process-buffer
		(get-buffer-create (format "*trace of SMTP session to %s*" host)))

	  ;; clear the trace buffer of old output
	  (save-excursion
	    (set-buffer process-buffer)
	    (erase-buffer))

	  ;; open the connection to the server
	  (setq process (open-network-stream "SMTP" process-buffer host port))
	  (and (null process) (throw 'done nil))

	  ;; set the send-filter
	  (set-process-filter process 'smtpmail-process-filter)

	  (save-excursion
	    (set-buffer process-buffer)
362
	    (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix)
Richard M. Stallman's avatar
Richard M. Stallman committed
363 364 365 366 367 368 369 370 371 372
	    (make-local-variable 'smtpmail-read-point)
	    (setq smtpmail-read-point (point-min))

	    
	    (if (or (null (car (setq greeting (smtpmail-read-response process))))
		    (not (integerp (car greeting)))
		    (>= (car greeting) 400))
		(throw 'done nil)
	      )

373 374
	    ;; EHLO
	    (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
Richard M. Stallman's avatar
Richard M. Stallman committed
375 376 377 378

	    (if (or (null (car (setq response-code (smtpmail-read-response process))))
		    (not (integerp (car response-code)))
		    (>= (car response-code) 400))
379 380 381 382 383 384 385 386 387 388
		(progn
		  ;; HELO
		  (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn)))

		  (if (or (null (car (setq response-code (smtpmail-read-response process))))
			  (not (integerp (car response-code)))
			  (>= (car response-code) 400))
		      (throw 'done nil)))
	      (let ((extension-lines (cdr (cdr response-code))))
		(while extension-lines
389
		  (let ((name (intern (downcase (car (split-string (substring (car extension-lines) 4) "[ ]"))))))
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		    (and name
			 (cond ((memq name '(verb xvrb 8bitmime onex xone
						  expn size dsn etrn
						  help xusr))
				(setq supported-extensions
				      (cons name supported-extensions)))
			       (t (message "unknown extension %s"
					   name)))))
		  (setq extension-lines (cdr extension-lines)))))

	    (if (or (member 'onex supported-extensions)
		    (member 'xone supported-extensions))
		(progn
		  (smtpmail-send-command process (format "ONEX"))
		  (if (or (null (car (setq response-code (smtpmail-read-response process))))
			  (not (integerp (car response-code)))
			  (>= (car response-code) 400))
		      (throw 'done nil))))

	    (if (and smtpmail-debug-info
		     (or (member 'verb supported-extensions)
			 (member 'xvrb supported-extensions)))
		(progn
		  (smtpmail-send-command process (format "VERB"))
		  (if (or (null (car (setq response-code (smtpmail-read-response process))))
			  (not (integerp (car response-code)))
			  (>= (car response-code) 400))
		      (throw 'done nil))))

	    (if (member 'xusr supported-extensions)
		(progn
		  (smtpmail-send-command process (format "XUSR"))
		  (if (or (null (car (setq response-code (smtpmail-read-response process))))
			  (not (integerp (car response-code)))
			  (>= (car response-code) 400))
		      (throw 'done nil))))
Richard M. Stallman's avatar
Richard M. Stallman committed
426 427

	    ;; MAIL FROM: <sender>
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	    (let ((size-part
		   (if (member 'size supported-extensions)
		       (format " SIZE=%d"
			       (save-excursion
				 (set-buffer smtpmail-text-buffer)
				 ;; size estimate:
				 (+ (- (point-max) (point-min))
				    ;; Add one byte for each change-of-line
				    ;; because or CR-LF representation:
				    (count-lines (point-min) (point-max))
				    ;; For some reason, an empty line is
				    ;; added to the message.  Maybe this
				    ;; is a bug, but it can't hurt to add
				    ;; those two bytes anyway:
				    2)))
		     ""))
		  (body-part
		   (if (member '8bitmime supported-extensions)
		       ;; FIXME:
		       ;; Code should be added here that transforms
		       ;; the contents of the message buffer into
		       ;; something the receiving SMTP can handle.
		       ;; For a receiver that supports 8BITMIME, this
		       ;; may mean converting BINARY to BASE64, or
		       ;; adding Content-Transfer-Encoding and the
		       ;; other MIME headers.  The code should also
		       ;; return an indication of what encoding the
		       ;; message buffer is now, i.e. ASCII or
		       ;; 8BITMIME.
		       (if nil
			   " BODY=8BITMIME"
			 "")
		     "")))
;	      (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
	      (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
						     user-mail-address
						     size-part
						     body-part))

	      (if (or (null (car (setq response-code (smtpmail-read-response process))))
		      (not (integerp (car response-code)))
		      (>= (car response-code) 400))
		  (throw 'done nil)
		))
Richard M. Stallman's avatar
Richard M. Stallman committed
472 473
	    
	    ;; RCPT TO: <recipient>
474 475
	    (let ((n 0))
	      (while (not (null (nth n recipient)))
476
		(smtpmail-send-command process (format "RCPT TO: <%s>" (maybe-append-domain (nth n recipient))))
477 478
		(setq n (1+ n))

479 480
		(setq response-code (smtpmail-read-response process))
		(if (or (null (car response-code))
481 482 483 484 485
			(not (integerp (car response-code)))
			(>= (car response-code) 400))
		    (throw 'done nil)
		  )
		))
Richard M. Stallman's avatar
Richard M. Stallman committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	    
	    ;; DATA
	    (smtpmail-send-command process "DATA")

	    (if (or (null (car (setq response-code (smtpmail-read-response process))))
		    (not (integerp (car response-code)))
		    (>= (car response-code) 400))
		(throw 'done nil)
	      )

	    ;; Mail contents
	    (smtpmail-send-data process smtpmail-text-buffer)

	    ;;DATA end "."
	    (smtpmail-send-command process ".")

	    (if (or (null (car (setq response-code (smtpmail-read-response process))))
		    (not (integerp (car response-code)))
		    (>= (car response-code) 400))
		(throw 'done nil)
	      )

	    ;;QUIT
;	    (smtpmail-send-command process "QUIT")
;	    (and (null (car (smtpmail-read-response process)))
;		 (throw 'done nil))
	    t ))
      (if process
	  (save-excursion
	    (set-buffer (process-buffer process))
	    (smtpmail-send-command process "QUIT")
	    (smtpmail-read-response process)

;	    (if (or (null (car (setq response-code (smtpmail-read-response process))))
;		    (not (integerp (car response-code)))
;		    (>= (car response-code) 400))
;		(throw 'done nil)
;	      )
	    (delete-process process))))))


(defun smtpmail-process-filter (process output)
  (save-excursion
    (set-buffer (process-buffer process))
    (goto-char (point-max))
    (insert output)))

(defun smtpmail-read-response (process)
  (let ((case-fold-search nil)
535
	(response-strings nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
536
	(response-continue t)
537
	(return-value '(nil ()))
Richard M. Stallman's avatar
Richard M. Stallman committed
538 539 540
	match-end)

    (while response-continue
541
      (goto-char smtpmail-read-point)
Richard M. Stallman's avatar
Richard M. Stallman committed
542 543 544 545 546
      (while (not (search-forward "\r\n" nil t))
	(accept-process-output process)
	(goto-char smtpmail-read-point))

      (setq match-end (point))
547 548 549
      (setq response-strings
	    (cons (buffer-substring smtpmail-read-point (- match-end 2))
		  response-strings))
Richard M. Stallman's avatar
Richard M. Stallman committed
550 551 552
	
      (goto-char smtpmail-read-point)
      (if (looking-at "[0-9]+ ")
553 554 555 556
	  (let ((begin (match-beginning 0))
		(end (match-end 0)))
	    (if smtpmail-debug-info
		(message "%s" (car response-strings)))
Richard M. Stallman's avatar
Richard M. Stallman committed
557

558
	    (setq smtpmail-read-point match-end)
Richard M. Stallman's avatar
Richard M. Stallman committed
559

560 561 562 563 564 565 566 567
	    ;; ignore lines that start with "0"
	    (if (looking-at "0[0-9]+ ")
		nil
	      (setq response-continue nil)
	      (setq return-value
		    (cons (string-to-int 
			   (buffer-substring begin end)) 
			  (nreverse response-strings)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
568 569
	
	(if (looking-at "[0-9]+-")
570 571 572
	    (progn (if smtpmail-debug-info
		     (message "%s" (car response-strings)))
		   (setq smtpmail-read-point match-end)
Richard M. Stallman's avatar
Richard M. Stallman committed
573 574 575 576 577
		   (setq response-continue t))
	  (progn
	    (setq smtpmail-read-point match-end)
	    (setq response-continue nil)
	    (setq return-value 
578
		  (cons nil (nreverse response-strings)))
Richard M. Stallman's avatar
Richard M. Stallman committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	    )
	  )))
    (setq smtpmail-read-point match-end)
    return-value))


(defun smtpmail-send-command (process command)
  (goto-char (point-max))
  (if (= (aref command 0) ?P)
      (insert "PASS <omitted>\r\n")
    (insert command "\r\n"))
  (setq smtpmail-read-point (point))
  (process-send-string process command)
  (process-send-string process "\r\n"))

(defun smtpmail-send-data-1 (process data)
  (goto-char (point-max))

Kenichi Handa's avatar
Kenichi Handa committed
597 598 599 600
  (if (and (multibyte-string-p data)
	   smtpmail-code-conv-from)
      (setq data (string-as-multibyte
		  (encode-coding-string data smtpmail-code-conv-from))))
Richard M. Stallman's avatar
Richard M. Stallman committed
601 602 603 604 605
	
  (if smtpmail-debug-info
      (insert data "\r\n"))

  (setq smtpmail-read-point (point))
606 607
  ;; Escape "." at start of a line
  (if (eq (string-to-char data) ?.)
Richard M. Stallman's avatar
Richard M. Stallman committed
608
      (process-send-string process "."))
609
  (process-send-string process data)
Richard M. Stallman's avatar
Richard M. Stallman committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
  (process-send-string process "\r\n")
  )

(defun smtpmail-send-data (process buffer)
  (let
      ((data-continue t)
       (sending-data nil)
       this-line
       this-line-end)

    (save-excursion
      (set-buffer buffer)
      (goto-char (point-min)))

    (while data-continue
      (save-excursion
	(set-buffer buffer)
	(beginning-of-line)
	(setq this-line (point))
	(end-of-line)
	(setq this-line-end (point))
	(setq sending-data nil)
	(setq sending-data (buffer-substring this-line this-line-end))
	(if (/= (forward-line 1) 0)
	    (setq data-continue nil)))

      (smtpmail-send-data-1 process sending-data)
      )
    )
  )
    

(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
  "Get address list suitable for smtp RCPT TO: <address>."
  (require 'mail-utils)  ;; pick up mail-strip-quoted-names
    
646 647 648 649 650 651 652 653 654
  (unwind-protect
      (save-excursion
	(set-buffer smtpmail-address-buffer) (erase-buffer)
	(let
	    ((case-fold-search t)
	     (simple-address-list "")
	     this-line
	     this-line-end
	     addr-regexp)
Richard M. Stallman's avatar
Richard M. Stallman committed
655 656
	  (insert-buffer-substring smtpmail-text-buffer header-start header-end)
	  (goto-char (point-min))
657 658
	  ;; RESENT-* fields should stop processing of regular fields.
	  (save-excursion
659 660 661
	    (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" header-end t)
		(setq addr-regexp "^Resent-\\(to\\|cc\\|bcc\\):")
	      (setq addr-regexp  "^\\(To:\\|Cc:\\|Bcc:\\)")))
662 663

	  (while (re-search-forward addr-regexp header-end t)
Richard M. Stallman's avatar
Richard M. Stallman committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	    (replace-match "")
	    (setq this-line (match-beginning 0))
	    (forward-line 1)
	    ;; get any continuation lines
	    (while (and (looking-at "^[ \t]+") (< (point) header-end))
	      (forward-line 1))
	    (setq this-line-end (point-marker))
	    (setq simple-address-list
		  (concat simple-address-list " "
			  (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
	    )
	  (erase-buffer)
	  (insert-string " ")
	  (insert-string simple-address-list)
	  (insert-string "\n")
	  (subst-char-in-region (point-min) (point-max) 10 ?  t);; newline --> blank
	  (subst-char-in-region (point-min) (point-max) ?, ?  t);; comma   --> blank
	  (subst-char-in-region (point-min) (point-max)  9 ?  t);; tab     --> blank

	  (goto-char (point-min))
	  ;; tidyness in case hook is not robust when it looks at this
	  (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))

	  (goto-char (point-min))
688
	  (let (recipient-address-list)
689
	    (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
690
	      (backward-char 1)
691 692
	      (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
						 recipient-address-list))
693 694
	      )
	    (setq smtpmail-recipient-address-list recipient-address-list))
Richard M. Stallman's avatar
Richard M. Stallman committed
695 696

	  )
697
	)
Richard M. Stallman's avatar
Richard M. Stallman committed
698 699 700 701 702
    )
  )


(defun smtpmail-do-bcc (header-end)
703
  "Delete [Resent-]BCC: and their continuation lines from the header area.
Richard M. Stallman's avatar
Richard M. Stallman committed
704 705 706
There may be multiple BCC: lines, and each may have arbitrarily
many continuation lines."
  (let ((case-fold-search t))
Karl Heuer's avatar
Karl Heuer committed
707 708 709 710 711 712 713 714 715
    (save-excursion
      (goto-char (point-min))
      ;; iterate over all BCC: lines
      (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t)
	(delete-region (match-beginning 0)
		       (progn (forward-line 1) (point)))
	;; get rid of any continuation lines
	(while (and (looking-at "^[ \t].*\n") (< (point) header-end))
	  (replace-match ""))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
716 717 718 719


(provide 'smtpmail)

Richard M. Stallman's avatar
Richard M. Stallman committed
720
;;; smtpmail.el ends here