How to generate dynamic email signature when sending in D365 Marketing
Published: 2022-11-09 01:16:36

_When sending emails in D365 Marketing app, it is always a good practice to include some personalized content in the email for a special and lively touch._

Recently, a lot of clients have the requirement to take the personalization further and include an email signature of the salesperson (or marketing person). In my opinion, it is a wise move because a email signature can make the communication feel less "automated". However, do make sure that "reply-to" address points to an accessible email box in case a recipient replies to the email directly since you don't want to miss out on a customer's inquiry.

The logic of this implementation is quite simple: when sending an email to a contact during a customer journey, append the email signature of the owning user for the contact record at the end of email body. If you are no stranger to **[Dynamic Content](https://learn.microsoft.com/en-us/dynamics365/marketing/dynamic-email-content)**, you might already have an idea of the implementation details. However, I do want to bring up one caveat of dynamic content before discussing the details of my solution.

---

According to Microsoft Docs - 

> Dynamic content gets resolved just before a message is sent to a specific individual. You'll typically use dynamic content to merge information from the recipient's contact record (such as first and last name), to place special links, and to place information and links from the content settings.

The caveat is that although you can populate dynamic content using information of records related to the recipient's contact record, you cannot traverse more than one relationship down the path from the contact table. For example, you can use a contact's parent account name to seed dynamic content because account table is **directly** related to contact table in D365 (only one relationship away). On the flip side, it is not feasible to use the parent account's territory information in dynamic content since territory table is only directly related to account table (two relationships away from contact table). Even if you use the dynamic content syntax like "contact.contact_account_parentcustomerid.territoryid.name", it will not resolve to the name of territory in the actual email sent. This is also specified in a Microsoft documentation:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chkgl1vxxybhny2rmp1z.png)
<figcaption>Since my example has 3 hops (periods) in the expression, it will not resolve according to Microsoft.</figcaption>

---

What does it imply? You might be tempted to use the out-of-box table **Email Signature** (if you are not familiar with Email Signature, check out this [amazing article](https://centricconsulting.com/blog/how-to-create-an-email-signature-in-dynamics-365-customer-engagement/) by Allen Paige) to populate dynamic content for an email but it is not a viable solution since the email signature table is not directly related to the contact table. You could customize the table to add a direct one-to-many relationship to the contact table but this is very wrong, **semantically**. How is an email signature of a system user directly related to a contact?

To work around this impedement, we could use the first name and last name fields in combination as dynamic content but this approach falls short if you want to include additional information ,format the email signature or attach written signature pictures.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kqdex47nsxruw1j6w8p5.png)
<figcaption>Signature with formatted text and picture</figcaption>

---

Are we stranded here? Rich text field to the rescue! When creating a new column to store text in Dataverse tables, we are given the format option of "Rich Text".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gwba1f68pv33vl82b81.png)
<figcaption>Column creation with rich text format</figcaption>

---

When "Rich text" format is chosen together with data type of "Multiple lines of text", the data stored in this field will be plain HTML and the field can be edited with rich text control when using placed on the system user table form.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8fuleu6m48l4fs9rxuf.png)
<figcaption>Rich text editor on a form of model-driven app</figcaption>

---

When adding images using the rich text control, make sure to use the "**Web Address**" option instead of "**From File**".


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kejwmgahgw1y1hogdokj.png)
<figcaption>Attach images with web address only</figcaption>

---

**_Since image uploaded to D365 will only be available in the D365 environment but not be publically accessible on the internet, resulting in broken images when recipients open the email._**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5p8lndnmya43pqx1c1nd.png)
<figcaption>Broken image since image can only be accessed within D365</figcaption>

---

Once the record is saved, the signature is stored as plain HTML which can be injected into marketing emails through dynamic content using this syntax: "contact.owninguser.hsi_emailsignature". Since emails are built with HTML behind the scene, the signature's HTML will be injected as a part of the whole HTML body, which will be rendered by email engines as user interface.

Let's perform a test send and see the final result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0w0ql07hajwdjt9r0e9h.png)
<figcaption>I used a relatively large dimension for image here so the signature might not look appealing</figcaption>

---

Now you have learnt how to add some advanced personalization to your marketing emails, congratulations!