Change _MailMixin.send signature and procedure
-
Add the parameter
envelope_from=Noneto be able to modify the envelope from from client code. The former version hadenvelope_fromparameter only inConnection.sendand it was impossible to call from client code without hacking a little. -
In order to use this parameter, the mail is sent using the
connectionobject instead of themessageobject. This also reduce one level of innecesary indirection (Message.sendonly doesconnection.send(self))
There was no simple way to call Connection.send with envelope_form parameter. Normal client code should go like this (as stated in the docs)
msg = Message(...)
mail.send(msg) #nowhere to set seamlessly the envelope_from argument
is this actually the need to differentiate between Sender and From?
The need arose when we wanted to redirect bounce emails to another account. Say [email protected] sends an email to a client, but that client's email not longer exists, so the server will return a bounce email saying that the mail was not found. That mail is normally sent to the "envelope-form" field which is almost always the same as the sender. In our case, we wanted to track this in another mail account so there's the motivation to the pull request.
that sounds exactly like the scenario for differentiating From the author of the message, to the Sender the agent who will deliver the authored message (and therefor should not receive the actual replies).
e.g. here: http://tools.ietf.org/html/rfc5322#section-3.6.2
Hi, any feedbacks on this? Can I help somehow? This is a very simple change but I will be glad to provide some tests if needed or help in something.
@pbu88 I'm open to exposing this better from the Message class rather than by send(). Have you tried using mail_options to achieve this goal?
We could add an envelope_from field to the Message class and have Connection.send method (https://github.com/mattupstate/flask-mail/blob/master/flask_mail.py#L188) do this instead:
self.host.sendmail(sanitize_address(message.envelope_from or message.sender)
This will deprecate the need for an envelope_from optional argument in that method. Unfortunately, I don't see a way of doing this without actually modifying the current code. Envelope from is the first argument to SMTP's FORM command and is also the first argument of SMTP.sendmail() method. It makes sense to me to have it on the Message object too, but I took this path because I already saw it as an optional argument on Connection.send() method.
The actual use case for this is not only hiding the originator mail (or changing it). It's main purpose is control the bounce when the mail failed to deliver. I'll love to hear some feedback about this :)
Actually, I'm not that comfortable with including envelope_from in Message object. In my opinion Message objects represent the content passed to the SMTP's DATA command. Envelope from is argument of SMTP's MAIL command, they are somehow independent from each other. Since, I think, the Mail object plays a nice role abstracting the SMTP conversation between the client and the server, the envelope from argument should be handled in Mail object (_MailMixin in this case).