Commit | Line | Data |
---|---|---|
092af6d8 | 1 | ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail |
24975917 | 2 | |
e2f7c221 | 3 | ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
24975917 RS |
4 | |
5 | ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp> | |
0e2701ca | 6 | ;; Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu> |
c50d5ce0 | 7 | ;; ESMTP support: Simon Leinen <simon@switch.ch> |
24975917 RS |
8 | ;; Keywords: mail |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
26 | ||
27 | ;;; Commentary: | |
28 | ||
29 | ;; Send Mail to smtp host from smtpmail temp buffer. | |
24975917 | 30 | |
f38d3514 | 31 | ;; Please add these lines in your .emacs(_emacs) or use customize. |
24975917 | 32 | ;; |
f38d3514 KH |
33 | ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail' |
34 | ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use `message' | |
24975917 | 35 | ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST") |
24975917 | 36 | ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME") |
f38d3514 | 37 | ;;(setq smtpmail-debug-info t) ; only to debug problems |
24975917 | 38 | |
308bc5d8 RS |
39 | ;; To queue mail, set smtpmail-queue-mail to t and use |
40 | ;; smtpmail-send-queued-mail to send. | |
41 | ||
42 | ||
24975917 RS |
43 | ;;; Code: |
44 | ||
45 | (require 'sendmail) | |
308bc5d8 | 46 | (require 'time-stamp) |
24975917 RS |
47 | |
48 | ;;; | |
00ed33e7 RS |
49 | (defgroup smtpmail nil |
50 | "SMTP protocol for sending mail." | |
51 | :group 'mail) | |
24975917 | 52 | |
00ed33e7 RS |
53 | |
54 | (defcustom smtpmail-default-smtp-server nil | |
55 | "*Specify default SMTP server." | |
56 | :type '(choice (const nil) string) | |
57 | :group 'smtpmail) | |
58 | ||
59 | (defcustom smtpmail-smtp-server | |
e2f7c221 | 60 | (or (getenv "SMTPSERVER") smtpmail-default-smtp-server) |
00ed33e7 RS |
61 | "*The name of the host running SMTP server." |
62 | :type '(choice (const nil) string) | |
63 | :group 'smtpmail) | |
24975917 | 64 | |
00ed33e7 RS |
65 | (defcustom smtpmail-smtp-service 25 |
66 | "*SMTP service port number. smtp or 25 ." | |
67 | :type 'integer | |
68 | :group 'smtpmail) | |
24975917 | 69 | |
00ed33e7 | 70 | (defcustom smtpmail-local-domain nil |
24975917 RS |
71 | "*Local domain name without a host name. |
72 | If the function (system-name) returns the full internet address, | |
00ed33e7 RS |
73 | don't define this value." |
74 | :type '(choice (const nil) string) | |
75 | :group 'smtpmail) | |
76 | ||
77 | (defcustom smtpmail-debug-info nil | |
78 | "*smtpmail debug info printout. messages and process buffer." | |
79 | :type 'boolean | |
80 | :group 'smtpmail) | |
81 | ||
82 | (defcustom smtpmail-code-conv-from nil ;; *junet* | |
83 | "*smtpmail code convert from this code to *internal*..for tiny-mime.." | |
84 | :type 'boolean | |
85 | :group 'smtpmail) | |
24975917 | 86 | |
308bc5d8 RS |
87 | (defcustom smtpmail-queue-mail nil |
88 | "*Specify if mail is queued (if t) or sent immediately (if nil). | |
89 | If queued, it is stored in the directory `smtpmail-queue-dir' | |
90 | and sent with `smtpmail-send-queued-mail'." | |
91 | :type 'boolean | |
92 | :group 'smtpmail) | |
93 | ||
94 | (defcustom smtpmail-queue-dir "~/Mail/queued-mail/" | |
95 | "*Directory where `smtpmail.el' stores queued mail." | |
96 | :type 'directory | |
97 | :group 'smtpmail) | |
98 | ||
99 | (defvar smtpmail-queue-index-file "index" | |
100 | "File name of queued mail index, | |
101 | This is relative to `smtpmail-queue-dir'.") | |
102 | ||
fb035bbf RS |
103 | (defvar smtpmail-address-buffer) |
104 | (defvar smtpmail-recipient-address-list) | |
105 | ||
106 | ;; Buffer-local variable. | |
107 | (defvar smtpmail-read-point) | |
108 | ||
308bc5d8 RS |
109 | (defvar smtpmail-queue-index (concat smtpmail-queue-dir |
110 | smtpmail-queue-index-file)) | |
111 | ||
24975917 RS |
112 | ;;; |
113 | ;;; | |
114 | ;;; | |
115 | ||
f38d3514 | 116 | ;;;###autoload |
24975917 | 117 | (defun smtpmail-send-it () |
e2f7c221 | 118 | (require 'mail-utils) |
24975917 RS |
119 | (let ((errbuf (if mail-interactive |
120 | (generate-new-buffer " smtpmail errors") | |
121 | 0)) | |
122 | (tembuf (generate-new-buffer " smtpmail temp")) | |
123 | (case-fold-search nil) | |
24975917 RS |
124 | delimline |
125 | (mailbuf (current-buffer))) | |
126 | (unwind-protect | |
127 | (save-excursion | |
128 | (set-buffer tembuf) | |
129 | (erase-buffer) | |
130 | (insert-buffer-substring mailbuf) | |
131 | (goto-char (point-max)) | |
132 | ;; require one newline at the end. | |
133 | (or (= (preceding-char) ?\n) | |
134 | (insert ?\n)) | |
135 | ;; Change header-delimiter to be what sendmail expects. | |
92a3f23d | 136 | (mail-sendmail-undelimit-header) |
24975917 | 137 | (setq delimline (point-marker)) |
e2f7c221 | 138 | ;; (sendmail-synch-aliases) |
24975917 RS |
139 | (if mail-aliases |
140 | (expand-mail-aliases (point-min) delimline)) | |
141 | (goto-char (point-min)) | |
142 | ;; ignore any blank lines in the header | |
143 | (while (and (re-search-forward "\n\n\n*" delimline t) | |
144 | (< (point) delimline)) | |
145 | (replace-match "\n")) | |
146 | (let ((case-fold-search t)) | |
5feeeae2 RS |
147 | ;; We used to process Resent-... headers here, |
148 | ;; but it was not done properly, and the job | |
149 | ;; is done correctly in smtpmail-deduce-address-list. | |
24975917 RS |
150 | ;; Don't send out a blank subject line |
151 | (goto-char (point-min)) | |
5feeeae2 RS |
152 | (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t) |
153 | (replace-match "") | |
154 | ;; This one matches a Subject just before the header delimiter. | |
155 | (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t) | |
156 | (= (match-end 0) delimline)) | |
157 | (replace-match ""))) | |
0e2701ca RS |
158 | ;; Put the "From:" field in unless for some odd reason |
159 | ;; they put one in themselves. | |
160 | (goto-char (point-min)) | |
161 | (if (not (re-search-forward "^From:" delimline t)) | |
162 | (let* ((login user-mail-address) | |
163 | (fullname (user-full-name))) | |
164 | (cond ((eq mail-from-style 'angles) | |
165 | (insert "From: " fullname) | |
166 | (let ((fullname-start (+ (point-min) 6)) | |
167 | (fullname-end (point-marker))) | |
168 | (goto-char fullname-start) | |
169 | ;; Look for a character that cannot appear unquoted | |
170 | ;; according to RFC 822. | |
171 | (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]" | |
172 | fullname-end 1) | |
173 | (progn | |
174 | ;; Quote fullname, escaping specials. | |
175 | (goto-char fullname-start) | |
176 | (insert "\"") | |
177 | (while (re-search-forward "[\"\\]" | |
178 | fullname-end 1) | |
179 | (replace-match "\\\\\\&" t)) | |
180 | (insert "\"")))) | |
181 | (insert " <" login ">\n")) | |
182 | ((eq mail-from-style 'parens) | |
183 | (insert "From: " login " (") | |
184 | (let ((fullname-start (point))) | |
185 | (insert fullname) | |
186 | (let ((fullname-end (point-marker))) | |
187 | (goto-char fullname-start) | |
188 | ;; RFC 822 says \ and nonmatching parentheses | |
189 | ;; must be escaped in comments. | |
190 | ;; Escape every instance of ()\ ... | |
191 | (while (re-search-forward "[()\\]" fullname-end 1) | |
192 | (replace-match "\\\\\\&" t)) | |
193 | ;; ... then undo escaping of matching parentheses, | |
194 | ;; including matching nested parentheses. | |
195 | (goto-char fullname-start) | |
196 | (while (re-search-forward | |
197 | "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)" | |
198 | fullname-end 1) | |
199 | (replace-match "\\1(\\3)" t) | |
200 | (goto-char fullname-start)))) | |
201 | (insert ")\n")) | |
202 | ((null mail-from-style) | |
203 | (insert "From: " login "\n"))))) | |
24975917 RS |
204 | ;; Insert an extra newline if we need it to work around |
205 | ;; Sun's bug that swallows newlines. | |
206 | (goto-char (1+ delimline)) | |
207 | (if (eval mail-mailer-swallows-blank-line) | |
208 | (newline)) | |
0e2701ca RS |
209 | ;; Find and handle any FCC fields. |
210 | (goto-char (point-min)) | |
211 | (if (re-search-forward "^FCC:" delimline t) | |
212 | (mail-do-fcc delimline)) | |
24975917 RS |
213 | (if mail-interactive |
214 | (save-excursion | |
215 | (set-buffer errbuf) | |
216 | (erase-buffer)))) | |
217 | ;; | |
218 | ;; | |
219 | ;; | |
220 | (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*")) | |
221 | (setq smtpmail-recipient-address-list | |
5feeeae2 | 222 | (smtpmail-deduce-address-list tembuf (point-min) delimline)) |
24975917 | 223 | (kill-buffer smtpmail-address-buffer) |
308bc5d8 | 224 | |
24975917 | 225 | (smtpmail-do-bcc delimline) |
308bc5d8 RS |
226 | ; Send or queue |
227 | (if (not smtpmail-queue-mail) | |
228 | (if (not (null smtpmail-recipient-address-list)) | |
229 | (if (not (smtpmail-via-smtp | |
230 | smtpmail-recipient-address-list tembuf)) | |
231 | (error "Sending failed; SMTP protocol error")) | |
232 | (error "Sending failed; no recipients")) | |
233 | (let* ((file-data (concat | |
234 | smtpmail-queue-dir | |
3375a61c KH |
235 | (concat (time-stamp-yyyy-mm-dd) |
236 | "_" (time-stamp-hh:mm:ss)))) | |
308bc5d8 RS |
237 | (file-elisp (concat file-data ".el")) |
238 | (buffer-data (create-file-buffer file-data)) | |
239 | (buffer-elisp (create-file-buffer file-elisp)) | |
240 | (buffer-scratch "*queue-mail*")) | |
241 | (save-excursion | |
242 | (set-buffer buffer-data) | |
243 | (erase-buffer) | |
244 | (insert-buffer tembuf) | |
245 | (write-file file-data) | |
246 | (set-buffer buffer-elisp) | |
247 | (erase-buffer) | |
248 | (insert (concat | |
249 | "(setq smtpmail-recipient-address-list '" | |
250 | (prin1-to-string smtpmail-recipient-address-list) | |
251 | ")\n")) | |
252 | (write-file file-elisp) | |
253 | (set-buffer (generate-new-buffer buffer-scratch)) | |
254 | (insert (concat file-data "\n")) | |
255 | (append-to-file (point-min) | |
256 | (point-max) | |
257 | smtpmail-queue-index) | |
258 | ) | |
259 | (kill-buffer buffer-scratch) | |
260 | (kill-buffer buffer-data) | |
261 | (kill-buffer buffer-elisp)))) | |
24975917 RS |
262 | (kill-buffer tembuf) |
263 | (if (bufferp errbuf) | |
264 | (kill-buffer errbuf))))) | |
265 | ||
308bc5d8 RS |
266 | (defun smtpmail-send-queued-mail () |
267 | "Send mail that was queued as a result of setting `smtpmail-queue-mail'." | |
268 | (interactive) | |
269 | ;;; Get index, get first mail, send it, get second mail, etc... | |
270 | (let ((buffer-index (find-file-noselect smtpmail-queue-index)) | |
271 | (file-msg "") | |
272 | (tembuf nil)) | |
273 | (save-excursion | |
274 | (set-buffer buffer-index) | |
275 | (beginning-of-buffer) | |
276 | (while (not (eobp)) | |
277 | (setq file-msg (buffer-substring (point) (save-excursion | |
278 | (end-of-line) | |
279 | (point)))) | |
280 | (load file-msg) | |
281 | (setq tembuf (find-file-noselect file-msg)) | |
282 | (if (not (null smtpmail-recipient-address-list)) | |
283 | (if (not (smtpmail-via-smtp smtpmail-recipient-address-list | |
284 | tembuf)) | |
285 | (error "Sending failed; SMTP protocol error")) | |
286 | (error "Sending failed; no recipients")) | |
287 | (delete-file file-msg) | |
288 | (delete-file (concat file-msg ".el")) | |
289 | (kill-buffer tembuf) | |
290 | (kill-line 1)) | |
291 | (set-buffer buffer-index) | |
292 | (save-buffer smtpmail-queue-index) | |
293 | (kill-buffer buffer-index) | |
294 | ))) | |
24975917 RS |
295 | |
296 | ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer) | |
297 | ||
298 | (defun smtpmail-fqdn () | |
299 | (if smtpmail-local-domain | |
300 | (concat (system-name) "." smtpmail-local-domain) | |
301 | (system-name))) | |
302 | ||
303 | (defun smtpmail-via-smtp (recipient smtpmail-text-buffer) | |
304 | (let ((process nil) | |
f38d3514 KH |
305 | (host (or smtpmail-smtp-server |
306 | (error "`smtpmail-smtp-server' not defined"))) | |
8805249b | 307 | (port smtpmail-smtp-service) |
24975917 | 308 | response-code |
8805249b | 309 | greeting |
c50d5ce0 RS |
310 | process-buffer |
311 | (supported-extensions '())) | |
24975917 RS |
312 | (unwind-protect |
313 | (catch 'done | |
314 | ;; get or create the trace buffer | |
315 | (setq process-buffer | |
316 | (get-buffer-create (format "*trace of SMTP session to %s*" host))) | |
317 | ||
318 | ;; clear the trace buffer of old output | |
319 | (save-excursion | |
320 | (set-buffer process-buffer) | |
321 | (erase-buffer)) | |
322 | ||
323 | ;; open the connection to the server | |
324 | (setq process (open-network-stream "SMTP" process-buffer host port)) | |
325 | (and (null process) (throw 'done nil)) | |
326 | ||
327 | ;; set the send-filter | |
328 | (set-process-filter process 'smtpmail-process-filter) | |
329 | ||
330 | (save-excursion | |
331 | (set-buffer process-buffer) | |
4b876894 | 332 | (set-buffer-process-coding-system 'raw-text-unix 'raw-text-unix) |
24975917 RS |
333 | (make-local-variable 'smtpmail-read-point) |
334 | (setq smtpmail-read-point (point-min)) | |
335 | ||
336 | ||
337 | (if (or (null (car (setq greeting (smtpmail-read-response process)))) | |
338 | (not (integerp (car greeting))) | |
339 | (>= (car greeting) 400)) | |
340 | (throw 'done nil) | |
341 | ) | |
342 | ||
c50d5ce0 RS |
343 | ;; EHLO |
344 | (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn))) | |
24975917 RS |
345 | |
346 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
347 | (not (integerp (car response-code))) | |
348 | (>= (car response-code) 400)) | |
c50d5ce0 RS |
349 | (progn |
350 | ;; HELO | |
351 | (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn))) | |
352 | ||
353 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
354 | (not (integerp (car response-code))) | |
355 | (>= (car response-code) 400)) | |
356 | (throw 'done nil))) | |
357 | (let ((extension-lines (cdr (cdr response-code)))) | |
358 | (while extension-lines | |
2c79f5b8 | 359 | (let ((name (intern (downcase (car (split-string (substring (car extension-lines) 4) "[ ]")))))) |
c50d5ce0 RS |
360 | (and name |
361 | (cond ((memq name '(verb xvrb 8bitmime onex xone | |
362 | expn size dsn etrn | |
363 | help xusr)) | |
364 | (setq supported-extensions | |
365 | (cons name supported-extensions))) | |
366 | (t (message "unknown extension %s" | |
367 | name))))) | |
368 | (setq extension-lines (cdr extension-lines))))) | |
369 | ||
370 | (if (or (member 'onex supported-extensions) | |
371 | (member 'xone supported-extensions)) | |
372 | (progn | |
373 | (smtpmail-send-command process (format "ONEX")) | |
374 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
375 | (not (integerp (car response-code))) | |
376 | (>= (car response-code) 400)) | |
377 | (throw 'done nil)))) | |
378 | ||
379 | (if (and smtpmail-debug-info | |
380 | (or (member 'verb supported-extensions) | |
381 | (member 'xvrb supported-extensions))) | |
382 | (progn | |
383 | (smtpmail-send-command process (format "VERB")) | |
384 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
385 | (not (integerp (car response-code))) | |
386 | (>= (car response-code) 400)) | |
387 | (throw 'done nil)))) | |
388 | ||
389 | (if (member 'xusr supported-extensions) | |
390 | (progn | |
391 | (smtpmail-send-command process (format "XUSR")) | |
392 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
393 | (not (integerp (car response-code))) | |
394 | (>= (car response-code) 400)) | |
395 | (throw 'done nil)))) | |
24975917 RS |
396 | |
397 | ;; MAIL FROM: <sender> | |
c50d5ce0 RS |
398 | (let ((size-part |
399 | (if (member 'size supported-extensions) | |
400 | (format " SIZE=%d" | |
401 | (save-excursion | |
402 | (set-buffer smtpmail-text-buffer) | |
403 | ;; size estimate: | |
404 | (+ (- (point-max) (point-min)) | |
405 | ;; Add one byte for each change-of-line | |
406 | ;; because or CR-LF representation: | |
407 | (count-lines (point-min) (point-max)) | |
408 | ;; For some reason, an empty line is | |
409 | ;; added to the message. Maybe this | |
410 | ;; is a bug, but it can't hurt to add | |
411 | ;; those two bytes anyway: | |
412 | 2))) | |
413 | "")) | |
414 | (body-part | |
415 | (if (member '8bitmime supported-extensions) | |
416 | ;; FIXME: | |
417 | ;; Code should be added here that transforms | |
418 | ;; the contents of the message buffer into | |
419 | ;; something the receiving SMTP can handle. | |
420 | ;; For a receiver that supports 8BITMIME, this | |
421 | ;; may mean converting BINARY to BASE64, or | |
422 | ;; adding Content-Transfer-Encoding and the | |
423 | ;; other MIME headers. The code should also | |
424 | ;; return an indication of what encoding the | |
425 | ;; message buffer is now, i.e. ASCII or | |
426 | ;; 8BITMIME. | |
427 | (if nil | |
428 | " BODY=8BITMIME" | |
429 | "") | |
430 | ""))) | |
431 | ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn))) | |
432 | (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s" | |
433 | user-mail-address | |
434 | size-part | |
435 | body-part)) | |
436 | ||
437 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
438 | (not (integerp (car response-code))) | |
439 | (>= (car response-code) 400)) | |
440 | (throw 'done nil) | |
441 | )) | |
24975917 RS |
442 | |
443 | ;; RCPT TO: <recipient> | |
8805249b RS |
444 | (let ((n 0)) |
445 | (while (not (null (nth n recipient))) | |
b21dc761 | 446 | (smtpmail-send-command process (format "RCPT TO: <%s>" (nth n recipient))) |
8805249b RS |
447 | (setq n (1+ n)) |
448 | ||
c50d5ce0 RS |
449 | (setq response-code (smtpmail-read-response process)) |
450 | (if (or (null (car response-code)) | |
8805249b RS |
451 | (not (integerp (car response-code))) |
452 | (>= (car response-code) 400)) | |
453 | (throw 'done nil) | |
454 | ) | |
455 | )) | |
24975917 RS |
456 | |
457 | ;; DATA | |
458 | (smtpmail-send-command process "DATA") | |
459 | ||
460 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
461 | (not (integerp (car response-code))) | |
462 | (>= (car response-code) 400)) | |
463 | (throw 'done nil) | |
464 | ) | |
465 | ||
466 | ;; Mail contents | |
467 | (smtpmail-send-data process smtpmail-text-buffer) | |
468 | ||
469 | ;;DATA end "." | |
470 | (smtpmail-send-command process ".") | |
471 | ||
472 | (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
473 | (not (integerp (car response-code))) | |
474 | (>= (car response-code) 400)) | |
475 | (throw 'done nil) | |
476 | ) | |
477 | ||
478 | ;;QUIT | |
479 | ; (smtpmail-send-command process "QUIT") | |
480 | ; (and (null (car (smtpmail-read-response process))) | |
481 | ; (throw 'done nil)) | |
482 | t )) | |
483 | (if process | |
484 | (save-excursion | |
485 | (set-buffer (process-buffer process)) | |
486 | (smtpmail-send-command process "QUIT") | |
487 | (smtpmail-read-response process) | |
488 | ||
489 | ; (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
490 | ; (not (integerp (car response-code))) | |
491 | ; (>= (car response-code) 400)) | |
492 | ; (throw 'done nil) | |
493 | ; ) | |
494 | (delete-process process)))))) | |
495 | ||
496 | ||
24975917 RS |
497 | (defun smtpmail-process-filter (process output) |
498 | (save-excursion | |
499 | (set-buffer (process-buffer process)) | |
500 | (goto-char (point-max)) | |
501 | (insert output))) | |
502 | ||
24975917 RS |
503 | (defun smtpmail-read-response (process) |
504 | (let ((case-fold-search nil) | |
c50d5ce0 | 505 | (response-strings nil) |
24975917 | 506 | (response-continue t) |
c50d5ce0 | 507 | (return-value '(nil ())) |
24975917 RS |
508 | match-end) |
509 | ||
24975917 | 510 | (while response-continue |
c8d16dbd | 511 | (goto-char smtpmail-read-point) |
24975917 RS |
512 | (while (not (search-forward "\r\n" nil t)) |
513 | (accept-process-output process) | |
514 | (goto-char smtpmail-read-point)) | |
515 | ||
516 | (setq match-end (point)) | |
c50d5ce0 RS |
517 | (setq response-strings |
518 | (cons (buffer-substring smtpmail-read-point (- match-end 2)) | |
519 | response-strings)) | |
24975917 RS |
520 | |
521 | (goto-char smtpmail-read-point) | |
522 | (if (looking-at "[0-9]+ ") | |
c50d5ce0 RS |
523 | (let ((begin (match-beginning 0)) |
524 | (end (match-end 0))) | |
525 | (if smtpmail-debug-info | |
526 | (message "%s" (car response-strings))) | |
24975917 | 527 | |
c50d5ce0 | 528 | (setq smtpmail-read-point match-end) |
24975917 | 529 | |
c50d5ce0 RS |
530 | ;; ignore lines that start with "0" |
531 | (if (looking-at "0[0-9]+ ") | |
532 | nil | |
533 | (setq response-continue nil) | |
534 | (setq return-value | |
535 | (cons (string-to-int | |
536 | (buffer-substring begin end)) | |
537 | (nreverse response-strings))))) | |
24975917 RS |
538 | |
539 | (if (looking-at "[0-9]+-") | |
c50d5ce0 RS |
540 | (progn (if smtpmail-debug-info |
541 | (message "%s" (car response-strings))) | |
542 | (setq smtpmail-read-point match-end) | |
24975917 RS |
543 | (setq response-continue t)) |
544 | (progn | |
545 | (setq smtpmail-read-point match-end) | |
546 | (setq response-continue nil) | |
547 | (setq return-value | |
c50d5ce0 | 548 | (cons nil (nreverse response-strings))) |
24975917 RS |
549 | ) |
550 | ))) | |
551 | (setq smtpmail-read-point match-end) | |
552 | return-value)) | |
553 | ||
554 | ||
24975917 RS |
555 | (defun smtpmail-send-command (process command) |
556 | (goto-char (point-max)) | |
557 | (if (= (aref command 0) ?P) | |
558 | (insert "PASS <omitted>\r\n") | |
559 | (insert command "\r\n")) | |
560 | (setq smtpmail-read-point (point)) | |
561 | (process-send-string process command) | |
562 | (process-send-string process "\r\n")) | |
563 | ||
24975917 RS |
564 | (defun smtpmail-send-data-1 (process data) |
565 | (goto-char (point-max)) | |
566 | ||
fb035bbf RS |
567 | (when smtpmail-code-conv-from |
568 | (setq data (encode-coding-string data *internal* smtpmail-code-conv-from))) | |
24975917 RS |
569 | |
570 | (if smtpmail-debug-info | |
571 | (insert data "\r\n")) | |
572 | ||
573 | (setq smtpmail-read-point (point)) | |
57810560 KH |
574 | ;; Escape "." at start of a line |
575 | (if (eq (string-to-char data) ?.) | |
24975917 | 576 | (process-send-string process ".")) |
57810560 | 577 | (process-send-string process data) |
24975917 RS |
578 | (process-send-string process "\r\n") |
579 | ) | |
580 | ||
581 | (defun smtpmail-send-data (process buffer) | |
582 | (let | |
583 | ((data-continue t) | |
584 | (sending-data nil) | |
585 | this-line | |
586 | this-line-end) | |
587 | ||
588 | (save-excursion | |
589 | (set-buffer buffer) | |
590 | (goto-char (point-min))) | |
591 | ||
592 | (while data-continue | |
593 | (save-excursion | |
594 | (set-buffer buffer) | |
595 | (beginning-of-line) | |
596 | (setq this-line (point)) | |
597 | (end-of-line) | |
598 | (setq this-line-end (point)) | |
599 | (setq sending-data nil) | |
600 | (setq sending-data (buffer-substring this-line this-line-end)) | |
601 | (if (/= (forward-line 1) 0) | |
602 | (setq data-continue nil))) | |
603 | ||
604 | (smtpmail-send-data-1 process sending-data) | |
605 | ) | |
606 | ) | |
607 | ) | |
608 | ||
609 | ||
610 | (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end) | |
611 | "Get address list suitable for smtp RCPT TO: <address>." | |
612 | (require 'mail-utils) ;; pick up mail-strip-quoted-names | |
613 | (let | |
614 | ((case-fold-search t) | |
615 | (simple-address-list "") | |
616 | this-line | |
13f1d088 KH |
617 | this-line-end |
618 | addr-regexp) | |
24975917 RS |
619 | |
620 | (unwind-protect | |
621 | (save-excursion | |
622 | ;; | |
623 | (set-buffer smtpmail-address-buffer) (erase-buffer) | |
624 | (insert-buffer-substring smtpmail-text-buffer header-start header-end) | |
625 | (goto-char (point-min)) | |
13f1d088 KH |
626 | ;; RESENT-* fields should stop processing of regular fields. |
627 | (save-excursion | |
5feeeae2 RS |
628 | (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" header-end t) |
629 | (setq addr-regexp "^Resent-\\(to\\|cc\\|bcc\\):") | |
630 | (setq addr-regexp "^\\(To:\\|Cc:\\|Bcc:\\)"))) | |
13f1d088 KH |
631 | |
632 | (while (re-search-forward addr-regexp header-end t) | |
24975917 RS |
633 | (replace-match "") |
634 | (setq this-line (match-beginning 0)) | |
635 | (forward-line 1) | |
636 | ;; get any continuation lines | |
637 | (while (and (looking-at "^[ \t]+") (< (point) header-end)) | |
638 | (forward-line 1)) | |
639 | (setq this-line-end (point-marker)) | |
640 | (setq simple-address-list | |
641 | (concat simple-address-list " " | |
642 | (mail-strip-quoted-names (buffer-substring this-line this-line-end)))) | |
643 | ) | |
644 | (erase-buffer) | |
645 | (insert-string " ") | |
646 | (insert-string simple-address-list) | |
647 | (insert-string "\n") | |
648 | (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank | |
649 | (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank | |
650 | (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank | |
651 | ||
652 | (goto-char (point-min)) | |
653 | ;; tidyness in case hook is not robust when it looks at this | |
654 | (while (re-search-forward "[ \t]+" header-end t) (replace-match " ")) | |
655 | ||
656 | (goto-char (point-min)) | |
8805249b | 657 | (let (recipient-address-list) |
e2f7c221 | 658 | (while (re-search-forward " \\([^ ]+\\) " (point-max) t) |
8805249b | 659 | (backward-char 1) |
e2f7c221 RS |
660 | (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1)) |
661 | recipient-address-list)) | |
8805249b RS |
662 | ) |
663 | (setq smtpmail-recipient-address-list recipient-address-list)) | |
24975917 RS |
664 | |
665 | ) | |
666 | ) | |
667 | ) | |
668 | ) | |
669 | ||
670 | ||
671 | (defun smtpmail-do-bcc (header-end) | |
5feeeae2 | 672 | "Delete [Resent-]BCC: and their continuation lines from the header area. |
24975917 RS |
673 | There may be multiple BCC: lines, and each may have arbitrarily |
674 | many continuation lines." | |
675 | (let ((case-fold-search t)) | |
067427f5 KH |
676 | (save-excursion |
677 | (goto-char (point-min)) | |
678 | ;; iterate over all BCC: lines | |
679 | (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t) | |
680 | (delete-region (match-beginning 0) | |
681 | (progn (forward-line 1) (point))) | |
682 | ;; get rid of any continuation lines | |
683 | (while (and (looking-at "^[ \t].*\n") (< (point) header-end)) | |
684 | (replace-match "")))))) | |
24975917 RS |
685 | |
686 | ||
687 | (provide 'smtpmail) | |
688 | ||
092af6d8 | 689 | ;;; smtpmail.el ends here |