(hs-show-hidden-short-form): Updated doc string.
[bpt/emacs.git] / lisp / mail / smtpmail.el
CommitLineData
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
RS
30
31;; Please add these lines in your .emacs(_emacs).
32;;
33;;(setq send-mail-function 'smtpmail-send-it)
34;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
35;;(setq smtpmail-smtp-service "smtp")
36;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
37;;(setq smtpmail-debug-info t)
38;;(load-library "smtpmail")
39;;(setq smtpmail-code-conv-from nil)
0e2701ca 40;;(setq user-full-name "YOUR NAME HERE")
24975917
RS
41
42;;; Code:
43
44(require 'sendmail)
45
46;;;
00ed33e7
RS
47(defgroup smtpmail nil
48 "SMTP protocol for sending mail."
49 :group 'mail)
24975917 50
00ed33e7
RS
51
52(defcustom smtpmail-default-smtp-server nil
53 "*Specify default SMTP server."
54 :type '(choice (const nil) string)
55 :group 'smtpmail)
56
57(defcustom smtpmail-smtp-server
e2f7c221 58 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server)
00ed33e7
RS
59 "*The name of the host running SMTP server."
60 :type '(choice (const nil) string)
61 :group 'smtpmail)
24975917 62
00ed33e7
RS
63(defcustom smtpmail-smtp-service 25
64 "*SMTP service port number. smtp or 25 ."
65 :type 'integer
66 :group 'smtpmail)
24975917 67
00ed33e7 68(defcustom smtpmail-local-domain nil
24975917
RS
69 "*Local domain name without a host name.
70If the function (system-name) returns the full internet address,
00ed33e7
RS
71don't define this value."
72 :type '(choice (const nil) string)
73 :group 'smtpmail)
74
75(defcustom smtpmail-debug-info nil
76 "*smtpmail debug info printout. messages and process buffer."
77 :type 'boolean
78 :group 'smtpmail)
79
80(defcustom smtpmail-code-conv-from nil ;; *junet*
81 "*smtpmail code convert from this code to *internal*..for tiny-mime.."
82 :type 'boolean
83 :group 'smtpmail)
24975917
RS
84
85;;;
86;;;
87;;;
88
89(defun smtpmail-send-it ()
e2f7c221 90 (require 'mail-utils)
24975917
RS
91 (let ((errbuf (if mail-interactive
92 (generate-new-buffer " smtpmail errors")
93 0))
94 (tembuf (generate-new-buffer " smtpmail temp"))
95 (case-fold-search nil)
96 resend-to-addresses
97 delimline
98 (mailbuf (current-buffer)))
99 (unwind-protect
100 (save-excursion
101 (set-buffer tembuf)
102 (erase-buffer)
103 (insert-buffer-substring mailbuf)
104 (goto-char (point-max))
105 ;; require one newline at the end.
106 (or (= (preceding-char) ?\n)
107 (insert ?\n))
108 ;; Change header-delimiter to be what sendmail expects.
109 (goto-char (point-min))
110 (re-search-forward
111 (concat "^" (regexp-quote mail-header-separator) "\n"))
112 (replace-match "\n")
113 (backward-char 1)
114 (setq delimline (point-marker))
e2f7c221 115;; (sendmail-synch-aliases)
24975917
RS
116 (if mail-aliases
117 (expand-mail-aliases (point-min) delimline))
118 (goto-char (point-min))
119 ;; ignore any blank lines in the header
120 (while (and (re-search-forward "\n\n\n*" delimline t)
121 (< (point) delimline))
122 (replace-match "\n"))
123 (let ((case-fold-search t))
124 (goto-char (point-min))
24975917 125 (goto-char (point-min))
24975917
RS
126 (while (re-search-forward "^Resent-to:" delimline t)
127 (setq resend-to-addresses
128 (save-restriction
129 (narrow-to-region (point)
130 (save-excursion
131 (end-of-line)
132 (point)))
133 (append (mail-parse-comma-list)
134 resend-to-addresses))))
135;;; Apparently this causes a duplicate Sender.
136;;; ;; If the From is different than current user, insert Sender.
137;;; (goto-char (point-min))
138;;; (and (re-search-forward "^From:" delimline t)
139;;; (progn
140;;; (require 'mail-utils)
141;;; (not (string-equal
142;;; (mail-strip-quoted-names
143;;; (save-restriction
144;;; (narrow-to-region (point-min) delimline)
145;;; (mail-fetch-field "From")))
146;;; (user-login-name))))
147;;; (progn
148;;; (forward-line 1)
149;;; (insert "Sender: " (user-login-name) "\n")))
24975917
RS
150 ;; Don't send out a blank subject line
151 (goto-char (point-min))
152 (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
153 (replace-match ""))
0e2701ca
RS
154 ;; Put the "From:" field in unless for some odd reason
155 ;; they put one in themselves.
156 (goto-char (point-min))
157 (if (not (re-search-forward "^From:" delimline t))
158 (let* ((login user-mail-address)
159 (fullname (user-full-name)))
160 (cond ((eq mail-from-style 'angles)
161 (insert "From: " fullname)
162 (let ((fullname-start (+ (point-min) 6))
163 (fullname-end (point-marker)))
164 (goto-char fullname-start)
165 ;; Look for a character that cannot appear unquoted
166 ;; according to RFC 822.
167 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
168 fullname-end 1)
169 (progn
170 ;; Quote fullname, escaping specials.
171 (goto-char fullname-start)
172 (insert "\"")
173 (while (re-search-forward "[\"\\]"
174 fullname-end 1)
175 (replace-match "\\\\\\&" t))
176 (insert "\""))))
177 (insert " <" login ">\n"))
178 ((eq mail-from-style 'parens)
179 (insert "From: " login " (")
180 (let ((fullname-start (point)))
181 (insert fullname)
182 (let ((fullname-end (point-marker)))
183 (goto-char fullname-start)
184 ;; RFC 822 says \ and nonmatching parentheses
185 ;; must be escaped in comments.
186 ;; Escape every instance of ()\ ...
187 (while (re-search-forward "[()\\]" fullname-end 1)
188 (replace-match "\\\\\\&" t))
189 ;; ... then undo escaping of matching parentheses,
190 ;; including matching nested parentheses.
191 (goto-char fullname-start)
192 (while (re-search-forward
193 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
194 fullname-end 1)
195 (replace-match "\\1(\\3)" t)
196 (goto-char fullname-start))))
197 (insert ")\n"))
198 ((null mail-from-style)
199 (insert "From: " login "\n")))))
24975917
RS
200 ;; Insert an extra newline if we need it to work around
201 ;; Sun's bug that swallows newlines.
202 (goto-char (1+ delimline))
203 (if (eval mail-mailer-swallows-blank-line)
204 (newline))
0e2701ca
RS
205 ;; Find and handle any FCC fields.
206 (goto-char (point-min))
207 (if (re-search-forward "^FCC:" delimline t)
208 (mail-do-fcc delimline))
24975917
RS
209 (if mail-interactive
210 (save-excursion
211 (set-buffer errbuf)
212 (erase-buffer))))
213 ;;
214 ;;
215 ;;
216 (setq smtpmail-address-buffer (generate-new-buffer "*smtp-mail*"))
217 (setq smtpmail-recipient-address-list
52a2a9c3
MB
218 (or resend-to-addresses
219 (smtpmail-deduce-address-list tembuf (point-min) delimline)))
24975917
RS
220 (kill-buffer smtpmail-address-buffer)
221
222 (smtpmail-do-bcc delimline)
223
224 (if (not (null smtpmail-recipient-address-list))
225 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list tembuf))
8805249b
RS
226 (error "Sending failed; SMTP protocol error"))
227 (error "Sending failed; no recipients"))
24975917
RS
228 )
229 (kill-buffer tembuf)
230 (if (bufferp errbuf)
231 (kill-buffer errbuf)))))
232
233
234;(defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
235
236(defun smtpmail-fqdn ()
237 (if smtpmail-local-domain
238 (concat (system-name) "." smtpmail-local-domain)
239 (system-name)))
240
241(defun smtpmail-via-smtp (recipient smtpmail-text-buffer)
242 (let ((process nil)
8805249b
RS
243 (host smtpmail-smtp-server)
244 (port smtpmail-smtp-service)
24975917 245 response-code
8805249b 246 greeting
c50d5ce0
RS
247 process-buffer
248 (supported-extensions '()))
24975917
RS
249 (unwind-protect
250 (catch 'done
251 ;; get or create the trace buffer
252 (setq process-buffer
253 (get-buffer-create (format "*trace of SMTP session to %s*" host)))
254
255 ;; clear the trace buffer of old output
256 (save-excursion
257 (set-buffer process-buffer)
258 (erase-buffer))
259
260 ;; open the connection to the server
261 (setq process (open-network-stream "SMTP" process-buffer host port))
262 (and (null process) (throw 'done nil))
263
264 ;; set the send-filter
265 (set-process-filter process 'smtpmail-process-filter)
266
267 (save-excursion
268 (set-buffer process-buffer)
269 (make-local-variable 'smtpmail-read-point)
270 (setq smtpmail-read-point (point-min))
271
272
273 (if (or (null (car (setq greeting (smtpmail-read-response process))))
274 (not (integerp (car greeting)))
275 (>= (car greeting) 400))
276 (throw 'done nil)
277 )
278
c50d5ce0
RS
279 ;; EHLO
280 (smtpmail-send-command process (format "EHLO %s" (smtpmail-fqdn)))
24975917
RS
281
282 (if (or (null (car (setq response-code (smtpmail-read-response process))))
283 (not (integerp (car response-code)))
284 (>= (car response-code) 400))
c50d5ce0
RS
285 (progn
286 ;; HELO
287 (smtpmail-send-command process (format "HELO %s" (smtpmail-fqdn)))
288
289 (if (or (null (car (setq response-code (smtpmail-read-response process))))
290 (not (integerp (car response-code)))
291 (>= (car response-code) 400))
292 (throw 'done nil)))
293 (let ((extension-lines (cdr (cdr response-code))))
294 (while extension-lines
295 (let ((name (intern (downcase (substring (car extension-lines) 4)))))
296 (and name
297 (cond ((memq name '(verb xvrb 8bitmime onex xone
298 expn size dsn etrn
299 help xusr))
300 (setq supported-extensions
301 (cons name supported-extensions)))
302 (t (message "unknown extension %s"
303 name)))))
304 (setq extension-lines (cdr extension-lines)))))
305
306 (if (or (member 'onex supported-extensions)
307 (member 'xone supported-extensions))
308 (progn
309 (smtpmail-send-command process (format "ONEX"))
310 (if (or (null (car (setq response-code (smtpmail-read-response process))))
311 (not (integerp (car response-code)))
312 (>= (car response-code) 400))
313 (throw 'done nil))))
314
315 (if (and smtpmail-debug-info
316 (or (member 'verb supported-extensions)
317 (member 'xvrb supported-extensions)))
318 (progn
319 (smtpmail-send-command process (format "VERB"))
320 (if (or (null (car (setq response-code (smtpmail-read-response process))))
321 (not (integerp (car response-code)))
322 (>= (car response-code) 400))
323 (throw 'done nil))))
324
325 (if (member 'xusr supported-extensions)
326 (progn
327 (smtpmail-send-command process (format "XUSR"))
328 (if (or (null (car (setq response-code (smtpmail-read-response process))))
329 (not (integerp (car response-code)))
330 (>= (car response-code) 400))
331 (throw 'done nil))))
24975917
RS
332
333 ;; MAIL FROM: <sender>
c50d5ce0
RS
334 (let ((size-part
335 (if (member 'size supported-extensions)
336 (format " SIZE=%d"
337 (save-excursion
338 (set-buffer smtpmail-text-buffer)
339 ;; size estimate:
340 (+ (- (point-max) (point-min))
341 ;; Add one byte for each change-of-line
342 ;; because or CR-LF representation:
343 (count-lines (point-min) (point-max))
344 ;; For some reason, an empty line is
345 ;; added to the message. Maybe this
346 ;; is a bug, but it can't hurt to add
347 ;; those two bytes anyway:
348 2)))
349 ""))
350 (body-part
351 (if (member '8bitmime supported-extensions)
352 ;; FIXME:
353 ;; Code should be added here that transforms
354 ;; the contents of the message buffer into
355 ;; something the receiving SMTP can handle.
356 ;; For a receiver that supports 8BITMIME, this
357 ;; may mean converting BINARY to BASE64, or
358 ;; adding Content-Transfer-Encoding and the
359 ;; other MIME headers. The code should also
360 ;; return an indication of what encoding the
361 ;; message buffer is now, i.e. ASCII or
362 ;; 8BITMIME.
363 (if nil
364 " BODY=8BITMIME"
365 "")
366 "")))
367; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
368 (smtpmail-send-command process (format "MAIL FROM: <%s>%s%s"
369 user-mail-address
370 size-part
371 body-part))
372
373 (if (or (null (car (setq response-code (smtpmail-read-response process))))
374 (not (integerp (car response-code)))
375 (>= (car response-code) 400))
376 (throw 'done nil)
377 ))
24975917
RS
378
379 ;; RCPT TO: <recipient>
8805249b
RS
380 (let ((n 0))
381 (while (not (null (nth n recipient)))
b21dc761 382 (smtpmail-send-command process (format "RCPT TO: <%s>" (nth n recipient)))
8805249b
RS
383 (setq n (1+ n))
384
c50d5ce0
RS
385 (setq response-code (smtpmail-read-response process))
386 (if (or (null (car response-code))
8805249b
RS
387 (not (integerp (car response-code)))
388 (>= (car response-code) 400))
389 (throw 'done nil)
390 )
391 ))
24975917
RS
392
393 ;; DATA
394 (smtpmail-send-command process "DATA")
395
396 (if (or (null (car (setq response-code (smtpmail-read-response process))))
397 (not (integerp (car response-code)))
398 (>= (car response-code) 400))
399 (throw 'done nil)
400 )
401
402 ;; Mail contents
403 (smtpmail-send-data process smtpmail-text-buffer)
404
405 ;;DATA end "."
406 (smtpmail-send-command process ".")
407
408 (if (or (null (car (setq response-code (smtpmail-read-response process))))
409 (not (integerp (car response-code)))
410 (>= (car response-code) 400))
411 (throw 'done nil)
412 )
413
414 ;;QUIT
415; (smtpmail-send-command process "QUIT")
416; (and (null (car (smtpmail-read-response process)))
417; (throw 'done nil))
418 t ))
419 (if process
420 (save-excursion
421 (set-buffer (process-buffer process))
422 (smtpmail-send-command process "QUIT")
423 (smtpmail-read-response process)
424
425; (if (or (null (car (setq response-code (smtpmail-read-response process))))
426; (not (integerp (car response-code)))
427; (>= (car response-code) 400))
428; (throw 'done nil)
429; )
430 (delete-process process))))))
431
432
24975917
RS
433(defun smtpmail-process-filter (process output)
434 (save-excursion
435 (set-buffer (process-buffer process))
436 (goto-char (point-max))
437 (insert output)))
438
24975917
RS
439(defun smtpmail-read-response (process)
440 (let ((case-fold-search nil)
c50d5ce0 441 (response-strings nil)
24975917 442 (response-continue t)
c50d5ce0 443 (return-value '(nil ()))
24975917
RS
444 match-end)
445
24975917 446 (while response-continue
c8d16dbd 447 (goto-char smtpmail-read-point)
24975917
RS
448 (while (not (search-forward "\r\n" nil t))
449 (accept-process-output process)
450 (goto-char smtpmail-read-point))
451
452 (setq match-end (point))
c50d5ce0
RS
453 (setq response-strings
454 (cons (buffer-substring smtpmail-read-point (- match-end 2))
455 response-strings))
24975917
RS
456
457 (goto-char smtpmail-read-point)
458 (if (looking-at "[0-9]+ ")
c50d5ce0
RS
459 (let ((begin (match-beginning 0))
460 (end (match-end 0)))
461 (if smtpmail-debug-info
462 (message "%s" (car response-strings)))
24975917 463
c50d5ce0 464 (setq smtpmail-read-point match-end)
24975917 465
c50d5ce0
RS
466 ;; ignore lines that start with "0"
467 (if (looking-at "0[0-9]+ ")
468 nil
469 (setq response-continue nil)
470 (setq return-value
471 (cons (string-to-int
472 (buffer-substring begin end))
473 (nreverse response-strings)))))
24975917
RS
474
475 (if (looking-at "[0-9]+-")
c50d5ce0
RS
476 (progn (if smtpmail-debug-info
477 (message "%s" (car response-strings)))
478 (setq smtpmail-read-point match-end)
24975917
RS
479 (setq response-continue t))
480 (progn
481 (setq smtpmail-read-point match-end)
482 (setq response-continue nil)
483 (setq return-value
c50d5ce0 484 (cons nil (nreverse response-strings)))
24975917
RS
485 )
486 )))
487 (setq smtpmail-read-point match-end)
488 return-value))
489
490
24975917
RS
491(defun smtpmail-send-command (process command)
492 (goto-char (point-max))
493 (if (= (aref command 0) ?P)
494 (insert "PASS <omitted>\r\n")
495 (insert command "\r\n"))
496 (setq smtpmail-read-point (point))
497 (process-send-string process command)
498 (process-send-string process "\r\n"))
499
24975917
RS
500(defun smtpmail-send-data-1 (process data)
501 (goto-char (point-max))
502
503 (if (not (null smtpmail-code-conv-from))
504 (setq data (code-convert-string data smtpmail-code-conv-from *internal*)))
505
506 (if smtpmail-debug-info
507 (insert data "\r\n"))
508
509 (setq smtpmail-read-point (point))
57810560
KH
510 ;; Escape "." at start of a line
511 (if (eq (string-to-char data) ?.)
24975917 512 (process-send-string process "."))
57810560 513 (process-send-string process data)
24975917
RS
514 (process-send-string process "\r\n")
515 )
516
517(defun smtpmail-send-data (process buffer)
518 (let
519 ((data-continue t)
520 (sending-data nil)
521 this-line
522 this-line-end)
523
524 (save-excursion
525 (set-buffer buffer)
526 (goto-char (point-min)))
527
528 (while data-continue
529 (save-excursion
530 (set-buffer buffer)
531 (beginning-of-line)
532 (setq this-line (point))
533 (end-of-line)
534 (setq this-line-end (point))
535 (setq sending-data nil)
536 (setq sending-data (buffer-substring this-line this-line-end))
537 (if (/= (forward-line 1) 0)
538 (setq data-continue nil)))
539
540 (smtpmail-send-data-1 process sending-data)
541 )
542 )
543 )
544
545
546(defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end)
547 "Get address list suitable for smtp RCPT TO: <address>."
548 (require 'mail-utils) ;; pick up mail-strip-quoted-names
549 (let
550 ((case-fold-search t)
551 (simple-address-list "")
552 this-line
13f1d088
KH
553 this-line-end
554 addr-regexp)
24975917
RS
555
556 (unwind-protect
557 (save-excursion
558 ;;
559 (set-buffer smtpmail-address-buffer) (erase-buffer)
560 (insert-buffer-substring smtpmail-text-buffer header-start header-end)
561 (goto-char (point-min))
13f1d088
KH
562 ;; RESENT-* fields should stop processing of regular fields.
563 (save-excursion
564 (if (re-search-forward "^RESENT-TO:" header-end t)
565 (setq addr-regexp "^\\(RESENT-TO:\\|RESENT-CC:\\|RESENT-BCC:\\)")
566 (setq addr-regexp "^\\(TO:\\|CC:\\|BCC:\\)")))
567
568 (while (re-search-forward addr-regexp header-end t)
24975917
RS
569 (replace-match "")
570 (setq this-line (match-beginning 0))
571 (forward-line 1)
572 ;; get any continuation lines
573 (while (and (looking-at "^[ \t]+") (< (point) header-end))
574 (forward-line 1))
575 (setq this-line-end (point-marker))
576 (setq simple-address-list
577 (concat simple-address-list " "
578 (mail-strip-quoted-names (buffer-substring this-line this-line-end))))
579 )
580 (erase-buffer)
581 (insert-string " ")
582 (insert-string simple-address-list)
583 (insert-string "\n")
584 (subst-char-in-region (point-min) (point-max) 10 ? t);; newline --> blank
585 (subst-char-in-region (point-min) (point-max) ?, ? t);; comma --> blank
586 (subst-char-in-region (point-min) (point-max) 9 ? t);; tab --> blank
587
588 (goto-char (point-min))
589 ;; tidyness in case hook is not robust when it looks at this
590 (while (re-search-forward "[ \t]+" header-end t) (replace-match " "))
591
592 (goto-char (point-min))
8805249b 593 (let (recipient-address-list)
e2f7c221 594 (while (re-search-forward " \\([^ ]+\\) " (point-max) t)
8805249b 595 (backward-char 1)
e2f7c221
RS
596 (setq recipient-address-list (cons (buffer-substring (match-beginning 1) (match-end 1))
597 recipient-address-list))
8805249b
RS
598 )
599 (setq smtpmail-recipient-address-list recipient-address-list))
24975917
RS
600
601 )
602 )
603 )
604 )
605
606
607(defun smtpmail-do-bcc (header-end)
608 "Delete BCC: and their continuation lines from the header area.
609There may be multiple BCC: lines, and each may have arbitrarily
610many continuation lines."
611 (let ((case-fold-search t))
612 (save-excursion (goto-char (point-min))
613 ;; iterate over all BCC: lines
614 (while (re-search-forward "^BCC:" header-end t)
615 (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
616 ;; get rid of any continuation lines
617 (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
618 (replace-match ""))
619 )
620 ) ;; save-excursion
621 ) ;; let
622 )
623
624
625
626(provide 'smtpmail)
627
092af6d8 628;;; smtpmail.el ends here