cofoundry-cms / cofoundry

Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
https://www.cofoundry.org
MIT License
822 stars 144 forks source link

Sending mail with a different reply-to email address #323

Open HeyJoel opened 5 years ago

HeyJoel commented 5 years ago

Hello, I would like to send a mail with a different "reply-to" email. Is it possible ? Thanks

Originally posted by @dumboster in https://github.com/cofoundry-cms/cofoundry/issues/216#issuecomment-496169044

HeyJoel commented 5 years ago

@dumboster there's no "reply-to" field in our mail abstraction, but you can change the "from" address.

Changing the from address

The from address can be changed either

  1. Globally with the Cofoundry:Mail:DefaultFromAddress config setting
  2. Per template by inheriting from IMailTemplateWithCustomFromAddress instead of IMailTemplate

Implementing "reply-to"

It's unlikely we'd add "reply-to" functionality into the mail abstraction as the field is quite specific to the mail implementation you're using. What we could do is allow hooks to format messages for each mail plugin, in a similar way to what we do with configuring the SMTP connection in the MailKit plugin with ISmtpClientConnectionConfiguration.

Work-around

For now the work-around it's probably easiest to copy the code of your chosen mail plugin to your local project and modify the code to work in the way you want it, e.g. for the MailKit plugin you'll need to alter the FormatMessage property of MilKitMailDispatchSession to add your custom ReplyTo value.

If you prefer you can instead just create your own IMailDispatchSession and IMailDispatchService implementation and override the existing implementing using the DI System, but the mail plugin implementations are so lightweight that it's probably simpler for you just to copy the project code over. If you do go down the override route though just beware that you'll need to setup your RegistrationOptions with RegistrationOverridePriority.High in this case because you'll be overriding plugin code which is already overriding the default Cofoundry implementation.

dumboster commented 5 years ago

Thanks @HeyJoel. I finally overrided IMailDispatchSession, IMailDispatchService and IMailMessageRenderer and added a custom MalMessage with a new "ReplyTo" field, as I needed a different reply-to address for each mail.

HeyJoel commented 5 years ago

Cool, I'll bear that in mind when we come to implement this feature to see if we can allow more flexibility here, but there comes a point at which it's easier to do as you have done and override with custom logic yourself rather than try and cater to all needs in a generic API.

Thanks