LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Rewriting mail subject with Formail (https://www.linuxquestions.org/questions/linux-server-73/rewriting-mail-subject-with-formail-624233/)

Sheridan 02-27-2008 09:50 AM

Rewriting mail subject with Formail
 
Hi Guys,

I've done a little script that redirects all PHP mails to a single mailbox from the development server using formail and sendmail. The script is like this:

Code:

#!/bin/bash
/usr/bin/formail -R to X-original-to -R cc X-original-cc -R bcc X-original-bcc -f -A "to: betadump@mycompany.com" | sendmail -t -i

Essentially, you call this script exactly like you would call sendmail. For example:

Code:

> echo "test mail" | ./mrewrite my@address.com
It works as far as:
- It redirects all emails to single mailbox
- It even inserts X-original-to,bcc,cc headers to know the intended recipient of the mails

However, now developers are asking that it would be so good if on top of this I were to attach the original recipients (cc and bcc included) into the subject of the mails, like so:

Code:

Subject: (to: originalto@foo.bar, original2to@foo.bar, cc: originalcc, bcc: originalbcc) --- Original Subject of the Mail
I have read formail doc but I still don't see how. Is there a way to do that with formail or some bash scripting perhaps?

jgombos 02-28-2008 09:26 PM

AFAIK, it's just the -A switch to append. You're already using it for the TO field, so I'm not sure why that didn't occurr to you.. maybe I'm misunderstanding what it does.

Sheridan 02-29-2008 01:48 AM

Quote:

Originally Posted by jgombos (Post 3073413)
AFAIK, it's just the -A switch to append. You're already using it for the TO field, so I'm not sure why that didn't occurr to you.. maybe I'm misunderstanding what it does.

You are right - but my real problem is: How do I reference to the original values of "to", "cc", etc. inside -A ?

Like: -A "Subject: $to, $cc ..." or -A cc." - ".to." - ...", or how? The point is that I need to include the original recipients in the subject line, not just the header (that's working already).

Ok I'm obviously using this tool for the first time so I maybe missing some very trivial thing. But still...

jgombos 02-29-2008 08:33 AM

This is how I think it might be done (untested):

formail -A "Subject: (to: $(formail -x To: <filename>))"

That only uses the To field to keep the example simple. I'm guessing that both instances of formail can't use stdin. I'm uncertain, but <filename> could perhaps be /dev/stdin if you want to try it.

Also, I used $(formail...) for command substitution. Alternatively, ticks would work too: eg. `formail...`


All times are GMT -5. The time now is 07:13 PM.