MVC - How to load a template html page, modify it and send it by email

Asked

Viewed 775 times

0

I am making a form that sends an email to the customer, this email comes from a template depending on the client’s stage. Ex: Estágio 01 envio o e-mail com a body vindo do modelo_01.html, se o cliente está no estágio 04, envio o e-mail com o modelo_04.html

I’m doing a format that works, but as MVC is new to me, sometimes I think I’m doing it in hardcore form. Sometimes there’s a simpler way and I don’t know.

Like I’m doing: Inside the view in question I create a template file_01.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
    <body>
        <p>Prezado Cliente [Nome_Cliente],</p>
        <p>Estamos lhe enviando o andamento do seu site conosco.</p>
        <p>Novo status atualizado em:</p>
        <p>   [Mostra_EP] </p>
        <p>[data]<p>
    </body>
</html>

In the controller I called a library that I created that sends the email. First I read by Filesystem

var conteudo = System.IO.File.ReadAllText(Caminho);

Then replace the [Client Name],[Show_ep],[date]

conteudo = conteudo.Replace("[Nome_Cliente]", cliente.Nome);
conteudo = couteudo.Replace("[data],DateTime.Now);

In [Mostra_ep] it is a little more complex because I do a query in the database and a foreach and I’m assembling an html. After that Sending this content on:

 objEmail.Body = conteudo;

Is there anything simpler? type a Partial that I call her she would already return me an html already ready (she executes and returns an html) because the part of [Mostra_ep] is getting giant and I haven’t even finished yet.

  • 1

    Search by "Asp.net template engine".

1 answer

2


Your reasoning is correct, you’re doing too hardcore.

If you are using Asp.net MVC (I assume you are also using Razor Views), you can use the Razor feature to render this html for you.

.cshtml file:

@model SuaModelClasse
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <p>Prezado Cliente @model.NomeCliente,</p>
    <p>Estamos lhe enviando o andamento do seu site conosco.</p>
    <p>Novo status atualizado em:</p>
    <table>
        <tr>
            <th>Col1</th>
            <th>Col2</th>
        </tr>
        @foreach (var item in Model.Itens)
        {
            <tr>
                <td>@item.Col1</td>
                <td>@item.Col2</td>
            </tr>
        }
    </table>
    <p>@Model.Data<p>
</body>
</html>

This is an example. You can use all the features of Razor Views. Then render your view into a string and place the string in the email body.

This can be done by following what has been answered in this question.

Below the code taken from the above mentioned answer:

public string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;

    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines
            .FindPartialView(ControllerContext, viewName);

        var viewContext = new ViewContext(
            ControllerContext, viewResult.View, ViewData, TempData, sw);

        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.