On Thu, Nov 12, 2020 at 05:58:01PM +0100, Raphaël Fournier-S'niehotta <raph...@raphaelfournier.net> wrote:
> Hello fellow mutt users, > > When emailing to someone with mutt, I would like to have my messages > automatically CC'd to someone. Typically, we are a team of academics > working with a student, and I would like my exchanges with the student > to be sent to others. > > Mutt aliases seems to be on a "per field" basis. So I could use it to > put my coworkers in an alias, but they would be in the "To:" field, > not "CC". > > I wanted to use a send-hook and the my_hdr variable, but it works for > other fields (such as adding a "Organization: A Really Big Company, > Anytown, USA", from the manual). But not for the CC field (the manual > says: "note that my_hdr commands which modify recipient headers, or > the message's subject, don't have any effect on the current message > when executed from a send-hook"). > > Is there a workaround? Perhaps with the recent muttlisp? > > Setting edit_headers to yes for all my messages(and having a nice > macro in my editor) would not help, since I'd like such a behavior for > only a fraction of my communication. > > Thanks in advance! > -- > Raphaël Fournier-S'niehotta > http://raphael.fournier-sniehotta.fr/apropos Hi, I needed to do something very much like this. I needed to automatically Cc: all my work-related emails. But I found that the problem with doing it entirely in mutt was that the hooks that looked most likely to assist couldn't affect the recipient addresses (due to the order in which things happen inside mutt). That's the same limitation that you encountered. I had configured mutt to automatically set my from address, depending on where the email is being sent to. For example, when sending to this list, From: is m...@raf.org. When sending to a work-related address (e.g. colleague, vendor, government), From: is my work address. e.g.: send-hook '~t @work\.com\.au' 'my_hdr From: Me <m...@work.com.au>' send-hook '~t @vendor\.com\.au' 'my_hdr From: Me <m...@work.com.au>' send-hook '~t @ato\.gov\.au' 'my_hdr From: My Self <m...@work.com.au>' Then, the auto-cc is handled by an editor script: set editor = "~/bin/mutt-editor" Editing headers is a requirement: set edit_hdrs But no in-editor macros are needed. The editor script looks like this (~/bin/mutt-editor): #!/bin/sh # mutt-editor - Preprocessor/editor/postprocessor for mutt # Delete empty recipient headers perl -pi -e '$_ = "" if /^(Cc|Bcc|Reply-To): $/' "$@" # Add Cc: headers automatically when necessary mutt-autocc d...@work.com.au "$@" mutt-autocc k...@work.com.au "$@" mutt-autocc s...@work.com.au "$@" # Launch the editor ${VISUAL:-${EDITOR:-vi}} "$@" # Add Cc: headers automatically when necessary mutt-autocc d...@work.com.au "$@" mutt-autocc k...@work.com.au "$@" mutt-autocc s...@work.com.au "$@" The mutt-autocc script automatically adds a Cc: header if the From: address is my work address, or if the To: or Cc: headers contain work-related domains, and if the intended new recipient isn't already going to receive the email. It looks like this (~/bin/mutt-autocc): #!/usr/bin/perl -i use warnings; use strict; # mutt-autocc - Mail message preprocesor/filter # to automatically add a Cc: header when # sending/replying/forwarding from my work domain # or to an obviously work-related domain. # # usage: mutt-autocc cc@address filename my $cc_address = shift @ARGV; my $from_domain = qr/\@work\.com\.au\b/; my $to_domains = qr/\@(?:work\.com\.au|vendor\.com\.au|ato\.gov\.au)\b/; my $from_match = 0; my $to_match = 0; my $already = 0; while (<>) { # Does the From: header contain the from_domain? $from_match = 1 if /^From: .*$from_domain/i; # Do the To/Cc headers contain any of the to_domains? $to_match = 1 if /^(?:To|Cc): .*$to_domains/i; # Is the cc_address already going to receive the email? $already = 1 if /^(?:To|Cc|Bcc): .*\b\Q$cc_address\E\b/i; $already = 1 if /^ .*\b\Q$cc_address\E\b/i; # Add Cc: header if necessary once we've seen the recipient headers $already = 1, print("Cc: $cc_address\n") if /^Subject: / && ($from_match || $to_match) && !$already; # Print all lines print; } # vi:set ts=4 sw=4: Calling it before the real editor would usually be enough to add the Cc: headers before I start editing the email. But if I have missed any destination addresses in my send hooks, and I need to manually change the From: address to my work address, then the postprocessing phase will handle the rest. The same is true if the email wasn't originally being sent to the relevant recipient, but they are added manually during the editor session. Warning: As Cameron so rightly pointed out, automatically cc-ing can lead to unintended emails being sent. Especially when you have configured mutt to automatically set your From: address for one email, and it just stays that way by default for the next email. And even more especially when addresses can be added by a postprocessor after the editor. So, I strongly recommend, that if you use an approach like this, that after you have completed editing your email, and leave the editor, you go back into the editor (with the 'e' keystroke), to check the email again before sending it (with the 'y' keystroke). However, if you only need to auto-cc based on the To: header (and not the From: header), this probably isn't a concern, and you probably don't have to be so careful. But you'll need to assess the risks yourself. Also note that mutt will coalesce multiple Cc: headers into a single one after editing, so you don't need to worry about creating multiple Cc: headers. I hope that helps. If you think this approach will do what you need, but you'd like help modifying the perl to match your exact needs, let me know and I'll modify it for you. I'm assuming that your environment is UNIXy enough for all this to be possible. That might not be the case. cheers, raf