Email template engine

Newest version of Mail.dll email client includes a easy to use template engine.

It allows you to easily create html template for your emails:

Loading and rendering such template requires one line of code:

Contact contact = ...;

 string html = Template
     .FromFile("template.txt")
     .DataFrom(contact)
     .Render();

This is how the template looks like:

<html>
<head>
    <title>Your order</title>
</head>
<body>
	Hi [FirstName] [LastName],
	<br />
	<p>
	Your account [if Verified]has[else]<strong>has not</strong>[end] been verified. <br/>
	Your password is: [Password].
	</p>
	<p>
	Here are your orders:
	</p>
	[foreach Orders]
		<p>
		Order sent to <strong>[Street]</strong>:
		</p>
		<table style="width: 30%;">
			[foreach Items]
				<tr style="background-color: #E0ECFF;">
					<td>[Name]</td><td>[Price]</td>
				</tr>
			[end]
		</table>
	[end]
	<p>
		Thank you for your orders.
	<p>
</body>
</html>

Here’s the sample of how to load, fill the template and send it using Mail.dll:

Mail.Html(Template
              .FromFile("template.txt")
              .DataFrom(_contact)
              .Render())
    .Text("This is text version of the message.")
    .From(new MailBox("alice@mail.com", "Alice"))
    .To(new MailBox("bob@mail.com", "Bob"))
    .Subject("Your order")
    .UsingNewSmtp()
    .WithCredentials("alice@mail.com", "password")
    .Server("mail.com")
    .WithSSL()
    .Send();

And this is how the data used by template look like:

public class Contact
{
    public List<Order> Orders { get; private set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public bool Verified;
    private string Password { get; set; }

    public Contact()
    {
        Orders = new List<Order>();
    }
} ;

Tags: ,

One Response to “Email template engine”

  1. Uroš Says:

    This is a very usefull thing and easy to implement thanks to your tutorial! Great job.

Leave a Reply