1 ;;; sendmail.el --- mail sending commands for Emacs.
3 ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 (defconst mail-self-blind nil
"\
28 Non-nil means insert BCC to self in messages to be sent.
29 This is done when the message is initialized,
30 so you can remove or alter the BCC field to override the default.")
33 (defconst mail-interactive nil
"\
34 Non-nil means when sending a message wait for and display errors.
35 nil means let mailer mail back a message to report errors.")
38 (defconst mail-yank-ignored-headers
"^via:\\|^mail-from:\\|^origin:\\|^status:\\|^remailed\\|^received:\\|^message-id:\\|^summary-line:\\|^to:\\|^subject:\\|^in-reply-to:\\|^return-path:" "\
39 Delete these headers from old message when it's inserted in a reply.")
41 ;; Useful to set in site-init.el
43 (defconst send-mail-function
'sendmail-send-it
"\
44 Function to call to send the current buffer as mail.
45 The headers are be delimited by a line which is mail-header-separator.")
48 (defvar mail-header-separator
"--text follows this line--" "\
49 *Line used to separate headers from text in messages being composed.")
52 (defvar mail-archive-file-name nil
"\
53 *Name of file to write all outgoing messages in, or nil for none.
54 Do not use an rmail file here! Instead, use its inbox file.")
56 (defvar mail-default-reply-to nil
57 "*Address to insert as default Reply-to field of outgoing messages.")
59 (defvar mail-alias-file nil
60 "*If non-nil, the name of a file to use instead of `/usr/lib/aliases'.
61 This file defines aliases to be expanded by the mailer; this is a different
62 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
63 This variable has no effect unless your system uses sendmail as its mailer.")
65 (defvar mail-yank-prefix nil
66 "*Prefix insert on lines of yanked message being replied to.
67 nil means use indentation.")
69 (defvar mail-abbrevs-loaded nil
)
70 (defvar mail-mode-map nil
)
72 (defvar mail-signature nil
73 "*Text inserted at end of mail buffer when a message is initialized.
74 If t, it means to insert the contents of the file `~/.signature'.")
76 (defvar mail-reply-buffer nil
)
77 (defvar mail-send-actions nil
78 "A list of actions to be performed upon successful sending of a message.")
80 (defvar mail-default-headers nil
81 "*A string containing header lines, to be inserted in outgoing messages.
82 It is inserted before you edit the message,
83 so you can edit or delete these lines.")
85 (defvar mail-mode-syntax-table nil
86 "Syntax table used while in mail mode.")
88 (if (null mail-mode-syntax-table
)
90 (setq mail-mode-syntax-table
(copy-syntax-table text-mode-syntax-table
))
91 (modify-syntax-entry ?%
". " mail-mode-syntax-table
)))
93 (defun mail-setup (to subject in-reply-to cc replybuffer actions
)
94 (setq mail-send-actions actions
)
96 (setq mail-reply-buffer replybuffer
)
97 (goto-char (point-min))
103 ;;; Here removed code to extract names from within <...>
104 ;;; on the assumption that mail-strip-quoted-names
105 ;;; has been called and has done so.
106 (let ((fill-prefix "\t"))
107 (fill-region (point-min) (point-max))))
112 (insert "CC: " cc
"\n")
113 (fill-region-as-paragraph opos
(point-max))))
115 (insert "In-reply-to: " in-reply-to
"\n"))
116 (insert "Subject: " (or subject
"") "\n")
117 (if mail-default-headers
118 (insert mail-default-headers
))
119 (if mail-default-reply-to
120 (insert "Reply-to: " mail-default-reply-to
"\n"))
122 (insert "BCC: " (user-login-name) "\n"))
123 (if mail-archive-file-name
124 (insert "FCC: " mail-archive-file-name
"\n"))
125 (insert mail-header-separator
"\n")
126 ;; Read the .signature file if we haven't already done so
127 ;; (and if the user has not overridden it).
128 (cond ((eq mail-signature t
)
130 (insert-file-contents "~/.signature"))
132 (insert mail-signature
)))
133 (goto-char (point-max))
134 (or (bolp) (newline)))
135 (if to
(goto-char (point-max)))
136 (or to subject in-reply-to
137 (set-buffer-modified-p nil
))
138 (run-hooks 'mail-setup-hook
))
142 "Major mode for editing mail to be sent.
143 Like Text Mode but with these additional commands:
144 C-c C-s mail-send (send the message) C-c C-c mail-send-and-exit
145 C-c C-f move to a header field (and create it if there isn't):
146 C-c C-f C-t move to To: C-c C-f C-s move to Subj:
147 C-c C-f C-b move to BCC: C-c C-f C-c move to CC:
148 C-c C-t move to message text.
149 C-c C-y mail-yank-original (insert current message, in Rmail).
150 C-c C-q mail-fill-yanked-message (fill what was yanked).
151 C-c C-v mail-sent-via (add a sent-via field for each To or CC)."
153 (kill-all-local-variables)
154 (make-local-variable 'mail-reply-buffer
)
155 (setq mail-reply-buffer nil
)
156 (make-local-variable 'mail-send-actions
)
157 (set-syntax-table mail-mode-syntax-table
)
158 (use-local-map mail-mode-map
)
159 (setq local-abbrev-table text-mode-abbrev-table
)
160 (setq major-mode
'mail-mode
)
161 (setq mode-name
"Mail")
162 (setq buffer-offer-save t
)
163 (make-local-variable 'paragraph-separate
)
164 (make-local-variable 'paragraph-start
)
165 (setq paragraph-start
(concat "^" mail-header-separator
166 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
168 (setq paragraph-separate
(concat "^" mail-header-separator
169 "$\\|^[ \t]*[-_][-_][-_]+$\\|"
171 (run-hooks 'text-mode-hook
'mail-mode-hook
))
175 (setq mail-mode-map
(nconc (make-sparse-keymap) text-mode-map
))
176 (define-key mail-mode-map
"\C-c?" 'describe-mode
)
177 (define-key mail-mode-map
"\C-c\C-f\C-t" 'mail-to
)
178 (define-key mail-mode-map
"\C-c\C-f\C-b" 'mail-bcc
)
179 (define-key mail-mode-map
"\C-c\C-f\C-f" 'mail-fcc
)
180 (define-key mail-mode-map
"\C-c\C-f\C-c" 'mail-cc
)
181 (define-key mail-mode-map
"\C-c\C-f\C-s" 'mail-subject
)
182 (define-key mail-mode-map
"\C-c\C-t" 'mail-text
)
183 (define-key mail-mode-map
"\C-c\C-y" 'mail-yank-original
)
184 (define-key mail-mode-map
"\C-c\C-q" 'mail-fill-yanked-message
)
185 (define-key mail-mode-map
"\C-c\C-w" 'mail-signature
)
186 (define-key mail-mode-map
"\C-c\C-v" 'mail-sent-via
)
187 (define-key mail-mode-map
"\C-c\C-c" 'mail-send-and-exit
)
188 (define-key mail-mode-map
"\C-c\C-s" 'mail-send
))
190 (defun mail-send-and-exit (arg)
191 "Send message like mail-send, then, if no errors, exit from mail buffer.
192 Prefix arg means don't delete this window."
195 (bury-buffer (current-buffer))
199 (set-buffer (window-buffer (next-window (selected-window) 'not
)))
200 (eq major-mode
'rmail-mode
)))
202 (switch-to-buffer (other-buffer (current-buffer)))))
205 "Send the message in the current buffer.
206 If `mail-interactive' is non-nil, wait for success indication
207 or error messages, and inform user.
208 Otherwise any failure is reported in a message back to
209 the user from the mailer."
211 (if (or (buffer-modified-p)
212 (y-or-n-p "Message already sent; resend? "))
214 (message "Sending...")
215 (run-hooks 'mail-send-hook
)
216 (funcall send-mail-function
)
217 ;; Now perform actions on successful sending.
218 (while mail-send-actions
220 (apply (car (car mail-send-actions
)) (cdr (car mail-send-actions
)))
222 (setq mail-send-actions
(cdr mail-send-actions
)))
223 (set-buffer-modified-p nil
)
224 (delete-auto-save-file-if-necessary t
)
225 (message "Sending...done"))))
227 (defun sendmail-send-it ()
228 (let ((errbuf (if mail-interactive
229 (generate-new-buffer " sendmail errors")
231 (tembuf (generate-new-buffer " sendmail temp"))
232 (case-fold-search nil
)
234 (mailbuf (current-buffer)))
239 (insert-buffer-substring mailbuf
)
240 (goto-char (point-max))
241 ;; require one newline at the end.
242 (or (= (preceding-char) ?
\n)
244 ;; Change header-delimiter to be what sendmail expects.
245 (goto-char (point-min))
247 (concat "^" (regexp-quote mail-header-separator
) "\n"))
250 (setq delimline
(point-marker))
251 (goto-char (point-min))
252 ;; ignore any blank lines in the header
253 (while (and (re-search-forward "\n\n\n*" delimline t
)
254 (< (point) delimline
))
255 (replace-match "\n"))
256 (let ((case-fold-search t
))
257 (goto-char (point-min))
258 (if (re-search-forward "^Sender:" delimline t
)
259 (error "Sender may not be specified."))
260 ;; Find and handle any FCC fields.
261 (goto-char (point-min))
262 (if (re-search-forward "^FCC:" delimline t
)
263 (mail-do-fcc delimline
))
264 ;; If the From is different than current user, insert Sender.
265 (goto-char (point-min))
266 (and (re-search-forward "^From:" delimline t
)
268 (require 'mail-utils
)
270 (mail-strip-quoted-names
272 (narrow-to-region (point-min) delimline
)
273 (mail-fetch-field "From")))
277 (insert "Sender: " (user-login-name) "\n")))
278 ;; "S:" is an abbreviation for "Subject:".
279 (goto-char (point-min))
280 (if (re-search-forward "^S:" delimline t
)
281 (replace-match "Subject:"))
282 ;; Don't send out a blank subject line
283 (goto-char (point-min))
284 (if (re-search-forward "^Subject:[ \t]*\n" delimline t
)
290 (apply 'call-process-region
291 (append (list (point-min) (point-max)
292 (if (boundp 'sendmail-program
)
297 ;; Always specify who from,
298 ;; since some systems have broken sendmails.
299 (list "-f" (user-login-name))
300 ;;; ;; Don't say "from root" if running under su.
301 ;;; (and (equal (user-real-login-name) "root")
302 ;;; (list "-f" (user-login-name)))
304 (list (concat "-oA" mail-alias-file
)))
305 ;; These mean "report errors by mail"
306 ;; and "deliver in background".
307 (if (null mail-interactive
) '("-oem" "-odb"))))
311 (goto-char (point-min))
312 (while (re-search-forward "\n\n* *" nil t
)
313 (replace-match "; "))
314 (if (not (zerop (buffer-size)))
315 (error "Sending...failed to %s"
316 (buffer-substring (point-min) (point-max)))))))
319 (kill-buffer errbuf
)))))
321 (defun mail-do-fcc (header-end)
323 (rmailbuf (current-buffer))
324 (tembuf (generate-new-buffer " rmail output"))
325 (case-fold-search t
))
327 (goto-char (point-min))
328 (while (re-search-forward "^FCC:[ \t]*" header-end t
)
329 (setq fcc-list
(cons (buffer-substring (point)
332 (skip-chars-backward " \t")
335 (delete-region (match-beginning 0)
336 (progn (forward-line 1) (point))))
339 (insert "\nFrom " (user-login-name) " "
340 (current-time-string) "\n")
341 (insert-buffer-substring rmailbuf
)
342 ;; Make sure messages are separated.
343 (goto-char (point-max))
346 ;; ``Quote'' "^From " as ">From "
347 ;; (note that this isn't really quoting, as there is no requirement
348 ;; that "^[>]+From " be quoted in the same transparent way.)
349 (let ((case-fold-search nil
))
350 (while (search-forward "\nFrom " nil t
)
354 (let ((buffer (get-file-buffer (car fcc-list
))))
356 ;; File is present in a buffer => append to that buffer.
357 (let ((curbuf (current-buffer))
358 (beg (point-min)) (end (point-max)))
361 ;; Keep the end of the accessible portion at the same place
362 ;; unless it is the end of the buffer.
363 (let ((max (if (/= (1+ (buffer-size)) (point-max))
367 (narrow-to-region (point-min) (1+ (buffer-size)))
368 (goto-char (point-max))
369 (if (eq major-mode
'rmail-mode
)
370 ;; Append as a message to an RMAIL file
371 (let ((buffer-read-only nil
))
372 ;; This forces RMAIL's message counters to be
373 ;; recomputed when the next RMAIL operation is
374 ;; done on the buffer.
375 ;; See rmail-maybe-set-message-counters.
376 (setq rmail-total-messages nil
)
377 (insert "\C-l\n0, unseen,,\n*** EOOH ***\n"
378 "From: " (user-login-name) "\n"
379 "Date: " (current-time-string) "\n")
380 (insert-buffer-substring curbuf beg end
)
382 (rmail-set-message-counters))
383 (insert-buffer-substring curbuf beg end
)))
384 (if max
(narrow-to-region (point-min) max
))))))
385 ;; Else append to the file directly.
387 ;; Include a blank line before if file already exists.
388 (if (file-exists-p (car fcc-list
)) (point-min) (1+ (point-min)))
389 (point-max) (car fcc-list
) t
)))
390 (setq fcc-list
(cdr fcc-list
))))
391 (kill-buffer tembuf
)))
393 (defun mail-sent-via ()
394 "Make a Sent-via header line from each To or CC header line."
397 (goto-char (point-min))
398 ;; find the header-separator
399 (search-forward (concat "\n" mail-header-separator
"\n"))
401 ;; put a marker at the end of the header
402 (let ((end (point-marker))
405 (goto-char (point-min))
406 ;; search for the To: lines and make Sent-via: lines from them
407 ;; search for the next To: line
408 (while (re-search-forward "^\\(to\\|cc\\):" end t
)
409 ;; Grab this line plus all its continuations, sans the `to:'.
411 (buffer-substring (point)
413 (if (re-search-forward "^[^ \t\n]" end t
)
417 ;; Insert a copy, with altered header field name.
418 (insert-before-markers "Sent-via:" to-line
))))))
421 "Move point to end of To-field."
424 (mail-position-on-field "To"))
426 (defun mail-subject ()
427 "Move point to end of Subject-field."
430 (mail-position-on-field "Subject"))
433 "Move point to end of CC-field. Create a CC field if none."
436 (or (mail-position-on-field "cc" t
)
437 (progn (mail-position-on-field "to")
441 "Move point to end of BCC-field. Create a BCC field if none."
444 (or (mail-position-on-field "bcc" t
)
445 (progn (mail-position-on-field "to")
446 (insert "\nBCC: "))))
449 "Add a new FCC field, with file name completion."
452 (or (mail-position-on-field "fcc" t
) ;Put new field after exiting FCC.
453 (mail-position-on-field "to"))
454 (insert "\nFCC: " (read-file-name "Folder carbon copy: ")))
456 (defun mail-position-on-field (field &optional soft
)
458 (case-fold-search t
))
459 (goto-char (point-min))
460 (re-search-forward (concat "^" (regexp-quote mail-header-separator
) "\n"))
461 (setq end
(match-beginning 0))
462 (goto-char (point-min))
463 (if (re-search-forward (concat "^" (regexp-quote field
) ":") end t
)
465 (re-search-forward "^[^ \t]" nil
'move
)
467 (skip-chars-backward "\n")
470 (progn (goto-char end
)
471 (skip-chars-backward "\n")
472 (insert field
": \n")
473 (skip-chars-backward "\n")))
477 "Move point to beginning of text field."
479 (goto-char (point-min))
480 (search-forward (concat "\n" mail-header-separator
"\n")))
482 (defun mail-signature (atpoint)
483 "Sign letter with contents of ~/.signature file."
487 (goto-char (point-max)))
488 (skip-chars-backward " \t\n")
491 (delete-region (point) (point-max)))
493 (insert-file-contents (expand-file-name "~/.signature"))))
495 (defun mail-fill-yanked-message (&optional justifyp
)
496 "Fill the paragraphs of a message yanked into this one.
497 Numeric argument means justify as well."
500 (goto-char (point-min))
501 (search-forward (concat "\n" mail-header-separator
"\n") nil t
)
502 (fill-individual-paragraphs (point)
507 (defun mail-yank-original (arg)
508 "Insert the message being replied to, if any (in rmail).
509 Puts point before the text and mark after.
510 Normally, indents each nonblank line ARG spaces (default 3).
511 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
513 Just \\[universal-argument] as argument means don't indent, insert no prefix,
514 and don't delete any header fields."
516 (if mail-reply-buffer
517 (let ((start (point)))
518 (delete-windows-on mail-reply-buffer
)
519 (insert-buffer mail-reply-buffer
)
522 (mail-yank-clear-headers start
(mark))
523 (if (null mail-yank-prefix
)
524 (indent-rigidly start
(mark)
525 (if arg
(prefix-numeric-value arg
) 3))
528 (while (< (point) (mark))
529 (insert mail-yank-prefix
)
531 (exchange-point-and-mark)
532 (if (not (eolp)) (insert ?
\n)))))
534 (defun mail-yank-clear-headers (start end
)
537 (if (search-forward "\n\n" end t
)
539 (narrow-to-region start
(point))
541 (while (let ((case-fold-search t
))
542 (re-search-forward mail-yank-ignored-headers nil t
))
544 (delete-region (point)
545 (progn (re-search-forward "\n[^ \t]")
549 ;; Put these last, to reduce chance of lossage from quitting in middle of loading the file.
552 (defun mail (&optional noerase to subject in-reply-to cc replybuffer actions
)
553 "Edit a message to be sent. Argument means resume editing (don't erase).
554 Search for an existing mail buffer currently not in use and initialize it,
555 or make a new one if all existing mail buffers are busy.
556 With an argument, search for a busy existing mail buffer and re-select it.
558 Returns with message buffer selected; value t if message freshly initialized.
559 By default, the signature file `~/.signature' is inserted at the end;
560 see the variable `mail-signature'.
563 While editing message, type \\[mail-send-and-exit] to send the message and exit.
565 Various special commands starting with C-c are available in sendmail mode
566 to move to message header fields:
569 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
570 when the message is initialized.
572 If `mail-default-reply-to' is non-nil, it should be an address (a string);
573 a Reply-to: field with that address is inserted.
575 If `mail-archive-file-name' is non-nil, an FCC field with that file name
578 If `mail-setup-hook' is bound, its value is called with no arguments
579 after the message is initialized. It can add more default fields.
581 When calling from a program, the second through fifth arguments
582 TO, SUBJECT, IN-REPLY-TO and CC specify if non-nil
583 the initial contents of those header fields.
584 These arguments should not have final newlines.
585 The sixth argument REPLYBUFFER is a buffer whose contents
586 should be yanked if the user types C-c C-y.
587 The seventh argument ACTIONS is a list of actions to take
588 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
589 when the message is sent, we apply FUNCTION to ARGS.
590 This is how Rmail arranges to mark messages `answered'."
594 ;; If requested, look for a mail buffer that is modified and go to it.
597 (while (and (setq buffer
598 (get-buffer (if (= 1 index
) "*mail*"
599 (format "*mail*<%d>" index
))))
600 (not (buffer-modified-p buffer
)))
601 (setq index
(1+ index
)))
602 (if buffer
(switch-to-buffer buffer
)
603 ;; If none exists, start a new message.
604 ;; This will never re-use an existing unmodified mail buffer
605 ;; (since index is not 1 anymore). Perhaps it should.
606 (setq noerase nil
))))
607 ;; Unless we found a modified message and are happy, start a new message.
610 ;; Look for existing unmodified mail buffer.
611 (while (and (setq buffer
612 (get-buffer (if (= 1 index
) "*mail*"
613 (format "*mail*<%d>" index
))))
614 (buffer-modified-p buffer
))
615 (setq index
(1+ index
)))
616 ;; If none, make a new one.
618 (setq buffer
(generate-new-buffer "*mail*")))
619 ;; Go there and initialize it.
620 (switch-to-buffer buffer
)
622 (setq default-directory
(expand-file-name "~/"))
623 (auto-save-mode auto-save-default
)
625 (mail-setup to subject in-reply-to cc replybuffer actions
)
626 (if (and buffer-auto-save-file-name
627 (file-exists-p buffer-auto-save-file-name
))
628 (message "Auto save file for draft message exists; consider M-x mail-recover"))
632 (define-key ctl-x-map
"m" 'mail
)
634 (defun mail-recover ()
635 "Reread contents of current buffer from its last auto-save file."
637 (let ((file-name (make-auto-save-file-name)))
638 (cond ((save-window-excursion
639 (if (not (eq system-type
'vax-vms
))
640 (with-output-to-temp-buffer "*Directory*"
641 (buffer-disable-undo standard-output
)
642 (call-process "ls" nil standard-output nil
"-l" file-name
)))
643 (yes-or-no-p (format "Recover auto save file %s? " file-name
)))
644 (let ((buffer-read-only nil
))
646 (insert-file-contents file-name nil
)))
647 (t (error "mail-recover cancelled.")))))
650 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
651 "Like `mail' command, but display mail buffer in another window."
653 (let ((pop-up-windows t
))
654 (pop-to-buffer "*mail*"))
655 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
658 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions
)
659 "Like `mail' command, but display mail buffer in another frame."
661 (let ((pop-up-frames t
))
662 (pop-to-buffer "*mail*"))
663 (mail noerase to subject in-reply-to cc replybuffer sendactions
))
667 (define-key ctl-x-4-map
"m" 'mail-other-window
)
670 (define-key ctl-x-5-map
"m" 'mail-other-frame
)
673 ;;; Do not add anything but external entries on this page.
677 ;;; sendmail.el ends here