Skip to content

Instantly share code, notes, and snippets.

@maldworth
Created July 23, 2018 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maldworth/14c8850cbd5b24881fd7a97abb88a14d to your computer and use it in GitHub Desktop.
Save maldworth/14c8850cbd5b24881fd7a97abb88a14d to your computer and use it in GitHub Desktop.
Gist because SO Rep system doesn't like me...
@maldworth
Copy link
Author

Just a bit of advice with the Email Service. We have a generic Emailer Consumer. This only accepts a Command. So we made a contract similar to:

public interface SendEmail
{
    IList<IEmailAddress> To { get; }
    IList<IEmailAddress> Cc { get; }
    IList<IEmailAddress> Bcc { get; }
    IEmailAddress From { get; }
    IEmailAddress ReplyTo { get; }
    string Subject { get; }

    // Can have multipary email bodies, hence the list.
    IList<IContent> Body { get; } // IContent is two properties {type: "", value: ""}
}
public interface IEmailAddress
{
    string Email { get; }
    string Name { get; } // display name
}
public interface IContent
{
    string Type { get; } // mimetype eg. text/plain, or text/html
    string Value { get; } // actual text, or html
}

so now, my application might publish events when things happen (notice they are past tense):
UserCreated
OrderShipped

And so I may have multiple Receive Endpoints, which will subscribe to those events

UserCreatedEmailConsumer
OrderShippedEmailConsumer

And each of those consumers will query the database, and get the e-mail Template, all the information related to those events, and populate the: to, from, subject, substitute into the email template from the db, then it will issue a _sendEndpoint.Send(...). This will send a command to the SendEmailConsumer which will perform the actual send using your e-mail provider (whether it is your own in house SMTP, or a 3rd party like Sendgrid, MailGun, Sparkpost, etc...).

This is also a nice structure, because you have the ability to configure within your SendEmailConsumer, First level and Second Level retries (based on transient failures=network blip, or an outage window=sendgrid id doing a 3 hour maintenance).

Additionally, keeping the SendEmail contract generic, you can switch mail provider anytime in the future by only changing the SendEmailConsumer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment