0

I am trying to reply to messages larger than a certain size then forward to another user. Got this, but nothing happens. Its seem I am only able to add text to the end of the message.

:0
* > 1000
{
  :0 fhw
  | cat - ; echo "Insert this text at the top of the body"
  :0
  | formail -rk
  | $SENDMAIL -t
}
cmdln
  • 77
  • 2
  • 8

2 Answers2

1

Using sed helped a lot.

SEDSCRIPT='0,/^$/ s//\nLarge message rejected [Max=4MB]\n/'
MAILADDR=me@nowhere

:0
* > 4000000
* !^FROM_DAEMON
* !^X-Loop: $MAILADDR
| formail -rk -A "X-Loop: $MAILADDR" \
| sed "$SEDSCRIPT" \
| $SENDMAIL -t
cmdln
  • 77
  • 2
  • 8
  • If the `sed` script doesn't contain literal newlines there's no need per se to put it in a variable. – tripleee Jun 29 '17 at 09:06
  • Putting the `sed` before the `formail` would avoid pushing the entire message through the pipeline. – tripleee Jun 29 '17 at 09:07
  • @tripleee I took your advise on the `sed` script and rather broke out the response message into a variable. Not sure about running the script before `formail` though, how would I insert text in the correct place before the reply has been generated? – cmdln Jul 05 '17 at 12:51
  • Oh, my bad, you're right, you'll get a different end result if you switch them around. You could pipe just the headers to `formail` and then add a new body instead, I suppose, to avoid pushing through a huge blob which you are going to discard anyway. – tripleee Jul 05 '17 at 13:40
  • 1
    Putting the `sed` script in a variable is a workaround for the situation where the script itself needs to contain a literal newline between the quotes, but yours doesn't, so in the script's current form that's an unnecessary complication. – tripleee Jul 05 '17 at 13:43
0

It's not clear what exactly is wrong, but if you want to append text at the beginning, you obviously need to echo before cat, and work on the body (b), not the headers (h).

  :0 fbw
  | echo "Insert this"; cat -

I suppose you could technically break the headers by appending something at the end, but if you want it to appear in the body, it needs to have a neck (a newline) before it.

:0 fhw
| cat -; echo; echo "Insert this"

There is also a sed syntax which allows for somewhat more flexible manipulation (sed addressing lets you say things like "before the first line which starts with > for example) but getting newlines into sed command lines inside Procmail is hairy. As a workaround, I often use a string, and then just interpolate that. (How hairy exactly depends on details of sed syntax which are not standard. Some implementations seem to require newlines in the a and i commands.)

sedscript='1i\
insert this\
'
:0 fbw
| sed "$sedscript"

(If you are lucky, your sed will accept something simpler like sed '1i insert this'. The variant above seems to be the only one I can get to work on macOS, and thus probably generally *BSD.)

As an aside, a message which is 1000 bytes long isn't by any means large. I recall calculating an average message length of about 4k in my own inbox, but this was before people started to use HTML email clients. Depending on your inbound topology, just the headers could easily be more than 1000 bytes.

tripleee
  • 175,061
  • 34
  • 275
  • 318