Sending mail On Behalf Of with Rails

This is probably helpful to non-rails users too.
 
Today, I looked into reproducing a feature of some document management systems which was pointed out to me by a colleague.

Sending mail "On behalf of" is good for sending invitations, files, etc... it means that when the user clicks "Reply" the reply will go to the right email, and it means that they are more likely to pay attention to that email since it looks like it's coming from someone they probably know.
 
Anyway, so how to do that? There are many resources out there that tell you how to do this, but none of them are particularly clear and they don't give an example of how to test that it worked, where to test it, etc. All the advice I found was summarised as "Add a Sender header", without even much guidance as to what you're supposed to put in there. I guessed, wrongly, that the user's email was supposed to go into "Sender".
 
So, anyway, here's what it's supposed to be:
 
[code lang="ruby"]
@from = "#{sent_by.name} <#{sent_by.email}>"
@headers["Sender"] = "Your App <yourapp@yourapp.com>"
[code]
 
So, the "From" header needs to be the email of the user on behalf of whom the email is being sent, while the Sender email needs to be your application's email.
 
What will happen then?
 
Well, as far as Mail.app, gmail, and Outlook Express are concerned, not much. They don't display the "On Behalf Of" / "Sent By", so testing on those clients won't help you:

However, if you test on Entourage or Outlook, you'll get the desired behaviour (though Entourage words it differently):

Hope this helps others - I would have liked to read this post before I got started on this task!
 
Daniel