hmind
hmind

Reputation: 880

Send email to address alternate from "To:"

I am implementing a sort of dynamic mailing-list system in Rails. I am looking to basically send an email and have the recipient receive it in this form:

From: [email protected]
To: [email protected]

<body>

Basically, the challenge is how do I send an email to an address while defining a different To: header so that they can easily reply to the mailing list or just the original sender?

Upvotes: 2

Views: 1270

Answers (2)

fiedl
fiedl

Reputation: 6137

Mail vs. Envelope

In emails as in physical mails (paper sheet in paper envelope), the recipient on the envelope may differ from the recipient on the letter sheet itself.

As the mailman would only consider the envelope's recipient, so do mail servers.

That means, one actually can tell the smtp service to send and email to a recipient different than the one listed in the To: field of the emails header.

Trying This Out Manually

You can try this out, manually, for example, by using the sendmail command of postfix.

# bash
sendmail [email protected] < mail.txt

where

# mail.txt
From: [email protected]
To: [email protected]
Subject: Testmail

This is a test mail.

This will deliver the email to [email protected], not to [email protected], but the latter will be shown in the recipient field of the mail client.

Specifying the Envelope_to Field in Rails

The ActionMailer::Base internally uses the mail gem. Currently, Apr 2013, there is a pull request, allowing to specify the envelope_to field on a message before delivery.

Until this is pulled, you can specify the fork in the Gemfile.

# Gemfile 
# ...
gem 'mail', git: 'git://github.com/jeremy/mail.git'

You need to run bundle update mail after that, and, depending on your current rails version, maybe also bundle update rails in order to resolve some dependency issues.

After that, you can set the envelope recipient of a mail message like this:

# rails
message # => <Mail::Message ...>
message.to = [ "[email protected]" ]
message.smtp_envelope_to = [ "[email protected]" ]
message.from = ...
message.subject = ...
message.body = ...
message.deliver

Documentation

Upvotes: 6

Atastor
Atastor

Reputation: 741

Why not use the BCC header for this? Put [email protected] into BCC and [email protected] into TO.

Clearification:

There is no technical solution for this. The email headers do what they do, you can't influence them in that once the email is on the way.

I am sure, though, you can find a combined use of TO, CC, BCC and REPLY-TO that gives you what you want.

Upvotes: 0

Related Questions