Mercurial > emacs
annotate lisp/mail/smtpmail.el @ 15396:fbecc317220d
Whitespace changes.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 10 Jun 1996 21:15:47 +0000 |
parents | abcc218dcbbc |
children | 89c1e7fe879a |
rev | line source |
---|---|
15345 | 1 ;; Simple SMTP protocol (RFC 821) for sending mail |
2 | |
3 ;; Copyright (C) 1995 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp> | |
6 ;; Keywords: mail | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;; Send Mail to smtp host from smtpmail temp buffer. | |
28 ;; alfa release | |
29 | |
30 ;; Please add these lines in your .emacs(_emacs). | |
31 ;; | |
32 ;;(setq send-mail-function 'smtpmail-send-it) | |
33 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST") | |
34 ;;(setq smtpmail-smtp-service "smtp") | |
35 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME") | |
36 ;;(setq smtpmail-debug-info t) | |
37 ;;(load-library "smtpmail") | |
38 ;;(setq smtpmail-code-conv-from nil) | |
39 | |
40 ;;; Code: | |
41 | |
42 (require 'sendmail) | |
43 | |
44 ;;; | |
45 (defvar smtpmail-default-smtp-server nil | |
46 "*Specify default SMTP server.") | |
47 | |
48 (defvar smtpmail-smtp-server (or (getenv "SMTPSERVER") | |
49 smtpmail-default-smtp-server) | |
50 "*The name of the host running SMTP server.") | |
51 | |
52 (defvar smtpmail-smtp-service "smtp" | |
53 "*SMTP service port number. smtp or 25 .") | |
54 | |
55 (defvar smtpmail-local-domain nil | |
56 "*Local domain name without a host name. | |
57 If the function (system-name) returns the full internet address, | |
58 don't define this value.") | |
59 | |
60 (defvar smtpmail-debug-info nil | |
61 "*smtpmail debug info printout. messages and process buffer.") | |
62 | |
63 (defvar smtpmail-code-conv-from nil ;; *junet* | |
64 "*smtpmail code convert from this code to *internal*..for tiny-mime..") | |
65 | |
66 ;;; | |
67 ;;; | |
68 ;;; | |
69 | |
70 (defun smtpmail-send-it () | |
71 (let ((errbuf (if mail-interactive | |
72 (generate-new-buffer " smtpmail errors") | |
73 0)) | |
74 (tembuf (generate-new-buffer " smtpmail temp")) | |
75 (case-fold-search nil) | |
76 resend-to-addresses | |
77 delimline | |
78 (mailbuf (current-buffer))) | |
79 (unwind-protect | |
80 (save-excursion | |
81 (set-buffer tembuf) | |
82 (erase-buffer) | |
83 (insert-buffer-substring mailbuf) | |
84 (goto-char (point-max)) | |
85 ;; require one newline at the end. | |
86 (or (= (preceding-char) ?\n) | |
87 (insert ?\n)) | |
88 ;; Change header-delimiter to be what sendmail expects. | |
89 (goto-char (point-min)) | |
90 (re-search-forward | |
91 (concat "^" (regexp-quote mail-header-separator) "\n")) | |
92 (replace-match "\n") | |
93 (backward-char 1) | |
94 (setq delimline (point-marker)) | |
95 (if mail-aliases | |
96 (expand-mail-aliases (point-min) delimline)) | |
97 (goto-char (point-min)) | |
98 ;; ignore any blank lines in the header | |
99 (while (and (re-search-forward "\n\n\n*" delimline t) | |
100 (< (point) delimline)) | |
101 (replace-match "\n")) | |
102 (let ((case-fold-search t)) | |
103 (goto-char (point-min)) | |
104 ;; Find and handle any FCC fields. | |
105 (goto-char (point-min)) | |
106 (if (re-search-forward "^FCC:" delimline t) | |
107 (mail-do-fcc delimline)) | |
108 (goto-char (point-min)) | |
109 (require 'mail-utils) | |
110 (while (re-search-forward "^Resent-to:" delimline t) | |
111 (setq resend-to-addresses | |
112 (save-restriction | |
113 (narrow-to-region (point) | |
114 (save-excursion | |
115 (end-of-line) | |
116 (point))) | |
117 (append (mail-parse-comma-list) | |
118 resend-to-addresses)))) | |
119 ;;; Apparently this causes a duplicate Sender. | |
120 ;;; ;; If the From is different than current user, insert Sender. | |
121 ;;; (goto-char (point-min)) | |
122 ;;; (and (re-search-forward "^From:" delimline t) | |
123 ;;; (progn | |
124 ;;; (require 'mail-utils) | |
125 ;;; (not (string-equal | |
126 ;;; (mail-strip-quoted-names | |
127 ;;; (save-restriction | |
128 ;;; (narrow-to-region (point-min) delimline) | |
129 ;;; (mail-fetch-field "From"))) | |
130 ;;; (user-login-name)))) | |
131 ;;; (progn | |
132 ;;; (forward-line 1) | |
133 ;;; (insert "Sender: " (user-login-name) "\n"))) | |
134 ;; "S:" is an abbreviation for "Subject:". | |
135 (goto-char (point-min)) | |
136 (if (re-search-forward "^S:" delimline t) | |
137 (replace-match "Subject:")) | |
138 ;; Don't send out a blank subject line | |
139 (goto-char (point-min)) | |
140 (if (re-search-forward "^Subject:[ \t]*\n" delimline t) | |
141 (replace-match "")) | |
142 ;; Insert an extra newline if we need it to work around | |
143 ;; Sun's bug that swallows newlines. | |
144 (goto-char (1+ delimline)) | |
145 (if (eval mail-mailer-swallows-blank-line) | |
146 (newline)) | |
147 (if mail-interactive | |
148 (save-excursion | |
149 (set-buffer errbuf) | |
150 (erase-buffer)))) | |
151 ;; | |
152 ;; | |
153 ;; | |
154 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*")) | |
155 (setq smtpmail-recipient-address-list | |
156 (smtpmail-deduce-address-list tembuf (point-min) delimline)) | |
157 (kill-buffer smtpmail-address-buffer) | |
158 | |
159 (smtpmail-do-bcc delimline) | |
160 | |
161 (if (not (null smtpmail-recipient-address-list)) | |
162 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list tembuf)) | |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
163 (error "Sending failed; SMTP protocol error")) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
164 (error "Sending failed; no recipients")) |
15345 | 165 ) |
166 (kill-buffer tembuf) | |
167 (if (bufferp errbuf) | |
168 (kill-buffer errbuf))))) | |
169 | |
170 | |
171 ;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer) | |
172 | |
173 (defun smtpmail-fqdn () | |
174 (if smtpmail-local-domain | |
175 (concat (system-name) "." smtpmail-local-domain) | |
176 (system-name))) | |
177 | |
178 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer) | |
179 (let ((process nil) | |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
180 (host smtpmail-smtp-server) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
181 (port smtpmail-smtp-service) |
15345 | 182 response-code |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
183 greeting |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
184 process-buffer) |
15345 | 185 (unwind-protect |
186 (catch 'done | |
187 ;; get or create the trace buffer | |
188 (setq process-buffer | |
189 (get-buffer-create (format "*trace of SMTP session to %s*" host))) | |
190 | |
191 ;; clear the trace buffer of old output | |
192 (save-excursion | |
193 (set-buffer process-buffer) | |
194 (erase-buffer)) | |
195 | |
196 ;; open the connection to the server | |
197 (setq process (open-network-stream "SMTP" process-buffer host port)) | |
198 (and (null process) (throw 'done nil)) | |
199 | |
200 ;; set the send-filter | |
201 (set-process-filter process 'smtpmail-process-filter) | |
202 | |
203 (save-excursion | |
204 (set-buffer process-buffer) | |
205 (make-local-variable 'smtpmail-read-point) | |
206 (setq smtpmail-read-point (point-min)) | |
207 | |
208 | |
209 (if (or (null (car (setq greeting (smtpmail-read-response process)))) | |
210 (not (integerp (car greeting))) | |
211 (>= (car greeting) 400)) | |
212 (throw 'done nil) | |
213 ) | |
214 | |
215 ;; HELO | |
216 (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn))) | |
217 | |
218 (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
219 (not (integerp (car response-code))) | |
220 (>= (car response-code) 400)) | |
221 (throw 'done nil) | |
222 ) | |
223 | |
224 ;; MAIL FROM: <sender> | |
225 ; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn))) | |
226 (smtpmail-send-command process (format "MAIL FROM:%s" user-mail-address)) | |
227 | |
228 (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
229 (not (integerp (car response-code))) | |
230 (>= (car response-code) 400)) | |
231 (throw 'done nil) | |
232 ) | |
233 | |
234 ;; RCPT TO: <recipient> | |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
235 (let ((n 0)) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
236 (while (not (null (nth n recipient))) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
237 (smtpmail-send-command process (format "RCPT TO: %s" (nth n recipient))) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
238 (setq n (1+ n)) |
15345 | 239 |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
240 (if (or (null (car (setq response-code (smtpmail-read-response process)))) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
241 (not (integerp (car response-code))) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
242 (>= (car response-code) 400)) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
243 (throw 'done nil) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
244 ) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
245 )) |
15345 | 246 |
247 ;; DATA | |
248 (smtpmail-send-command process "DATA") | |
249 | |
250 (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
251 (not (integerp (car response-code))) | |
252 (>= (car response-code) 400)) | |
253 (throw 'done nil) | |
254 ) | |
255 | |
256 ;; Mail contents | |
257 (smtpmail-send-data process smtpmail-text-buffer) | |
258 | |
259 ;;DATA end "." | |
260 (smtpmail-send-command process ".") | |
261 | |
262 (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
263 (not (integerp (car response-code))) | |
264 (>= (car response-code) 400)) | |
265 (throw 'done nil) | |
266 ) | |
267 | |
268 ;;QUIT | |
269 ; (smtpmail-send-command process "QUIT") | |
270 ; (and (null (car (smtpmail-read-response process))) | |
271 ; (throw 'done nil)) | |
272 t )) | |
273 (if process | |
274 (save-excursion | |
275 (set-buffer (process-buffer process)) | |
276 (smtpmail-send-command process "QUIT") | |
277 (smtpmail-read-response process) | |
278 | |
279 ; (if (or (null (car (setq response-code (smtpmail-read-response process)))) | |
280 ; (not (integerp (car response-code))) | |
281 ; (>= (car response-code) 400)) | |
282 ; (throw 'done nil) | |
283 ; ) | |
284 (delete-process process)))))) | |
285 | |
286 | |
287 (defun smtpmail-process-filter (process output) | |
288 (save-excursion | |
289 (set-buffer (process-buffer process)) | |
290 (goto-char (point-max)) | |
291 (insert output))) | |
292 | |
293 (defun smtpmail-read-response (process) | |
294 (let ((case-fold-search nil) | |
295 (response-string nil) | |
296 (response-continue t) | |
297 (return-value '(nil "")) | |
298 match-end) | |
299 | |
300 ; (setq response-string nil) | |
301 ; (setq response-continue t) | |
302 ; (setq return-value '(nil "")) | |
303 | |
304 (goto-char smtpmail-read-point) | |
305 (while response-continue | |
306 (while (not (search-forward "\r\n" nil t)) | |
307 (accept-process-output process) | |
308 (goto-char smtpmail-read-point)) | |
309 | |
310 (setq match-end (point)) | |
311 (if (null response-string) | |
312 (setq response-string | |
313 (buffer-substring smtpmail-read-point (- match-end 2)))) | |
314 | |
315 (goto-char smtpmail-read-point) | |
316 (if (looking-at "[0-9]+ ") | |
317 (progn (setq response-continue nil) | |
318 ; (setq return-value response-string) | |
319 | |
320 (if smtpmail-debug-info | |
321 (message response-string)) | |
322 | |
323 (setq smtpmail-read-point match-end) | |
324 (setq return-value | |
325 (cons (string-to-int | |
326 (buffer-substring (match-beginning 0) (match-end 0))) | |
327 response-string))) | |
328 | |
329 (if (looking-at "[0-9]+-") | |
330 (progn (setq smtpmail-read-point match-end) | |
331 (setq response-continue t)) | |
332 (progn | |
333 (setq smtpmail-read-point match-end) | |
334 (setq response-continue nil) | |
335 (setq return-value | |
336 (cons nil response-string)) | |
337 ) | |
338 ))) | |
339 (setq smtpmail-read-point match-end) | |
340 return-value)) | |
341 | |
342 | |
343 (defun smtpmail-send-command (process command) | |
344 (goto-char (point-max)) | |
345 (if (= (aref command 0) ?P) | |
346 (insert "PASS <omitted>\r\n") | |
347 (insert command "\r\n")) | |
348 (setq smtpmail-read-point (point)) | |
349 (process-send-string process command) | |
350 (process-send-string process "\r\n")) | |
351 | |
352 (defun smtpmail-send-data-1 (process data) | |
353 (goto-char (point-max)) | |
354 | |
355 (if (not (null smtpmail-code-conv-from)) | |
356 (setq data (code-convert-string data smtpmail-code-conv-from *internal*))) | |
357 | |
358 (if smtpmail-debug-info | |
359 (insert data "\r\n")) | |
360 | |
361 (setq smtpmail-read-point (point)) | |
362 (process-send-string process data) | |
363 ;; . -> .. | |
364 (if (string-equal data ".") | |
365 (process-send-string process ".")) | |
366 (process-send-string process "\r\n") | |
367 ) | |
368 | |
369 (defun smtpmail-send-data (process buffer) | |
370 (let | |
371 ((data-continue t) | |
372 (sending-data nil) | |
373 this-line | |
374 this-line-end) | |
375 | |
376 (save-excursion | |
377 (set-buffer buffer) | |
378 (goto-char (point-min))) | |
379 | |
380 (while data-continue | |
381 (save-excursion | |
382 (set-buffer buffer) | |
383 (beginning-of-line) | |
384 (setq this-line (point)) | |
385 (end-of-line) | |
386 (setq this-line-end (point)) | |
387 (setq sending-data nil) | |
388 (setq sending-data (buffer-substring this-line this-line-end)) | |
389 (if (/= (forward-line 1) 0) | |
390 (setq data-continue nil))) | |
391 | |
392 (smtpmail-send-data-1 process sending-data) | |
393 ) | |
394 ) | |
395 ) | |
396 | |
397 | |
398 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end) | |
399 "Get address list suitable for smtp RCPT TO: <address>." | |
400 (require 'mail-utils) ;; pick up mail-strip-quoted-names | |
401 (let | |
402 ((case-fold-search t) | |
403 (simple-address-list "") | |
404 this-line | |
405 this-line-end) | |
406 | |
407 (unwind-protect | |
408 (save-excursion | |
409 ;; | |
410 (set-buffer smtpmail-address-buffer) (erase-buffer) | |
411 (insert-buffer-substring smtpmail-text-buffer header-start header-end) | |
412 (goto-char (point-min)) | |
413 (while (re-search-forward "^\\(TO:\\|CC:\\|BCC:\\)" header-end t) | |
414 (replace-match "") | |
415 (setq this-line (match-beginning 0)) | |
416 (forward-line 1) | |
417 ;; get any continuation lines | |
418 (while (and (looking-at "^[ \t]+") (< (point) header-end)) | |
419 (forward-line 1)) | |
420 (setq this-line-end (point-marker)) | |
421 (setq simple-address-list | |
422 (concat simple-address-list " " | |
423 (mail-strip-quoted-names (buffer-substring this-line this-line-end)))) | |
424 ) | |
425 (erase-buffer) | |
426 (insert-string " ") | |
427 (insert-string simple-address-list) | |
428 (insert-string "\n") | |
429 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank | |
430 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank | |
431 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank | |
432 | |
433 (goto-char (point-min)) | |
434 ;; tidyness in case hook is not robust when it looks at this | |
435 (while (re-search-forward "[ \t]+" header-end t) (replace-match " ")) | |
436 | |
437 (goto-char (point-min)) | |
15346
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
438 (let (recipient-address-list) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
439 (while (re-search-forward " [^ ]+ " (point-max) t) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
440 (backward-char 1) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
441 (setq recipient-address-list(cons (buffer-substring (match-beginning 0) (match-end 0)) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
442 recipient-address-list)) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
443 ) |
4cd6ff2384dc
(smtpmail-send-it): Fix error messages.
Richard M. Stallman <rms@gnu.org>
parents:
15345
diff
changeset
|
444 (setq smtpmail-recipient-address-list recipient-address-list)) |
15345 | 445 |
446 ) | |
447 ) | |
448 ) | |
449 ) | |
450 | |
451 | |
452 (defun smtpmail-do-bcc (header-end) | |
453 "Delete BCC: and their continuation lines from the header area. | |
454 There may be multiple BCC: lines, and each may have arbitrarily | |
455 many continuation lines." | |
456 (let ((case-fold-search t)) | |
457 (save-excursion (goto-char (point-min)) | |
458 ;; iterate over all BCC: lines | |
459 (while (re-search-forward "^BCC:" header-end t) | |
460 (delete-region (match-beginning 0) (progn (forward-line 1) (point))) | |
461 ;; get rid of any continuation lines | |
462 (while (and (looking-at "^[ \t].*\n") (< (point) header-end)) | |
463 (replace-match "")) | |
464 ) | |
465 ) ;; save-excursion | |
466 ) ;; let | |
467 ) | |
468 | |
469 | |
470 | |
471 (provide 'smtpmail) | |
472 | |
473 ;; smtpmail.el ends here |