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