Split basic features into smtp.el.
[elisp/gnus.git-] / lisp / smtpmail.el
1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
2
3 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
7 ;; ESMTP support: Simon Leinen <simon@switch.ch>
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.
30
31 ;; Please add these lines in your .emacs(_emacs).
32 ;;
33 ;;(setq send-mail-function 'smtpmail-send-it)
34 ;;(setq smtp-default-server "YOUR SMTP HOST")
35 ;;(setq smtp-service "smtp")
36 ;;(setq smtp-local-domain "YOUR DOMAIN NAME")
37 ;;(setq smtp-debug-info t)
38 ;;(autoload 'smtpmail-send-it "smtpmail")
39 ;;(setq user-full-name "YOUR NAME HERE")
40
41 ;; To queue mail, set smtpmail-queue-mail to t and use 
42 ;; smtpmail-send-queued-mail to send.
43
44
45 ;;; Code:
46
47 (require 'smtp)
48 (require 'sendmail)
49 (require 'time-stamp)
50
51 ;;;
52
53 (defcustom smtpmail-queue-mail nil 
54   "*Specify if mail is queued (if t) or sent immediately (if nil).
55 If queued, it is stored in the directory `smtpmail-queue-dir'
56 and sent with `smtpmail-send-queued-mail'."
57   :type 'boolean
58   :group 'smtp)
59
60 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
61   "*Directory where `smtpmail.el' stores queued mail."
62   :type 'directory
63   :group 'smtp)
64
65 (defvar smtpmail-queue-index-file "index"
66   "File name of queued mail index,
67 This is relative to `smtpmail-queue-dir'.")
68
69 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
70                                      smtpmail-queue-index-file))
71
72 ;;;
73 ;;;
74 ;;;
75
76 (defun smtpmail-send-it ()
77   (require 'mail-utils)
78   (let ((errbuf (if mail-interactive
79                     (generate-new-buffer " smtpmail errors")
80                   0))
81         (tembuf (generate-new-buffer " smtpmail temp"))
82         (case-fold-search nil)
83         resend-to-addresses
84         delimline
85         (mailbuf (current-buffer)))
86     (unwind-protect
87         (save-excursion
88           (set-buffer tembuf)
89           (erase-buffer)
90           (insert-buffer-substring mailbuf)
91           (goto-char (point-max))
92           ;; require one newline at the end.
93           (or (= (preceding-char) ?\n)
94               (insert ?\n))
95           ;; Change header-delimiter to be what sendmail expects.
96           (goto-char (point-min))
97           (re-search-forward
98             (concat "^" (regexp-quote mail-header-separator) "\n"))
99           (replace-match "\n")
100           (backward-char 1)
101           (setq delimline (point-marker))
102 ;;        (sendmail-synch-aliases)
103           (if mail-aliases
104               (expand-mail-aliases (point-min) delimline))
105           (goto-char (point-min))
106           ;; ignore any blank lines in the header
107           (while (and (re-search-forward "\n\n\n*" delimline t)
108                       (< (point) delimline))
109             (replace-match "\n"))
110           (let ((case-fold-search t))
111             (goto-char (point-min))
112             (goto-char (point-min))
113             (while (re-search-forward "^Resent-to:" delimline t)
114               (setq resend-to-addresses
115                     (save-restriction
116                       (narrow-to-region (point)
117                                         (save-excursion
118                                           (end-of-line)
119                                           (point)))
120                       (append (mail-parse-comma-list)
121                               resend-to-addresses))))
122 ;;; Apparently this causes a duplicate Sender.
123 ;;;         ;; If the From is different than current user, insert Sender.
124 ;;;         (goto-char (point-min))
125 ;;;         (and (re-search-forward "^From:"  delimline t)
126 ;;;              (progn
127 ;;;                (require 'mail-utils)
128 ;;;                (not (string-equal
129 ;;;                      (mail-strip-quoted-names
130 ;;;                       (save-restriction
131 ;;;                         (narrow-to-region (point-min) delimline)
132 ;;;                         (mail-fetch-field "From")))
133 ;;;                      (user-login-name))))
134 ;;;              (progn
135 ;;;                (forward-line 1)
136 ;;;                (insert "Sender: " (user-login-name) "\n")))
137             ;; Don't send out a blank subject line
138             (goto-char (point-min))
139             (if (re-search-forward "^Subject:[ \t]*\n" delimline t)
140                 (replace-match ""))
141             ;; Put the "From:" field in unless for some odd reason
142             ;; they put one in themselves.
143             (goto-char (point-min))
144             (if (not (re-search-forward "^From:" delimline t))
145                 (let* ((login user-mail-address)
146                        (fullname (user-full-name)))
147                   (cond ((eq mail-from-style 'angles)
148                          (insert "From: " fullname)
149                          (let ((fullname-start (+ (point-min) 6))
150                                (fullname-end (point-marker)))
151                            (goto-char fullname-start)
152                            ;; Look for a character that cannot appear unquoted
153                            ;; according to RFC 822.
154                            (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
155                                                   fullname-end 1)
156                                (progn
157                                  ;; Quote fullname, escaping specials.
158                                  (goto-char fullname-start)
159                                  (insert "\"")
160                                  (while (re-search-forward "[\"\\]"
161                                                            fullname-end 1)
162                                    (replace-match "\\\\\\&" t))
163                                  (insert "\""))))
164                          (insert " <" login ">\n"))
165                         ((eq mail-from-style 'parens)
166                          (insert "From: " login " (")
167                          (let ((fullname-start (point)))
168                            (insert fullname)
169                            (let ((fullname-end (point-marker)))
170                              (goto-char fullname-start)
171                              ;; RFC 822 says \ and nonmatching parentheses
172                              ;; must be escaped in comments.
173                              ;; Escape every instance of ()\ ...
174                              (while (re-search-forward "[()\\]" fullname-end 1)
175                                (replace-match "\\\\\\&" t))
176                              ;; ... then undo escaping of matching parentheses,
177                              ;; including matching nested parentheses.
178                              (goto-char fullname-start)
179                              (while (re-search-forward 
180                                      "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
181                                      fullname-end 1)
182                                (replace-match "\\1(\\3)" t)
183                                (goto-char fullname-start))))
184                          (insert ")\n"))
185                         ((null mail-from-style)
186                          (insert "From: " login "\n")))))
187             ;; Insert an extra newline if we need it to work around
188             ;; Sun's bug that swallows newlines.
189             (goto-char (1+ delimline))
190             (if (eval mail-mailer-swallows-blank-line)
191                 (newline))
192             ;; Find and handle any FCC fields.
193             (goto-char (point-min))
194             (if (re-search-forward "^FCC:" delimline t)
195                 (mail-do-fcc delimline))
196             (if mail-interactive
197                 (save-excursion
198                   (set-buffer errbuf)
199                   (erase-buffer))))
200           ;;
201           ;;
202           ;;
203           (setq smtp-address-buffer (generate-new-buffer "*smtp-mail*"))
204           (setq smtp-recipient-address-list
205                 (or resend-to-addresses
206                     (smtp-deduce-address-list tembuf (point-min) delimline)))
207           (kill-buffer smtp-address-buffer)
208           
209           (smtp-do-bcc delimline)
210           ; Send or queue
211           (if (not smtpmail-queue-mail)
212               (if (not (null smtp-recipient-address-list))
213                   (if (not (smtp-via-smtp 
214                             smtp-recipient-address-list tembuf))
215                       (error "Sending failed; SMTP protocol error"))
216                 (error "Sending failed; no recipients"))
217             (let* ((file-data (concat 
218                                smtpmail-queue-dir
219                                (time-stamp-strftime 
220                                 "%02y%02m%02d-%02H%02M%02S")))
221                    (file-elisp (concat file-data ".el"))
222                    (buffer-data (create-file-buffer file-data))
223                    (buffer-elisp (create-file-buffer file-elisp))
224                    (buffer-scratch "*queue-mail*"))
225               (save-excursion
226                 (set-buffer buffer-data)
227                 (erase-buffer)
228                 (insert-buffer tembuf)
229                 (write-file file-data)
230                 (set-buffer buffer-elisp)
231                 (erase-buffer)
232                 (insert (concat
233                          "(setq smtp-recipient-address-list '"
234                          (prin1-to-string smtp-recipient-address-list)
235                          ")\n"))                    
236                 (write-file file-elisp)
237                 (set-buffer (generate-new-buffer buffer-scratch))
238                 (insert (concat file-data "\n"))
239                 (append-to-file (point-min) 
240                                 (point-max) 
241                                 smtpmail-queue-index)
242                 )
243               (kill-buffer buffer-scratch)
244               (kill-buffer buffer-data)
245               (kill-buffer buffer-elisp))))
246       (kill-buffer tembuf)
247       (if (bufferp errbuf)
248           (kill-buffer errbuf)))))
249
250 (defun smtpmail-send-queued-mail ()
251   "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
252   (interactive)
253   ;;; Get index, get first mail, send it, get second mail, etc...
254   (let ((buffer-index (find-file-noselect smtpmail-queue-index))
255         (file-msg "")
256         (tembuf nil))
257     (save-excursion
258       (set-buffer buffer-index)
259       (beginning-of-buffer)
260       (while (not (eobp))
261         (setq file-msg (buffer-substring (point) (save-excursion
262                                                    (end-of-line)
263                                                    (point))))
264         (load file-msg)
265         (setq tembuf (find-file-noselect file-msg))
266         (if (not (null smtp-recipient-address-list))
267             (if (not (smtp-via-smtp smtp-recipient-address-list 
268                                         tembuf))
269                 (error "Sending failed; SMTP protocol error"))
270           (error "Sending failed; no recipients"))  
271         (delete-file file-msg)
272         (delete-file (concat file-msg ".el"))
273         (kill-buffer tembuf)
274         (kill-line 1))      
275       (set-buffer buffer-index)
276       (save-buffer smtpmail-queue-index)
277       (kill-buffer buffer-index)
278       )))
279
280
281 ;;;
282
283 (provide 'smtpmail)
284
285 ;;; smtpmail.el ends here