New file.
[elisp/lemi.git] / mail / smtpmail.el
1 ;;; smtpmail.el --- SMTP interface for mail-mode
2
3 ;; Copyright (C) 1995, 1996, 1998, 1999, 2001 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 FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the 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
29 ;; Please add these lines in your .emacs(_emacs).
30 ;;
31 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
32 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you are using Gnus.
33 ;;(setq smtp-default-server "YOUR SMTP HOST")
34 ;;(setq smtp-service "smtp")
35 ;;(setq smtp-local-domain "YOUR DOMAIN NAME")
36 ;;(setq smtp-debug-info t)
37 ;;(autoload 'smtpmail-send-it "smtpmail")
38 ;;(setq user-full-name "YOUR NAME HERE")
39
40 ;; To queue mail, set smtpmail-queue-mail to t and use 
41 ;; smtpmail-send-queued-mail to send.
42
43
44 ;;; Code:
45
46 (require 'custom)
47 (require 'smtp)
48 (require 'sendmail)
49 (require 'time-stamp)
50 (require 'mel) ; binary-write-decoded-region, binary-find-file-noselect
51
52 (eval-when-compile (require 'static))
53
54 ;; (static-when (featurep 'xemacs)
55 ;;   (define-obsolete-variable-alias 'smtpmail-default-smtp-server
56 ;;     'smtp-default-server)
57 ;;   (define-obsolete-variable-alias 'smtpmail-smtp-server 'smtp-server)
58 ;;   (define-obsolete-variable-alias 'smtpmail-smtp-service 'smtp-service)
59 ;;   (define-obsolete-variable-alias 'smtpmail-local-domain 'smtp-local-domain)
60 ;;   (define-obsolete-variable-alias 'smtpmail-debug-info 'smtp-debug-info)
61 ;;   )
62
63 ;;;
64
65 (defcustom smtpmail-queue-mail nil 
66   "Specify if mail is queued (if t) or sent immediately (if nil).
67 If queued, it is stored in the directory `smtpmail-queue-dir'
68 and sent with `smtpmail-send-queued-mail'."
69   :type 'boolean
70   :group 'smtp)
71
72 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
73   "Directory where `smtpmail.el' stores queued mail."
74   :type 'directory
75   :group 'smtp)
76
77 (defvar smtpmail-queue-index-file "index"
78   "File name of queued mail index,
79 This is relative to `smtpmail-queue-dir'.")
80
81 (defvar smtpmail-queue-index
82   (concat (file-name-as-directory smtpmail-queue-dir)
83           smtpmail-queue-index-file))
84
85 (defvar smtpmail-recipient-address-list nil)
86
87
88 ;;;
89 ;;;
90 ;;;
91
92 ;;;###autoload
93 (defun smtpmail-send-it ()
94   (require 'mail-utils)
95   (let ((errbuf (if mail-interactive
96                     (generate-new-buffer " smtpmail errors")
97                   0))
98         (tembuf (generate-new-buffer " smtpmail temp"))
99         (case-fold-search nil)
100         resend-to-addresses
101         delimline
102         (mailbuf (current-buffer)))
103     (unwind-protect
104         (save-excursion
105           (set-buffer tembuf)
106           (erase-buffer)
107           (insert-buffer-substring mailbuf)
108           (goto-char (point-max))
109           ;; require one newline at the end.
110           (or (= (preceding-char) ?\n)
111               (insert ?\n))
112           ;; Change header-delimiter to be what sendmail expects.
113           (goto-char (point-min))
114           (if (re-search-forward
115                (concat "^\\(" (regexp-quote mail-header-separator) "\\)?\n"))
116               (replace-match "\n"))
117           (backward-char 1)
118           (setq delimline (point-marker))
119 ;;        (sendmail-synch-aliases)
120           (if (and mail-aliases (fboundp 'expand-mail-aliases)) ; XEmacs
121               (expand-mail-aliases (point-min) delimline))
122           (goto-char (point-min))
123           ;; ignore any blank lines in the header
124           (while (and (re-search-forward "\n\n\n*" delimline t)
125                       (< (point) delimline))
126             (replace-match "\n"))
127           (let ((case-fold-search t))
128             (goto-char (point-min))
129             (goto-char (point-min))
130             (while (re-search-forward "^Resent-to:" delimline t)
131               (setq resend-to-addresses
132                     (save-restriction
133                       (narrow-to-region (point)
134                                         (save-excursion
135                                           (forward-line 1)
136                                           (while (looking-at "^[ \t]")
137                                             (forward-line 1))
138                                           (point)))
139                       (append (mail-parse-comma-list)
140                               resend-to-addresses))))
141 ;;; Apparently this causes a duplicate Sender.
142 ;;;         ;; If the From is different than current user, insert Sender.
143 ;;;         (goto-char (point-min))
144 ;;;         (and (re-search-forward "^From:"  delimline t)
145 ;;;              (progn
146 ;;;                (require 'mail-utils)
147 ;;;                (not (string-equal
148 ;;;                      (mail-strip-quoted-names
149 ;;;                       (save-restriction
150 ;;;                         (narrow-to-region (point-min) delimline)
151 ;;;                         (mail-fetch-field "From")))
152 ;;;                      (user-login-name))))
153 ;;;              (progn
154 ;;;                (forward-line 1)
155 ;;;                (insert "Sender: " (user-login-name) "\n")))
156             ;; Don't send out a blank subject line
157             (goto-char (point-min))
158             (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
159                 (replace-match ""))
160             ;; Put the "From:" field in unless for some odd reason
161             ;; they put one in themselves.
162             (goto-char (point-min))
163             (if (not (re-search-forward "^From:" delimline t))
164                 (let* ((login user-mail-address)
165                        (fullname (user-full-name)))
166                   (cond ((eq mail-from-style 'angles)
167                          (insert "From: " fullname)
168                          (let ((fullname-start (+ (point-min) 6))
169                                (fullname-end (point-marker)))
170                            (goto-char fullname-start)
171                            ;; Look for a character that cannot appear unquoted
172                            ;; according to RFC 822.
173                            (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
174                                                   fullname-end 1)
175                                (progn
176                                  ;; Quote fullname, escaping specials.
177                                  (goto-char fullname-start)
178                                  (insert "\"")
179                                  (while (re-search-forward "[\"\\]"
180                                                            fullname-end 1)
181                                    (replace-match "\\\\\\&" t))
182                                  (insert "\""))))
183                          (insert " <" login ">\n"))
184                         ((eq mail-from-style 'parens)
185                          (insert "From: " login " (")
186                          (let ((fullname-start (point)))
187                            (insert fullname)
188                            (let ((fullname-end (point-marker)))
189                              (goto-char fullname-start)
190                              ;; RFC 822 says \ and nonmatching parentheses
191                              ;; must be escaped in comments.
192                              ;; Escape every instance of ()\ ...
193                              (while (re-search-forward "[()\\]" fullname-end 1)
194                                (replace-match "\\\\\\&" t))
195                              ;; ... then undo escaping of matching parentheses,
196                              ;; including matching nested parentheses.
197                              (goto-char fullname-start)
198                              (while (re-search-forward 
199                                      "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
200                                      fullname-end 1)
201                                (replace-match "\\1(\\3)" t)
202                                (goto-char fullname-start))))
203                          (insert ")\n"))
204                         ((null mail-from-style)
205                          (insert "From: " login "\n")))))
206             ;; Insert an extra newline if we need it to work around
207             ;; Sun's bug that swallows newlines.
208             (goto-char (1+ delimline))
209             (if (eval mail-mailer-swallows-blank-line)
210                 (newline))
211             ;; Find and handle any FCC fields.
212             (goto-char (point-min))
213             (if (re-search-forward "^FCC:" delimline t)
214                 (mail-do-fcc delimline))
215             (if mail-interactive
216                 (save-excursion
217                   (set-buffer errbuf)
218                   (erase-buffer))))
219           ;;
220           ;;
221           ;;
222           (setq smtpmail-recipient-address-list
223                 (or resend-to-addresses
224                     (smtp-deduce-address-list tembuf (point-min) delimline)))
225
226           (smtpmail-do-bcc delimline)
227           ; Send or queue
228           (if (not smtpmail-queue-mail)
229               (if smtpmail-recipient-address-list
230                   (smtp-send-buffer user-mail-address
231                                     smtpmail-recipient-address-list
232                                     tembuf)
233                 (error "Sending failed; no recipients"))
234             (let* ((file-data (convert-standard-filename
235                                (concat
236                                 (file-name-as-directory smtpmail-queue-dir)
237                                 (time-stamp-yyyy-mm-dd)
238                                 "_" (time-stamp-hh:mm:ss))))
239                    (file-elisp (concat file-data ".el"))
240                    (buffer-data (create-file-buffer file-data))
241                    (buffer-elisp (create-file-buffer file-elisp))
242                    (buffer-scratch "*queue-mail*"))
243               (save-excursion
244                 (set-buffer buffer-data)
245                 (erase-buffer)
246                 (insert-buffer tembuf)
247                 (or (file-directory-p smtpmail-queue-dir)
248                     (make-directory smtpmail-queue-dir t))
249                 (binary-write-decoded-region (point-min) (point-max) file-data)
250                 (set-buffer buffer-elisp)
251                 (erase-buffer)
252                 (insert (concat
253                          "(setq smtpmail-recipient-address-list '"
254                          (prin1-to-string smtpmail-recipient-address-list)
255                          ")\n"))                    
256                 (write-file file-elisp)
257                 (set-buffer (generate-new-buffer buffer-scratch))
258                 (insert (concat file-data "\n"))
259                 (append-to-file (point-min) 
260                                 (point-max) 
261                                 smtpmail-queue-index)
262                 )
263               (kill-buffer buffer-scratch)
264               (kill-buffer buffer-data)
265               (kill-buffer buffer-elisp))))
266       (kill-buffer tembuf)
267       (if (bufferp errbuf)
268           (kill-buffer errbuf)))))
269
270 (defun smtpmail-send-queued-mail ()
271   "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
272   (interactive)
273   ;;; Get index, get first mail, send it, get second mail, etc...
274   (let ((buffer-index (find-file-noselect smtpmail-queue-index))
275         (file-msg "")
276         (tembuf nil))
277     (save-excursion
278       (set-buffer buffer-index)
279       (beginning-of-buffer)
280       (while (not (eobp))
281         (setq file-msg (buffer-substring (point) (save-excursion
282                                                    (end-of-line)
283                                                    (point))))
284         (load file-msg)
285         (setq tembuf (binary-find-file-noselect file-msg))
286         (if smtpmail-recipient-address-list
287             (smtp-send-buffer user-mail-address
288                               smtpmail-recipient-address-list tembuf)
289           (error "Sending failed; no recipients"))  
290         (delete-file file-msg)
291         (delete-file (concat file-msg ".el"))
292         (kill-buffer tembuf)
293         (kill-line 1))      
294       (set-buffer buffer-index)
295       (save-buffer smtpmail-queue-index)
296       (kill-buffer buffer-index)
297       )))
298
299
300 (defun smtpmail-do-bcc (header-end)
301   "Delete BCC: and their continuation lines from the header area.
302 There may be multiple BCC: lines, and each may have arbitrarily
303 many continuation lines."
304   (let ((case-fold-search t))
305     (save-excursion
306       (goto-char (point-min))
307       ;; iterate over all BCC: lines
308       (while (re-search-forward "^BCC:" header-end t)
309         (delete-region (match-beginning 0) (progn (forward-line 1) (point)))
310         ;; get rid of any continuation lines
311         (while (and (looking-at "^[ \t].*\n") (< (point) header-end))
312           (replace-match ""))
313         )
314       ) ;; save-excursion
315     ) ;; let
316   )
317
318
319 ;;;
320
321 (provide 'smtpmail)
322
323 ;;; smtpmail.el ends here