1

Closed

EmailMessageEventHandler should respect MessagePrepared flag.

description

Let's say I create my own IMessageEventHandler implementation as part of a feature that sends my own message type using my own channel. This handler adds recipients (passed through via MessageContext.Address) to the MailMessage.To collection.

However, if Orchard.Email is enabled, its EmailMessageEventHandler will ALSO add MessageContext.Address to the MailMessage.To collection.

So if both Orchard.Email and my custom feature are enabled, a recipient address gets added twice to the MailMessage.To collection.

To correct this, the EmailMessageEventHandler should check for the MessagePrepared flag of the context, and exit early if set to true.

This will have ramifications though: the MailActions.cs file has a MailActionsHandler class that implements IMessageEventHandler, but it relies on EmailMessageEventHandler to actually add MessageContext.Address to the MailMessage.To list. Since this handler sets the MessagePrepared to true, after implementing my suggested change, nobody will receive any email (which does not necessarily have to be a bad thing ;)).

To complete the fix, this MailActionsHandler needs to add the addresses itself to the To collection.

I can see how the two classes were designed to work together, but for the scenario just described, it does not work well, so I would like to suggest refactoring this a bit as described.
Closed Sep 22, 2013 at 7:00 AM by sfmskywalker

comments

sfmskywalker wrote Sep 22, 2013 at 6:56 AM

After some investigation, this change turns out to be somewhat tricky, as all other IMessageEventHandlers rely on the one in Orchard.Email to add the recipients to the MailMessage.To collection. The original idea seems to have been that IMessageEventHandler implementations represent "alterations". This means that all implementations will depend on EmailMessageEventHandler. This unfortunately is a flawed design, but will have to do for now until we revise messaging entirely. Closing this ticket.