How do I encode placeholders in emails?

The requirement is that in the application in which I work, I make all available letters editable.

The obvious solution is to save all messages in the database and use a text editor to edit messages.

However, I am wondering how to do this when it comes to placeholders in an email message.

Placeholders for the message must also be stored in the database. A message can contain many placeholders. Placeholder can be in many posts.

I could let the user select a placeholder from the list of placeholders when they insert it.

The e-mail message will need to specify placeholders, usually {0}, {1}, etc., but for display purposes this should be more user-friendly, such as $ WebsiteLink $. Some placeholders are straightforward, for example, a link to a site that changes only depending on the deployment, or the name of a registered user.

Other placeholders are more complex. They depend on receiving information from the database at a specific time, for example, on behalf of a selected group member. So for this placeholder, instead of storing some static text, I probably need to call a method or event to get this information.

I don’t understand how to do this. You can help?

I hope the specification is clear, let me know if not?

+3
source share
4

String.Format ( ) .

, , {name} {date} {0}.

, , , .

+3

, ( this, , ), :

1. HTML-, mailTemplate.html:

(, ...)

<table>
    <tr>
        <td> 
            <%MY_PLACEHOLDER%>
        </td>
    </tr>
</table>

2. , , templatePath - :

var mailDefinition = new MailDefinition
                         {
                              BodyFileName = templatePath,
                              IsBodyHtml = true,
                              From = ... // you'll have to fill that
                         };
string replacementForPlaceholder = "Look at me!";
var replacements = new ListDictionary { { "<%MY_PLACEHOLDER%>", replacementForPlaceholder }};

string recipients = ... // well, specify recipients
var message = mailDefinition.CreateMailMessage(recipients, replacements, new LiteralControl());

. HTML -, , HTML.

+1

Last month, I implemented the template-based EmailModule below, which uses Razor syntax. It should work well in your case. It is easy to implement.

http://kazimanzurrashid.com/posts/use-razor-for-email-template-outside-asp-dot-net-mvc

Thanks to Kazi Manzur Rashid.

0
source

C # now supports interpolation. For those who are still worried about this, take a look here .

0
source

All Articles