凡来存储电子邮件模板 [英] Where to store email templates

查看:375
本文介绍了凡来存储电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net web应用程序,它发送几封电子邮件给用户的注册过程中。现在我有他们的内联与code,但我想追究他们在中央位置,我可以对其进行编辑,而无需到VS。

I have an asp.net web application, which sends several emails to users during the signup procedure. Right now I have them inline with the code, but I would like to hold them in a central location where I can edit them without going in to VS.

什么是最好的地方/格式来存储这些HTML模板?

What would be the best place/format to store these HTML templates?

推荐答案

我存储我的所有电子邮件模板我的web应用程序的 ASP.NET MVC剃刀浏览,但作为在光组件,我可以轻松地从任何项目中引用嵌入的资源

I store all my e-mail templates for my web-app as ASP.NET MVC Razor Views, but as embedded resource in a light assembly that I can easily reference from any project.

一个模板看起来像这样(注意本地化):

A template looks like this (notice the localization):

@model Milkshake.Commerce.Model.Users.UserDto
@using Milkshake.Core.Internationalization;
@using Milkshake.Commerce.Model.Meta;

@if (Language.CurrentForInterface.TwoLetterISOLanguageName.Equals("da"))
{

<h1>Hej @Model.FirstName</h1>

<p>
    Din nye brugerkonto til Milkshake Commerce er blevet oprettet.
</p>

<p>
    Gå til dine <a href="http://@ShopSettings.Instance.Domain.TrimEnd('/')/Account">konto indstillinger</a>, brug din e-mail adresse som adgangskode og du vil blive videreført til dine konto indstillinger, hvor du kan ændre din adgangskode.
</p>

<p>Ha' en god dag!</p>
<h2>The Milkshake Commerce Team!</h2>

}
else
{

<h1>Hi @Model.FirstName</h1>

<p>
    Your new user account for Milkshake Commerce has been created for you.
</p>

<p>
    Go to your <a href="http://@ShopSettings.Instance.Domain.TrimEnd('/')/Account">user account page</a>, use your e-mail address as password and you'll be taken directly to your account page where you can change your password.
</p>

<p>Have a nice day!</p>
<h2>The Milkshake Commerce Team!</h2>

}

然后,我有一个大师的模板,名为 _AppEmailTemplate.cshtml

@using Milkshake.Commerce.Model.Resources

<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>

        <style type="text/css">
            body
            {
                font-family: Arial, Helvetica;
            }
            .layout-wrapper
            {
                width: 600px;
            }
            .header
            {
                background-color: #242225;
            }
            .header img
            {
                display: block;
            }
            .content
            {
                background-color: #ffffff; padding: 10px 20px; border: 10px solid #eaeaea; border-top: none;
            }
            .footer
            {
                padding: 20px; padding-top: 5px; font-size: 10px; color: #cccccc;
            }
            p
            {
                font-size: 14px;
            }
            p.company-details
            {
                font-size: 12px;
            }
            h1
            {
                font-size: 20px;
            }
            h2
            {
                font-size: 16px;
            }
        </style>
        <style type="text/css" id="mobile">
            @@media only screen and (max-device-width: 480px) {
                body
                {
                }
                .layout-wrapper
                {
                    width: 480px !important;
                }
                .header
                {
                    background-color: transparent !important;
                }
                .header img
                {
                    width: 480px !important;
                }
                .content
                {
                    border: none !important;
                }
                .footer
                {
                    padding-top: 15px !important;
                }
                p
                {
                    font-size: 22px !important;
                }
                h1
                {
                    font-size: 28px !important;
                }
                h2
                {
                    font-size: 24px !important;
                }
            }
        </style>
    </head>

    <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" bgcolor="#f1f1f1">

        <table width="100%" cellpadding="0" cellspacing="0" bgcolor="#f1f1f1">
            <tr>
                <td valign="top" align="center">

                    <table cellpadding="0" cellspacing="0" width="100%" height="80">
                        <tr>
                            <td class="header" align="center">
                                <table cellpadding="0" cellspacing="0" width="600" height="80" class="layout-wrapper" style="width: 600px;">
                                    <tr>
                                        <td>
                                            <img src="http://example.com/email-header.png" alt="Milkshake Commerce" />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>

                    <table cellpadding="0" cellspacing="0" width="600" class="layout-wrapper">
                        <tr>
                            <td class="content" align="left">
                                #¤#¤CONTENTSECTION#¤#¤
                            </td>
                        </tr>
                        <tr>
                            <td class="footer" align="left">
                                <p>@Text.appEmailDisclaimer</p>
                                <p>@Text.appEmailFooterAd.UrlDecode()</p>
                                <p class="company-details"><i>Company name etc.</i></p>
                            </td>
                        </tr>
                    </table>

                </td>
            </tr>
        </table>

    </body>

