How to generate dynamic “Reply-To:” based on “Message-ID:”? [+detail]

这一生的挚爱 提交于 2020-01-01 08:53:26

问题


How can you generate a dynamic "Reply-To:" (and "From:") header in emacs/gnus based on Message-ID of the created message? I would like to use external (perl) script to generate a dynamic +detail part based on the "Messaged-ID:" header.

user+detail@example.net

I have managed to create a header with content generated by my external script. The script gets usenet group name as command line parameter. I would like to pass it the message-id value too.

My current code
~/.emacs :

'(gnus-posting-styles ("^pl\\.test$" ("Reply-To" message-make-reply-to)))

~/.gnus

(defun message-make-reply-to()
  (my-script ".../reply-to.pl" (message-fetch-field "Message-Id")))

(defun my-script(path &optional param) ....

The problem: the script does not receive message-id as its parameter (my-script gets correctly explicitly set parameter)


回答1:


;; Make sure the Message-ID header is present in newly created messages
(setq message-generate-headers-first '(Message-ID))

;; Prevent emacs from resetting the Message-ID before the message is sent.
(setq message-deletable-headers
      (remove 'Message-ID message-deletable-headers))

(setq gnus-posting-styles
      '(("^pl\\.test$"
         ("Reply-To" '(message-make-reply-to)))))

Note the additional quote and parentheses around message-make-reply-to. The explanation for this is that the function is run at different times, depending on whether it's given as a symbol or as a quoted s-expression.

  • If given as symbol, it is run when a lambda function is added to message-setup-hook. That happens in a message-mode-hook, i.e. right after the new buffer is created and switched into message-mode. The cause for this is some wild quoting/unquoting of values during creation of the lambda function.
  • If given as a quoted sexpr, evaluation is delayed until after the buffer is filled with initial values. It is close to the last code which is run on message setup.

Alternative Solution (without gnus-posting-styles)

In cases where the new header should be added to every new message, the Reply-To header can also be set using the message-header-setup-hook. A custom hook needs to be defined to add the header for each new message.

(defun reply-to-message-header-setup-hook ()
  (let* ((msg-id (message-fetch-field "Message-ID"))
         (reply-to (my-script ".../reply-to-pl" msg-id)))
    (message-add-header (concat "Reply-To: " reply-to))))

;; Call the hook every time a new message is created
(add-hook 'message-header-setup-hook 'reply-to-message-header-setup-hook)

;; Make sure the Message-ID header is present in newly created messages
(setq message-generate-headers-first '(Message-ID))


来源:https://stackoverflow.com/questions/15310574/how-to-generate-dynamic-reply-to-based-on-message-id-detail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!