Discussion:
[VM] Filter outgoing mail
blueman
2012-05-04 13:11:08 UTC
Permalink
I would like to filter my *outgoing* mail composed in 'vm'.
I want to do this from Emacs at the last possible moment before the
email leaves emacs/vm...

Specifically, I have a perl filter that I would like to run on each
email.

What is the best way to hook into vm here. I know I could use
vm-mail-send-hook but that is a bit klugey in that I would have to first
read and that write back the region itself. I was curious whether there
are any hooks and/or functions more amenable to filtering.

Thanks!
blueman
2012-05-04 21:27:46 UTC
Permalink
Post by blueman
I would like to filter my *outgoing* mail composed in 'vm'.
I want to do this from Emacs at the last possible moment before the
email leaves emacs/vm...
Specifically, I have a perl filter that I would like to run on each
email.
What is the best way to hook into vm here. I know I could use
vm-mail-send-hook but that is a bit klugey in that I would have to first
read and that write back the region itself. I was curious whether there
are any hooks and/or functions more amenable to filtering.
Well, I wasn't able to find any hooks, but I solved my problem for now
by putting 'before' advice on the function smtpmail-via-smtp. That is
where the final changes are made to the email (including adding on mime
& content headers and stripping off the on RFC standard headers) before
it is actually sent.

The function I wrote is of the following form -- it can be adapted as
needed...

(defvar smtpmail-filter-email-command ""
"Command to run as filter in smtpmail-via-smtp function advice. Note: the
command must return 0 on success. (JJK).")

(defadvice smtpmail-via-smtp
(before smtpmail-via-smtp-filter-email nil activate)
"Run filter function 'smtpmail-filter-email-command' on outgoing email
if an only if the header 'X-JJKFILTER' is present in the header. Note
that header field is removed in the sent email.(JJK - advice)"
(save-excursion
(with-current-buffer smtpmail-text-buffer
(and (re-search-forward "\n\n" nil t)
(re-search-backward "^X-JJKFILTER:" nil t)
(progn
(delete-region (bol-point) (1+(eol-point)))
(unless (= (shell-command-on-region (point-min) (point-max) smtp-filter-email-command nil t) 0)
(error "ERROR: Failed to run smtpmail filter... sending aborted!")))))))
Uday Reddy
2012-05-05 07:18:25 UTC
Permalink
Post by blueman
Well, I wasn't able to find any hooks, but I solved my problem for now
by putting 'before' advice on the function smtpmail-via-smtp. That is
where the final changes are made to the email (including adding on mime
& content headers and stripping off the on RFC standard headers) before
it is actually sent.
Hi JJK, I couldn't see why this can't be done in vm-mail-send-hook or its
senior, mail-send-hook.

Structurally, tagging this on to smtpmail isn't quite right because it has
nothing to do with smtpmail as such. It wouldn't fire if you use some other
mail sending mechanism.

Cheers,
Uday
blueman
2012-06-04 04:14:58 UTC
Permalink
Post by Uday Reddy
Post by blueman
Well, I wasn't able to find any hooks, but I solved my problem for now
by putting 'before' advice on the function smtpmail-via-smtp. That is
where the final changes are made to the email (including adding on mime
& content headers and stripping off the on RFC standard headers) before
it is actually sent.
Hi JJK, I couldn't see why this can't be done in vm-mail-send-hook or its
senior, mail-send-hook.
The problem is that those hooks come too early in the process before all
the headers and mimification is done.
Post by Uday Reddy
Structurally, tagging this on to smtpmail isn't quite right because it has
nothing to do with smtpmail as such. It wouldn't fire if you use some other
mail sending mechanism.
I agree but I couldn't find any earlier hooks and this was the only
"clean" (though non-natural) place I could add my code via 'advice'
without hacking into the actual vm routines themselves.

Basically, I wanted the ability to modify the outgoing email when the
header & body is complete and it is just about to be sent off to the
mail transfer agent whether smtpmail or anything else.

Unfortunately, I couldn't find a hook that gave access to the entire
final email header and body. If I am missing something, please enlighten
me.

Continue reading on narkive:
Loading...