[gnu.emacs] rmail-extract-rejected-message

raible@EW09.NAS.NASA.GOV (Eric L. Raible) (11/29/88)

I think that anyone who uses rmail will find this to be a welcome addition.

(defvar mail-unsent-separator "^   ----- Unsent message follows -----$")

(setq rmail-mode-hook
      '((lambda ()
	  (define-key rmail-mode-map "e" 'rmail-extract-rejected-message))))

(defun rmail-extract-rejected-message ()
  "Edit a mail message which is based on the contents of the current message.

For a message rejected by the mail system, extract the interesting headers and
the body of the original message; otherwise copy the current message."
  (interactive)
  (rmail-beginning-of-message)
  (re-search-forward mail-unsent-separator nil t)
  (let* ((to   (rmail-get-field "To"))
	 (subj (rmail-get-field "Subject"))
	 (irp2 (rmail-get-field "In-reply-to"))
	 (cc   (rmail-get-field "Cc"))
	 (orig-message (buffer-substring
			(progn (search-forward "\n\n") (point))
			(point-max)))
	 (mail-setup-hook (function (lambda ()
				      (goto-char (point-max))
				      (insert orig-message)
				      (mail-to)))))
    (rmail-beginning-of-message)
    (mail-other-window nil to subj irp2 cc (current-buffer))))

(defun rmail-get-field (field-name)
  (save-excursion
    (let ((case-fold-search t))
      (if (re-search-forward (concat "^" field-name ": ") nil t)
	  (buffer-substring (point) (progn (end-of-line) (point)))))))

raible@EW09.NAS.NASA.GOV (Eric L. Raible) (01/21/89)

mh-mode has it, rmail mode didn't, so here it is.

I put rmail-extract-rejected-message on 'e' (and use x for expunge).

;; GNU Emacs and this file "rmail-extract.el", is distributed in the hope
;; that it will be useful, but WITHOUT ANY WARRANTY.  No author or
;; distributor accepts responsibility to anyone for the consequences
;; of using it or for whether it serves any particular purpose or
;; works at all, unless he says so in writing.  Refer to the GNU Emacs
;; General Public License for full details.

;; Everyone is granted permission to copy, modify and redistribute GNU
;; Emacs and rmail-extract.el, but only under the conditions described in
;; the GNU Emacs General Public License.  A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you can
;; know your rights and responsibilities.  It should be in a file
;; named COPYING.  Among other things, the copyright notice and this
;; notice must be preserved on all copies.


(defvar mail-unsent-separator "^   ----- Unsent message follows -----$")

(defun rmail-extract-rejected-message ()
  "Create a message to be sent based on the current RMAIL message.
Any text in the RMAIL message before mail-unsend-separator is ignored.

This function can also be meaningfully used to messages that have *not*
been rejected.  Try it and see."
  (interactive)
  (rmail-beginning-of-message)
  (re-search-forward mail-unsent-separator nil t)
  (let* ((to   (rmail-get-field "To"))
	 (subj (rmail-get-field "Subject"))
	 (irp2 (rmail-get-field "In-reply-to"))
	 (cc   (rmail-get-field "Cc"))
	 (orig-message (buffer-substring
			(progn (re-search-forward "\n\n") (point))
			(point-max)))
	 (mail-setup-hook (function (lambda ()
				      (goto-char (point-max))
				      (insert orig-message)
				      (mail-to)))))
    (rmail-beginning-of-message)
    (mail-other-window nil to subj irp2 cc (current-buffer))))

(defun rmail-get-field (field-name)
  (save-excursion
    (let ((case-fold-search t))
      (if (re-search-forward (concat "^" field-name ": ") nil t)
	  (buffer-substring (point) (progn (end-of-line) (point)))))))