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