1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
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>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; Send Mail to smtp host from smtpmail temp buffer.
31 ;; Please add these lines in your .emacs(_emacs).
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")
41 ;; To queue mail, set smtpmail-queue-mail to t and use
42 ;; smtpmail-send-queued-mail to send.
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'."
60 (defcustom smtpmail-queue-dir "~/Mail/queued-mail/"
61 "*Directory where `smtpmail.el' stores queued mail."
65 (defvar smtpmail-queue-index-file "index"
66 "File name of queued mail index,
67 This is relative to `smtpmail-queue-dir'.")
69 (defvar smtpmail-queue-index (concat smtpmail-queue-dir
70 smtpmail-queue-index-file))
76 (defun smtpmail-send-it ()
78 (let ((errbuf (if mail-interactive
79 (generate-new-buffer " smtpmail errors")
81 (tembuf (generate-new-buffer " smtpmail temp"))
82 (case-fold-search nil)
85 (mailbuf (current-buffer)))
90 (insert-buffer-substring mailbuf)
91 (goto-char (point-max))
92 ;; require one newline at the end.
93 (or (= (preceding-char) ?\n)
95 ;; Change header-delimiter to be what sendmail expects.
96 (goto-char (point-min))
98 (concat "^" (regexp-quote mail-header-separator) "\n"))
101 (setq delimline (point-marker))
102 ;; (sendmail-synch-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
116 (narrow-to-region (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)
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))))
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)
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^-~]"
157 ;; Quote fullname, escaping specials.
158 (goto-char fullname-start)
160 (while (re-search-forward "[\"\\]"
162 (replace-match "\\\\\\&" t))
164 (insert " <" login ">\n"))
165 ((eq mail-from-style 'parens)
166 (insert "From: " login " (")
167 (let ((fullname-start (point)))
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 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
182 (replace-match "\\1(\\3)" t)
183 (goto-char fullname-start))))
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)
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))
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)
209 (smtp-do-bcc delimline)
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
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*"))
226 (set-buffer buffer-data)
228 (insert-buffer tembuf)
229 (write-file file-data)
230 (set-buffer buffer-elisp)
233 "(setq smtp-recipient-address-list '"
234 (prin1-to-string smtp-recipient-address-list)
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)
241 smtpmail-queue-index)
243 (kill-buffer buffer-scratch)
244 (kill-buffer buffer-data)
245 (kill-buffer buffer-elisp))))
248 (kill-buffer errbuf)))))
250 (defun smtpmail-send-queued-mail ()
251 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
253 ;;; Get index, get first mail, send it, get second mail, etc...
254 (let ((buffer-index (find-file-noselect smtpmail-queue-index))
258 (set-buffer buffer-index)
259 (beginning-of-buffer)
261 (setq file-msg (buffer-substring (point) (save-excursion
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
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"))
275 (set-buffer buffer-index)
276 (save-buffer smtpmail-queue-index)
277 (kill-buffer buffer-index)
285 ;;; smtpmail.el ends here