Rewrite Mail sending address on the fly

Aug 23, '06 07:30:03AM

Contributed by: tobbyst

A slightly long-winded hint, but one that I think lots of you out there will find quite useful. Firstly though, why am I interested in changing how my email address looks depending on where it gets sent to? Am I an aspiring spammer, plotting to convince you that your Mac will be plagued by viruses unless you buy lots of v1agra from me?

Not at all! The reason is this: I have a domain. They are fairly cheap to buy and have a feature such that any_email@yourdomain.address will be caught and forwarded to the same 'catch-all' address. Whenever I contact a company, I like to say that my address is TheCompanyName@MyDomain.com, particularly if they are not a reputable company. That way, if they sell my email address to a spammer (or whatever), it is only their specific address that is compromised, and that can easily be configured to reject all mail via your domain host. I also find it useful for advanced searching: all of my eBay correspondence (i.e. eBay, PayPal, and whoever I buy/sell from) use the same address on my domain, so I can quickly check out my eBay activity despite that fact that these people may have nothing else in common.

But now onto the crux of the hint ... if I use Mail to send an email to DodgyCompany Inc, how do I make it look like the message is indeed from dodgy@MyDomain.com? I could go into Mail's account preferences and change the From text. Or I could have a different account set up for each company that I send to. But both of these ideas are quite tiresome for anything other than a one-off. Besides, this kind of mail manipulation is bread and butter stuff to Unix, and guess what lurks under the hood of our friendly Macs?

Now, we are briefly going to dabble in the nasty Unix world, but not for long; hence why I didn't mark this hint in the Unix category. We will be using postfix to send email (there are already plenty of fine hints, not to mention the Postfix Enabler, to get you going with this, so I won't go into the details here). Then we are going to use a tool called procmail (already installed on your Mac) to alter the emails.

Start by opening Finder and then press Shift-Command-G to get the Go to Folder dialog, and type in /etc/postfix, then press Return. Create a copy of master.cf somewhere safe! Now click on master.cf and press Command-I (Get Info). In the Info window, expand the Ownership & Permissions section, and then the Details to view the owner of the file. Change the owner to be yourself (you will need to provide an administrator password to do this). Now click on a blank part of the folder and do the same again (i.e. press Command-I and change the owner to be yourself) and then open master.cf in TextEdit.

This file looks nasty but don't worry! We need to change two small parts. First look (about halfway down the file) for a line like this:

smtp      inet  n       -       n       -       -       smtpd
Now add the following directly beneath:
        -o content_filter=filter:dummy
When done, the two lines should look like this:
smtp      inet  n       -       n       -       -       smtpd
        -o content_filter=filter:dummy
One more change to go! Add the following to the bottom of the file (replace joebloggs with your actual user name):
filter    unix  -       n       n       -       10      pipe
    flags=Rq user=joebloggs argv=/usr/bin/procmail /Users/joebloggs/.mailrecipe
Now save master.cf, open up the info windows on both the master.cf file and its folder, and change the owner back to system.

But what have we achieved by doing the above? We have told postfix that whenever it receives mail via smtp (e.g. from Mail), to send the mail for extra processing to procmail. Now, those crazy procmail developers call each procmail rule a 'recipe.' These recipes can be a little tricky to write, and because I promised that this wasn't really a Unix hint, I've written a basic utility called Recipe Manager to generate the recipes for you! (Instructions for its use are on the linked site.)

If you're still reading this hint, then give yourself a pat on the back! You now have (if you've downloaded the application and run it) a Postfix/procmail system set up on your Mac! If Postfix isn't running and you haven't yet looked at the other Postfix hints, type sudo Postfix start into a Terminal window (you may need to do this as an administrator user) to start it up.

All that remains is to point Mail at your shiny new Postfix server. To do this, go to the Accounts section of Mail's Preferences, select the Outgoing Mail Server (SMTP) list, click on Add server..., and enter localhost in the Outgoing Mail Server box. Click OK. Now, whenever you select 'localhost' as the SMTP server, you will be using the address rewriting facility from this hint. Select your old server to return to normality.

[robg adds: I haven't tested this one...]

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060820100312135