</html>

要实际发送的电子邮件,我使用 RazorEngine 渲染:

To actually send the e-mail, I use RazorEngine for rendering:

public void SendSystemEmail<T>(string templateName, string subject, string fromName, string recipientEmail, T model)
{
    dynamic template = this.GetEmailTemplate(templateName);
    string layoutBody = RazorEngine.Razor.Parse(template.Layout as string, model);
    string emailBody = RazorEngine.Razor.Parse(template.Template as string, model);

    emailBody = layoutBody.Replace(CONTENTSECTIONREPLACETOKEN, emailBody);

    PreMailer.Net.PreMailer pm = new PreMailer.Net.PreMailer();
    emailBody = pm.MoveCssInline(emailBody, true);

    EmailDto email = new EmailDto();
    email.Body = emailBody;
    email.IsBodyHtml = true;
    email.FromEmail = "support@example.com";
    email.ReplyToEmail = email.FromEmail;
    email.FromName = fromName;
    email.RecipientEmail = recipientEmail;
    email.Subject = subject;
    email.Type = EmailTypes.Transactional;

    if (String.IsNullOrWhiteSpace(email.FromName))
    {
        email.FromName = "Milkshake Software";
    }

    this.SendMailMessages(new List<EmailDto>() { email }, false);
}

以上code使用我自己的EmailDto对象。在这里,您可以轻松地创建一个 [MailMessage] [2] 实例直接,并用其发送的 [SmtpClient] [3]

The above code uses my own EmailDto object. Here you can easily create a [MailMessage][2] instance directly, and send it using the [SmtpClient][3].

此外,为了让所有的电子邮件客户端最好的渲染,我用我自己的 preMailer.Net 库将所有的CSS内联。阅读我的博客文章,以获取更多信息。 (code是Github上)

Also, to get the best rendering in all e-mail clients, I use my own PreMailer.Net library to move all CSS inline. Read my blog post here, for more information. (Code is on Github)

在GetEmailTemplate做到这一点:

The GetEmailTemplate does this:

/// <summary>
/// Gets the email template.
/// </summary>
/// <param name="templateName">Name of the template.</param>
/// <returns>Returns the e-mail template.</returns>
private dynamic GetEmailTemplate(string templateName)
{
    string masterTemplateContents = this.GetTemplateFileContents("_AppEmailTemplate.cshtml");
    string templateContents = this.GetTemplateFileContents(templateName + ".html.cshtml");

    return new { Layout = masterTemplateContents, Template = templateContents };
}

/// <summary>
/// Gets the template file contents.
/// </summary>
/// <param name="templateFileName">The name of the template file.</param>
/// <returns>Returns the contents of the template file.</returns>
private string GetTemplateFileContents(string templateFileName)
{
    return this.GetEmailFileContents("Templates", templateFileName);
}

/// <summary>
/// Gets the email file contents.
/// </summary>
/// <param name="lastNamespaceToken">The last namespace token.</param>
/// <param name="templateFileName">The name of the template file.</param>
/// <returns>
/// Returns the contents of the template file.
/// </returns>
private string GetEmailFileContents(string lastNamespaceToken, string templateFileName)
{
    var assembly = Assembly.GetExecutingAssembly();

    if (assembly != null)
    {
        StringBuilder sb = new StringBuilder();

        using (StreamReader sr = new StreamReader(assembly.GetManifestResourceStream(String.Format("MyApp.BusinessLogic.Communication.{0}.{1}", lastNamespaceToken, templateFileName))))
        {
            while (!sr.EndOfStream)
            {
                var line = sr.ReadLine();

                if (!line.StartsWith("@model"))
                {
                    sb.AppendLine(line);
                }
            }
        }

        return sb.ToString();
    }

    return null;
}

这篇关于凡来存储电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