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))
200 (set-buffer (window-buffer (next-window (selected-window) 'not
)))
201 (eq major-mode
'rmail-mode
)))
203 (switch-to-buffer (other-buffer (current-buffer)))))
206 "Send the message in the current buffer.
207 If `mail-interactive' is non-nil, wait for success indication
208 or error messages, and inform user.
209 Otherwise any failure is reported in a message back to
210 the user from the mailer."
212 (if (or (buffer-modified-p)
213 (y-or-n-p "Message already sent; resend? "))
215 (message "Sending...")
216 (run-hooks 'mail-send-hook
)
217 (funcall send-mail-function
)
218 ;; Now perform actions on successful sending.
219 (while mail-send-actions
221 (apply (car (car mail-send-actions
)) (cdr (car mail-send-actions
)))
223 (setq mail-send-actions
(cdr mail-send-actions
)))
224 (set-buffer-modified-p nil
)
225 (delete-auto-save-file-if-necessary t
)
226 (message "Sending...done"))))
228 (defun sendmail-send-it ()
229 (let ((errbuf (if mail-interactive
230 (generate-new-buffer " sendmail errors")
232 (tembuf (generate-new-buffer " sendmail temp"))
233 (case-fold-search nil
)
235 (mailbuf (current-buffer)))
240 (insert-buffer-substring mailbuf
)
241 (goto-char (point-max))
242 ;; require one newline at the end.
243 (or (= (preceding-char) ?
\n)
245 ;; Change header-delimiter to be what sendmail expects.
246 (goto-char (point-min))
248 (concat "^" (regexp-quote mail-header-separator
) "\n"))
251 (setq delimline
(point-marker))
252 (goto-char (point-min))
253 ;; ignore any blank lines in the header
254 (while (and (re-search-forward "\n\n\n*" delimline t
)
255 (< (point) delimline
))
256 (replace-match "\n"))
257 (let ((case-fold-search t
))
258 (goto-char (point-min))
259 (if (re-search-forward "^Sender:" delimline t
)
260 (error "Sender may not be specified."))
261 ;; Find and handle any FCC fields.
262 (goto-char (point-min))
263 (if (re-search-forward "^FCC:" delimline t
)
264 (mail-do-fcc delimline
))
265 ;; If the From is different than current user, insert Sender.
266 (goto-char (point-min))
267 (and (re-search-forward "^From:" delimline t
)
269 (require 'mail-utils
)
271 (mail-strip-quoted-names
273 (narrow-to-region (point-min) delimline
)
274 (mail-fetch-field "From")))
278 (insert "Sender: " (user-login-name) "\n")))
279 ;; "S:" is an abbreviation for "Subject:".
280 (goto-char (point-min))
281 (if (re-search-forward "^S:" delimline t
)
282 (replace-match "Subject:"))
283 ;; Don't send out a blank subject line
284 (goto-char (point-min))
285 (if (re-search-forward "^Subject:[ \t]*\n" delimline t
)
291 (apply 'call-process-region
292 (append (list (point-min) (point-max)
293 (if (boundp 'sendmail-program
)
298 ;; Always specify who from,
299 ;; since some systems have broken sendmails.
300 (list "-f" (user-login-name))
301 ;;; ;; Don't say "from root" if running under su.
302 ;;; (and (equal (user-real-login-name) "root")
303 ;;; (list "-f" (user-login-name)))
305 (list (concat "-oA" mail-alias-file
)))
306 ;; These mean "report errors by mail"
307 ;; and "deliver in background".
308 (if (null mail-interactive
) '("-oem" "-odb"))))
312 (goto-char (point-min))
313 (while (re-search-forward "\n\n* *" nil t
)
314 (replace-match "; "))
315 (if (not (zerop (buffer-size)))
316 (error "Sending...failed to %s"
317 (buffer-substring (point-min) (point-max)))))))
320 (kill-buffer errbuf
)))))
322 (defun mail-do-fcc (header-end)
324 (rmailbuf (current-buffer))
325 (tembuf (generate-new-buffer " rmail output"))
326 (case-fold-search t
))
328 (goto-char (point-min))
329 (while (re-search-forward "^FCC:[ \t]*" header-end t
)
330 (setq fcc-list
(cons (buffer-substring (point)
333 (skip-chars-backward " \t")
336 (delete-region (match-beginning 0)
337 (progn (forward-line 1) (point))))
340 (insert "\nFrom " (user-login-name) " "
341 (current-time-string) "\n")
342 (insert-buffer-substring rmailbuf
)
343 ;; Make sure messages are separated.
344 (goto-char (point-max))
347 ;; ``Quote'' "^From " as ">From "
348 ;; (note that this isn't really quoting, as there is no requirement
349 ;; that "^[>]+From " be quoted in the same transparent way.)
350 (let ((case-fold-search nil
))
351 (while (search-forward "\nFrom " nil t
)
355 (let ((buffer (get-file-buffer (car fcc-list
))))
357 ;; File is present in a buffer => append to that buffer.
358 (let ((curbuf (current-buffer))
359 (beg (point-min)) (end (point-max)))
362 ;; Keep the end of the accessible portion at the same place
363 ;; unless it is the end of the buffer.
364 (let ((max (if (/= (1+ (buffer-size)) (point-max))
368 (narrow-to-region (point-min) (1+ (buffer-size)))
369 (goto-char (point-max))
370 (if (eq major-mode
'rmail-mode
)
371 ;; Append as a message to an RMAIL file
372 (let ((buffer-read-only nil
))
373 ;; This forces RMAIL's message counters to be
374 ;; recomputed when the next RMAIL operation is
375 ;; done on the buffer.
376 ;; See rmail-maybe-set-message-counters.
377 (setq rmail-total-messages nil
)
378 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
379 "From: " (user-login-name) "\n"
380 "Date: " (current-time-string) "\n")
381 (insert-buffer-substring curbuf beg end
)
383 (rmail-set-message-counters))
384 (insert-buffer-substring curbuf beg end
)))
385 (if max
(narrow-to-region (point-min) max
))))))
386 ;; Else append to the file directly.
388 ;; Include a blank line before if file already exists.
389 (if (file-exists-p (car fcc-list
)) (point-min) (1+ (point-min)))
390 (point-max) (car fcc-list
) t
)))
391 (setq fcc-list
(cdr fcc-list
))))
392 (kill-buffer tembuf
)))
394 (defun mail-sent-via ()
395 "Make a Sent-via header line from each To or CC header line."
398 (goto-char (point-min))
399 ;; find the header-separator
400 (search-forward (concat "\n" mail-header-separator
"\n"))
402 ;; put a marker at the end of the header
403 (let ((end (point-marker))
406 (goto-char (point-min))
407 ;; search for the To: lines and make Sent-via: lines from them
408 ;; search for the next To: line
409 (while (re-search-forward "^\\(to\\|cc\\):" end t
)
410 ;; Grab this line plus all its continuations, sans the `to:'.
412 (buffer-substring (point)
414 (if (re-search-forward "^[^ \t\n]" end t
)
418 ;; Insert a copy, with altered header field name.
419 (insert-before-markers "Sent-via:" to-line
))))))
422 "Move point to end of To-field."
425 (mail-position-on-field "To"))
427 (defun mail-subject ()
428 "Move point to end of Subject-field."
431 (mail-position-on-field "Subject"))
434 "Move point to end of CC-field. Create a CC field if none."
437 (or (mail-position-on-field "cc" t
)
438 (progn (mail-position-on-field "to")
442 "Move point to end of BCC-field. Create a BCC field if none."
445 (or (mail-position-on-field "bcc" t
)
446 (progn (mail-position-on-field "to")
447 (insert "\nBCC: "))))
450 "Add a new FCC field, with file name completion."
453 (or (mail-position-on-field "fcc" t
) ;Put new field after exiting FCC.
454 (mail-position-on-field "to"))
455 (insert "\nFCC: " (read-file-name "Folder carbon copy: ")))
457 (defun mail-position-on-field (field &optional soft
)
459 (case-fold-search t
))
460 (goto-char (point-min))
461 (re-search-forward (concat "^" (regexp-quote mail-header-separator
) "\n"))
462 (setq end
(match-beginning 0))
463 (goto-char (point-min))
464 (if (re-search-forward (concat "^" (regexp-quote field
) ":") end t
)
466 (re-search-forward "^[^ \t]" nil
'move
)
468 (skip-chars-backward "\n")
471 (progn (goto-char end
)
472 (skip-chars-backward "\n")
473 (insert field
": \n")
474 (skip-chars-backward "\n")))
478 "Move point to beginning of text field."
480 (goto-char (point-min))
481 (search-forward (concat "\n" mail-header-separator
"\n")))
483 (defun mail-signature (atpoint)
484 "Sign letter with contents of ~/.signature file."
488 (goto-char (point-max)))
489 (skip-chars-backward " \t\n")
492 (delete-region (point) (point-max)))
494 (insert-file-contents (expand-file-name "~/.signature"))))
496 (defun mail-fill-yanked-message (&optional justifyp
)
497 "Fill the paragraphs of a message yanked into this one.
498 Numeric argument means justify as well."
501 (goto-char (point-min))
502 (search-forward (concat "\n" mail-header-separator
"\n") nil t
)
503 (fill-individual-paragraphs (point)
508 (defun mail-yank-original (arg)
509 "Insert the message being replied to, if any (in rmail).
510 Puts point before the text and mark after.
511 Normally, indents each nonblank line ARG spaces (default 3).
512 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
514 Just \\[universal-argument] as argument means don't indent, insert no prefix,
515 and don't delete any header fields."
517 (if mail-reply-buffer
518 (let ((start (point)))
519 (delete-windows-on mail-reply-buffer
)
520 (insert-buffer mail-reply-buffer
)
523 (mail-yank-clear-headers start
(mark))
524 (if (null mail-yank-prefix
)
525 (indent-rigidly start
(mark)
526 (if arg
(prefix-numeric-value arg
) 3))
529 (while (< (point) (mark))
530 (insert mail-yank-prefix
)
532 (exchange-point-and-mark)
533 (if (not (eolp)) (insert ?
\n)))))
535 (defun mail-yank-clear-headers (start end
)
538 (if (search-forward "\n\n" end t
)
540 (narrow-to-region start
(point))
542 (while (let ((case-fold-search t
))
543 (re-search-forward mail-yank-ignored-headers nil t
))
545 (delete-region (point)
546 (progn (re-search-forward "\n[^ \t]")
550 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
553 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions
)
554 "Edit a message to be sent. Argument means resume editing (don't erase).
555 Search for an existing mail buffer currently not in use and initialize it,
556 or make a new one if all existing mail buffers are busy.
557 With an argument, search for a busy existing mail buffer and re-select it.
559 Returns with message buffer selected; value t if message freshly initialized.
560 By default, the signature file `~/.signature' is inserted at the end;
561 see the variable `mail-signature'.
564 While editing message, type \\[mail-send-and-exit] to send the message and exit.
566 Various special commands starting with C-c are available in sendmail mode
567 to move to message header fields:
570 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
571 when the message is initialized.
573 If `mail-default-reply-to' is non-nil, it should be an address (a string);
574 a Reply-to: field with that address is inserted.
576 If `mail-archive-file-name' is non-nil, an FCC field with that file name
579 If `mail-setup-hook' is bound, its value is called with no arguments
580 after the message is initialized. It can add more default fields.
582 When calling from a program, the second through fifth arguments
583 TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
584 the initial contents of those header fields.
585 These arguments should not have final newlines.
586 The sixth argument REPLYBUFFER is a buffer whose contents
587 should be yanked if the user types C-c C-y.
588 The seventh argument ACTIONS is a list of actions to take
589 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
590 when the message is sent, we apply FUNCTION to ARGS.
591 This is how Rmail arranges to mark messages `answered'."
595 ;; If requested, look for a mail buffer that is modified and go to it.
598 (while (and (setq buffer
599 (get-buffer (if (= 1 index
) "*mail*"
600 (format "*mail*<%d>" index
))))
601 (not (buffer-modified-p buffer
)))
602 (setq index
(1+ index
)))
603 (if buffer
(switch-to-buffer buffer
)
604 ;; If none exists, start a new message.
605 ;; This will never re-use an existing unmodified mail buffer
606 ;; (since index is not 1 anymore). Perhaps it should.
607 (setq noerase nil
))))
608 ;; Unless we found a modified message and are happy, start a new message.
611 ;; Look for existing unmodified mail buffer.
612 (while (and (setq buffer
613 (get-buffer (if (= 1 index
) "*mail*"
614 (format "*mail*<%d>" index
))))
615 (buffer-modified-p buffer
))
616 (setq index
(1+ index
)))
617 ;; If none, make a new one.
619 (setq buffer
(generate-new-buffer "*mail*")))
620 ;; Go there and initialize it.
621 (switch-to-buffer buffer
)
623 (setq default-directory
(expand-file-name "~/"))
624 (auto-save-mode auto-save-default
)
626 (mail-setup to subject in-reply-to cc replybuffer actions
)
627 (if (and buffer-auto-save-file-name
628 (file-exists-p buffer-auto-save-file-name
))
629 (message "Auto save file for draft message exists; consider M-x mail-recover"))
633 (define-key ctl-x-map
"m" 'mail
)
635 (defun mail-recover ()
636 "Reread contents of current buffer from its last auto-save file."
638 (let ((file-name (make-auto-save-file-name)))
639 (cond ((save-window-excursion
640 (if (not (eq system-type
'vax-vms
))
641 (with-output-to-temp-buffer "*Directory*"
642 (buffer-disable-undo standard-output
)
643 (call-process "ls" nil standard-output nil
"-l" file-name
)))
644 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
645 (let ((buffer-read-only nil
))
647 (insert-file-contents file-name nil
)))
648 (t (error "mail-recover cancelled.")))))
651 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
652 "Like `mail' command, but display mail buffer in another window."
654 (let ((pop-up-windows t
))
655 (pop-to-buffer "*mail*"))
656 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
659 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
660 "Like `mail' command, but display mail buffer in another frame."
662 (let ((pop-up-frames t
))
663 (pop-to-buffer "*mail*"))
664 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
668 (define-key ctl-x-4-map
"m" 'mail-other-window
)
671 (define-key ctl-x-5-map
"m" 'mail-other-frame
)
674 ;;; Do not add anything but external entries on this page.
678 ;;; sendmail.el ends here