As the title says. If I have some logic to run that requires information about the current contact / recipient an email is being sent to, e.g. contact facets, what is the best way to get a hold of that contact profile?
EXM utilizes Sitecore's own rendering system to create each message. In doing so, the Dispatch server will make a call to the content delivery role to display the email through an HTTP call. It does so by passing in a handful of arguments, of which include both Message ID and Contact in EXM's
This process then emulates as the user, meaning when Sitecore renders the components on the email, it's doing so, just like if it were drawing to a web page.
Therefore all aspects of the Sitecore Context, as well as Analytics context, exists.
That means we can retrieve the Sitecore Contact by using normal API methods.
Tracking.Current.Contact should reveal the contact that the message is being built for.
From there, you can access the Contact Facets through normal means.
If you're using a shared layout, you can use the following method to determine whether a sublayout is rendering for a normal page request or EXM is requesting a message body, use: