1 ;;; sendmail.el --- mail sending commands for Emacs.
4 ;; Last-Modified: 24 Jun 1992
7 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 (defconst mail-self-blind nil
"\
29 Non-nil means insert BCC to self in messages to be sent.
30 This is done when the message is initialized,
31 so you can remove or alter the BCC field to override the default.")
34 (defconst mail-interactive nil
"\
35 Non-nil means when sending a message wait for and display errors.
36 nil means let mailer mail back a message to report errors.")
39 (defconst mail-yank-ignored-headers
"^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
40 Delete these headers from old message when it's inserted in a reply.")
42 ;; Useful to set in site-init.el
44 (defconst send-mail-function
'sendmail-send-it
"\
45 Function to call to send the current buffer as mail.
46 The headers are be delimited by a line which is mail-header-separator.")
49 (defvar mail-header-separator
"--text follows this line--" "\
50 *Line used to separate headers from text in messages being composed.")
53 (defvar mail-archive-file-name nil
"\
54 *Name of file to write all outgoing messages in, or nil for none.
55 Do not use an rmail file here! Instead, use its inbox file.")
57 (defvar mail-default-reply-to nil
58 "*Address to insert as default Reply-to field of outgoing messages.")
60 (defvar mail-alias-file nil
61 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
62 This file defines aliases to be expanded by the mailer; this is a different
63 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
64 This variable has no effect unless your system uses sendmail as its mailer.")
66 (defvar mail-yank-prefix nil
67 "*Prefix insert on lines of yanked message being replied to.
68 nil means use indentation.")
70 (defvar mail-abbrevs-loaded nil
)
71 (defvar mail-mode-map nil
)
73 (defvar mail-signature nil
74 "*Text inserted at end of mail buffer when a message is initialized.
75 If t, it means to insert the contents of the file `~/.signature'.")
77 (defvar mail-reply-buffer nil
)
78 (defvar mail-send-actions nil
79 "A list of actions to be performed upon successful sending of a message.")
81 (defvar mail-default-headers nil
82 "*A string containing header lines, to be inserted in outgoing messages.
83 It is inserted before you edit the message,
84 so you can edit or delete these lines.")
86 (defvar mail-mode-syntax-table nil
87 "Syntax table used while in mail mode.")
89 (if (null mail-mode-syntax-table
)
91 (setq mail-mode-syntax-table
(copy-syntax-table text-mode-syntax-table
))
92 (modify-syntax-entry ?%
". " mail-mode-syntax-table
)))
94 (defun mail-setup (to subject in-reply-to cc replybuffer actions
)
95 (setq mail-send-actions actions
)
97 (setq mail-reply-buffer replybuffer
)
98 (goto-char (point-min))
104 ;;; Here removed code to extract names from within <...>
105 ;;; on the assumption that mail-strip-quoted-names
106 ;;; has been called and has done so.
107 (let ((fill-prefix "\t"))
108 (fill-region (point-min) (point-max))))
113 (insert "CC: " cc
"\n")
114 (fill-region-as-paragraph opos
(point-max))))
116 (insert "In-reply-to: " in-reply-to
"\n"))
117 (insert "Subject: " (or subject
"") "\n")
118 (if mail-default-headers
119 (insert mail-default-headers
))
120 (if mail-default-reply-to
121 (insert "Reply-to: " mail-default-reply-to
"\n"))
123 (insert "BCC: " (user-login-name) "\n"))
124 (if mail-archive-file-name
125 (insert "FCC: " mail-archive-file-name
"\n"))
126 (insert mail-header-separator
"\n")
127 ;; Read the .signature file if we haven't already done so
128 ;; (and if the user has not overridden it).
129 (cond ((eq mail-signature t
)
131 (insert-file-contents "~/.signature"))
133 (insert mail-signature
)))
134 (goto-char (point-max))
135 (or (bolp) (newline)))
136 (if to
(goto-char (point-max)))
137 (or to subject in-reply-to
138 (set-buffer-modified-p nil
))
139 (run-hooks 'mail-setup-hook
))
143 "Major mode for editing mail to be sent.
144 Like Text Mode but with these additional commands:
145 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
146 C-c C-f move to a header field (and create it if there isn't):
147 C-c C-f C-t move to To: C-c C-f C-s move to Subj:
148 C-c C-f C-b move to BCC: C-c C-f C-c move to CC:
149 C-c C-t move to message text.
150 C-c C-y mail-yank-original (insert current message, in Rmail).
151 C-c C-q mail-fill-yanked-message (fill what was yanked).
152 C-c C-v mail-sent-via (add a sent-via field for each To or CC)."
154 (kill-all-local-variables)
155 (make-local-variable 'mail-reply-buffer
)
156 (setq mail-reply-buffer nil
)
157 (make-local-variable 'mail-send-actions
)
158 (set-syntax-table mail-mode-syntax-table
)
159 (use-local-map mail-mode-map
)
160 (setq local-abbrev-table text-mode-abbrev-table
)
161 (setq major-mode
'mail-mode
)
162 (setq mode-name
"Mail")
163 (setq buffer-offer-save t
)
164 (make-local-variable 'paragraph-separate
)
165 (make-local-variable 'paragraph-start
)
166 (setq paragraph-start
(concat "^" mail-header-separator
167 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
169 (setq paragraph-separate
(concat "^" mail-header-separator
170 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
172 (run-hooks 'text-mode-hook
'mail-mode-hook
))
176 (setq mail-mode-map
(nconc (make-sparse-keymap) text-mode-map
))
177 (define-key mail-mode-map
"\C-c?" 'describe-mode
)
178 (define-key mail-mode-map
"\C-c\C-f\C-t" 'mail-to
)
179 (define-key mail-mode-map
"\C-c\C-f\C-b" 'mail-bcc
)
180 (define-key mail-mode-map
"\C-c\C-f\C-f" 'mail-fcc
)
181 (define-key mail-mode-map
"\C-c\C-f\C-c" 'mail-cc
)
182 (define-key mail-mode-map
"\C-c\C-f\C-s" 'mail-subject
)
183 (define-key mail-mode-map
"\C-c\C-t" 'mail-text
)
184 (define-key mail-mode-map
"\C-c\C-y" 'mail-yank-original
)
185 (define-key mail-mode-map
"\C-c\C-q" 'mail-fill-yanked-message
)
186 (define-key mail-mode-map
"\C-c\C-w" 'mail-signature
)
187 (define-key mail-mode-map
"\C-c\C-v" 'mail-sent-via
)
188 (define-key mail-mode-map
"\C-c\C-c" 'mail-send-and-exit
)
189 (define-key mail-mode-map
"\C-c\C-s" 'mail-send
))
191 (defun mail-send-and-exit (arg)
192 "Send message like mail-send, then, if no errors, exit from mail buffer.
193 Prefix arg means don't delete this window."
196 (bury-buffer (current-buffer))
197 (if (or (one-window-p) arg
)
198 (switch-to-buffer (other-buffer (current-buffer)))
202 "Send the message in the current buffer.
203 If `mail-interactive' is non-nil, wait for success indication
204 or error messages, and inform user.
205 Otherwise any failure is reported in a message back to
206 the user from the mailer."
208 (if (or (buffer-modified-p)
209 (y-or-n-p "Message already sent; resend? "))
211 (message "Sending...")
212 (run-hooks 'mail-send-hook
)
213 (funcall send-mail-function
)
214 ;; Now perform actions on successful sending.
215 (while mail-send-actions
217 (apply (car (car mail-send-actions
)) (cdr (car mail-send-actions
)))
219 (setq mail-send-actions
(cdr mail-send-actions
)))
220 (set-buffer-modified-p nil
)
221 (delete-auto-save-file-if-necessary t
)
222 (message "Sending...done"))))
224 (defun sendmail-send-it ()
225 (let ((errbuf (if mail-interactive
226 (generate-new-buffer " sendmail errors")
228 (tembuf (generate-new-buffer " sendmail temp"))
229 (case-fold-search nil
)
231 (mailbuf (current-buffer)))
236 (insert-buffer-substring mailbuf
)
237 (goto-char (point-max))
238 ;; require one newline at the end.
239 (or (= (preceding-char) ?
\n)
241 ;; Change header-delimiter to be what sendmail expects.
242 (goto-char (point-min))
244 (concat "^" (regexp-quote mail-header-separator
) "\n"))
247 (setq delimline
(point-marker))
248 (goto-char (point-min))
249 ;; ignore any blank lines in the header
250 (while (and (re-search-forward "\n\n\n*" delimline t
)
251 (< (point) delimline
))
252 (replace-match "\n"))
253 (let ((case-fold-search t
))
254 (goto-char (point-min))
255 (if (re-search-forward "^Sender:" delimline t
)
256 (error "Sender may not be specified."))
257 ;; Find and handle any FCC fields.
258 (goto-char (point-min))
259 (if (re-search-forward "^FCC:" delimline t
)
260 (mail-do-fcc delimline
))
261 ;; If the From is different than current user, insert Sender.
262 (goto-char (point-min))
263 (and (re-search-forward "^From:" delimline t
)
265 (require 'mail-utils
)
267 (mail-strip-quoted-names
269 (narrow-to-region (point-min) delimline
)
270 (mail-fetch-field "From")))
274 (insert "Sender: " (user-login-name) "\n")))
275 ;; "S:" is an abbreviation for "Subject:".
276 (goto-char (point-min))
277 (if (re-search-forward "^S:" delimline t
)
278 (replace-match "Subject:"))
279 ;; Don't send out a blank subject line
280 (goto-char (point-min))
281 (if (re-search-forward "^Subject:[ \t]*\n" delimline t
)
287 (apply 'call-process-region
288 (append (list (point-min) (point-max)
289 (if (boundp 'sendmail-program
)
294 ;; Always specify who from,
295 ;; since some systems have broken sendmails.
296 (list "-f" (user-login-name))
297 ;;; ;; Don't say "from root" if running under su.
298 ;;; (and (equal (user-real-login-name) "root")
299 ;;; (list "-f" (user-login-name)))
301 (list (concat "-oA" mail-alias-file
)))
302 ;; These mean "report errors by mail"
303 ;; and "deliver in background".
304 (if (null mail-interactive
) '("-oem" "-odb"))))
308 (goto-char (point-min))
309 (while (re-search-forward "\n\n* *" nil t
)
310 (replace-match "; "))
311 (if (not (zerop (buffer-size)))
312 (error "Sending...failed to %s"
313 (buffer-substring (point-min) (point-max)))))))
316 (kill-buffer errbuf
)))))
318 (defun mail-do-fcc (header-end)
320 (rmailbuf (current-buffer))
321 (tembuf (generate-new-buffer " rmail output"))
322 (case-fold-search t
))
324 (goto-char (point-min))
325 (while (re-search-forward "^FCC:[ \t]*" header-end t
)
326 (setq fcc-list
(cons (buffer-substring (point)
329 (skip-chars-backward " \t")
332 (delete-region (match-beginning 0)
333 (progn (forward-line 1) (point))))
336 (insert "\nFrom " (user-login-name) " "
337 (current-time-string) "\n")
338 (insert-buffer-substring rmailbuf
)
339 ;; Make sure messages are separated.
340 (goto-char (point-max))
343 ;; ``Quote'' "^From " as ">From "
344 ;; (note that this isn't really quoting, as there is no requirement
345 ;; that "^[>]+From " be quoted in the same transparent way.)
346 (let ((case-fold-search nil
))
347 (while (search-forward "\nFrom " nil t
)
351 (let ((buffer (get-file-buffer (car fcc-list
))))
353 ;; File is present in a buffer => append to that buffer.
354 (let ((curbuf (current-buffer))
355 (beg (point-min)) (end (point-max)))
358 ;; Keep the end of the accessible portion at the same place
359 ;; unless it is the end of the buffer.
360 (let ((max (if (/= (1+ (buffer-size)) (point-max))
364 (narrow-to-region (point-min) (1+ (buffer-size)))
365 (goto-char (point-max))
366 (if (eq major-mode
'rmail-mode
)
367 ;; Append as a message to an RMAIL file
368 (let ((buffer-read-only nil
))
369 ;; This forces RMAIL's message counters to be
370 ;; recomputed when the next RMAIL operation is
371 ;; done on the buffer.
372 ;; See rmail-maybe-set-message-counters.
373 (setq rmail-total-messages nil
)
374 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
375 "From: " (user-login-name) "\n"
376 "Date: " (current-time-string) "\n")
377 (insert-buffer-substring curbuf beg end
)
379 (rmail-set-message-counters))
380 (insert-buffer-substring curbuf beg end
)))
381 (if max
(narrow-to-region (point-min) max
))))))
382 ;; Else append to the file directly.
384 ;; Include a blank line before if file already exists.
385 (if (file-exists-p (car fcc-list
)) (point-min) (1+ (point-min)))
386 (point-max) (car fcc-list
) t
)))
387 (setq fcc-list
(cdr fcc-list
))))
388 (kill-buffer tembuf
)))
390 (defun mail-sent-via ()
391 "Make a Sent-via header line from each To or CC header line."
394 (goto-char (point-min))
395 ;; find the header-separator
396 (search-forward (concat "\n" mail-header-separator
"\n"))
398 ;; put a marker at the end of the header
399 (let ((end (point-marker))
402 (goto-char (point-min))
403 ;; search for the To: lines and make Sent-via: lines from them
404 ;; search for the next To: line
405 (while (re-search-forward "^\\(to\\|cc\\):" end t
)
406 ;; Grab this line plus all its continuations, sans the `to:'.
408 (buffer-substring (point)
410 (if (re-search-forward "^[^ \t\n]" end t
)
414 ;; Insert a copy, with altered header field name.
415 (insert-before-markers "Sent-via:" to-line
))))))
418 "Move point to end of To-field."
421 (mail-position-on-field "To"))
423 (defun mail-subject ()
424 "Move point to end of Subject-field."
427 (mail-position-on-field "Subject"))
430 "Move point to end of CC-field. Create a CC field if none."
433 (or (mail-position-on-field "cc" t
)
434 (progn (mail-position-on-field "to")
438 "Move point to end of BCC-field. Create a BCC field if none."
441 (or (mail-position-on-field "bcc" t
)
442 (progn (mail-position-on-field "to")
443 (insert "\nBCC: "))))
446 "Add a new FCC field, with file name completion."
449 (or (mail-position-on-field "fcc" t
) ;Put new field after exiting FCC.
450 (mail-position-on-field "to"))
451 (insert "\nFCC: " (read-file-name "Folder carbon copy: ")))
453 (defun mail-position-on-field (field &optional soft
)
455 (case-fold-search t
))
456 (goto-char (point-min))
457 (re-search-forward (concat "^" (regexp-quote mail-header-separator
) "\n"))
458 (setq end
(match-beginning 0))
459 (goto-char (point-min))
460 (if (re-search-forward (concat "^" (regexp-quote field
) ":") end t
)
462 (re-search-forward "^[^ \t]" nil
'move
)
464 (skip-chars-backward "\n")
467 (progn (goto-char end
)
468 (skip-chars-backward "\n")
469 (insert field
": \n")
470 (skip-chars-backward "\n")))
474 "Move point to beginning of text field."
476 (goto-char (point-min))
477 (search-forward (concat "\n" mail-header-separator
"\n")))
479 (defun mail-signature (atpoint)
480 "Sign letter with contents of ~/.signature file."
484 (goto-char (point-max)))
485 (skip-chars-backward " \t\n")
488 (delete-region (point) (point-max)))
490 (insert-file-contents (expand-file-name "~/.signature"))))
492 (defun mail-fill-yanked-message (&optional justifyp
)
493 "Fill the paragraphs of a message yanked into this one.
494 Numeric argument means justify as well."
497 (goto-char (point-min))
498 (search-forward (concat "\n" mail-header-separator
"\n") nil t
)
499 (fill-individual-paragraphs (point)
504 (defun mail-yank-original (arg)
505 "Insert the message being replied to, if any (in rmail).
506 Puts point before the text and mark after.
507 Normally, indents each nonblank line ARG spaces (default 3).
508 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
510 Just \\[universal-argument] as argument means don't indent, insert no prefix,
511 and don't delete any header fields."
513 (if mail-reply-buffer
514 (let ((start (point)))
515 (delete-windows-on mail-reply-buffer
)
516 (insert-buffer mail-reply-buffer
)
519 (mail-yank-clear-headers start
(mark))
520 (if (null mail-yank-prefix
)
521 (indent-rigidly start
(mark)
522 (if arg
(prefix-numeric-value arg
) 3))
525 (while (< (point) (mark))
526 (insert mail-yank-prefix
)
528 (exchange-point-and-mark)
529 (if (not (eolp)) (insert ?
\n)))))
531 (defun mail-yank-clear-headers (start end
)
534 (if (search-forward "\n\n" end t
)
536 (narrow-to-region start
(point))
538 (while (let ((case-fold-search t
))
539 (re-search-forward mail-yank-ignored-headers nil t
))
541 (delete-region (point)
542 (progn (re-search-forward "\n[^ \t]")
546 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
549 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions
)
550 "Edit a message to be sent. Argument means resume editing (don't erase).
551 Search for an existing mail buffer currently not in use and initialize it,
552 or make a new one if all existing mail buffers are busy.
553 With an argument, search for a busy existing mail buffer and re-select it.
555 Returns with message buffer selected; value t if message freshly initialized.
556 By default, the signature file `~/.signature' is inserted at the end;
557 see the variable `mail-signature'.
560 While editing message, type \\[mail-send-and-exit] to send the message and exit.
562 Various special commands starting with C-c are available in sendmail mode
563 to move to message header fields:
566 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
567 when the message is initialized.
569 If `mail-default-reply-to' is non-nil, it should be an address (a string);
570 a Reply-to: field with that address is inserted.
572 If `mail-archive-file-name' is non-nil, an FCC field with that file name
575 If `mail-setup-hook' is bound, its value is called with no arguments
576 after the message is initialized. It can add more default fields.
578 When calling from a program, the second through fifth arguments
579 TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
580 the initial contents of those header fields.
581 These arguments should not have final newlines.
582 The sixth argument REPLYBUFFER is a buffer whose contents
583 should be yanked if the user types C-c C-y.
584 The seventh argument ACTIONS is a list of actions to take
585 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
586 when the message is sent, we apply FUNCTION to ARGS.
587 This is how Rmail arranges to mark messages `answered'."
591 ;; If requested, look for a mail buffer that is modified and go to it.
594 (while (and (setq buffer
595 (get-buffer (if (= 1 index
) "*mail*"
596 (format "*mail*<%d>" index
))))
597 (not (buffer-modified-p buffer
)))
598 (setq index
(1+ index
)))
599 (if buffer
(switch-to-buffer buffer
)
600 ;; If none exists, start a new message.
601 ;; This will never re-use an existing unmodified mail buffer
602 ;; (since index is not 1 anymore). Perhaps it should.
603 (setq noerase nil
))))
604 ;; Unless we found a modified message and are happy, start a new message.
607 ;; Look for existing unmodified mail buffer.
608 (while (and (setq buffer
609 (get-buffer (if (= 1 index
) "*mail*"
610 (format "*mail*<%d>" index
))))
611 (buffer-modified-p buffer
))
612 (setq index
(1+ index
)))
613 ;; If none, make a new one.
615 (setq buffer
(generate-new-buffer "*mail*")))
616 ;; Go there and initialize it.
617 (switch-to-buffer buffer
)
619 (setq default-directory
(expand-file-name "~/"))
620 (auto-save-mode auto-save-default
)
622 (mail-setup to subject in-reply-to cc replybuffer actions
)
623 (if (and buffer-auto-save-file-name
624 (file-exists-p buffer-auto-save-file-name
))
625 (message "Auto save file for draft message exists; consider M-x mail-recover"))
629 (define-key ctl-x-map
"m" 'mail
)
631 (defun mail-recover ()
632 "Reread contents of current buffer from its last auto-save file."
634 (let ((file-name (make-auto-save-file-name)))
635 (cond ((save-window-excursion
636 (if (not (eq system-type
'vax-vms
))
637 (with-output-to-temp-buffer "*Directory*"
638 (buffer-disable-undo standard-output
)
639 (call-process "ls" nil standard-output nil
"-l" file-name
)))
640 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
641 (let ((buffer-read-only nil
))
643 (insert-file-contents file-name nil
)))
644 (t (error "mail-recover cancelled.")))))
647 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
648 "Like `mail' command, but display mail buffer in another window."
650 (let ((pop-up-windows t
))
651 (pop-to-buffer "*mail*"))
652 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
655 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
656 "Like `mail' command, but display mail buffer in another frame."
658 (let ((pop-up-frames t
))
659 (pop-to-buffer "*mail*"))
660 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
664 (define-key ctl-x-4-map
"m" 'mail-other-window
)
667 (define-key ctl-x-5-map
"m" 'mail-other-frame
)
670 ;;; Do not add anything but external entries on this page.
674 ;;; sendmail.el ends here