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