Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an email template within my application [duplicate]

Possible Duplicate:
Razor views as email templates

I am sending out an email to the user from within my service of my website.

I want to format this so that it shows nice and a specific way.

Here is what I have:

<table bgcolor="#FFE680" border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
    <tr>
      <td width="100%" height="51" valign="middle"><h1> <strong>[COMPANY] User ID Reminder </strong></h1></td>
      <td align="right" valign="top" width="8"><img alt="" width="8" height="8" align="top" /></td>
    </tr>

  </tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
  <tbody>
    <tr>
      <td><table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tbody>
          <tr>
            <td align="left" valign="top" width="100%"><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tbody>
                <tr>
                  <td><table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tbody>
                      <tr>
                        <td colspan="1" align="left"></td>
                        </tr>
                      <tr>
                        <td align="left" valign="top" width="100%"><p> <br />
                          Dear [USERNAME], </p>
                          <p> In response to your request to be reminded of your User ID, please   find below the information we have on file for you. If you didn't   submit  this request, ignore this email. </p>
                          <table border="0" cellpadding="0" cellspacing="0">
                            <tbody>
                                  <tr>
                                    <td>Your User ID is: </td>
                                    <td>[USERNAME]</td>
                                    </tr>
                                  <tr>
                                    <td>Your registered email address is:  </td>
                                    <td>[EMAIL]</td>
                                  </tr>
                              </tbody>
                            </table>
                          <p> 
                            If you have forgotten your password, you can request it here.<br />
                          </p>
                          <p> 
                            Thank you,<br />[COMPANY]
                          </p></td>
                        </tr>
                      </tbody>
                    </table></td>
                  </tr>
                </tbody>
              </table></td>
          </tr>
          </tbody>
      </table></td>
    </tr>
  </tbody>
</table>

Now my question isn't about the looks of the email but how do I put this as the body of my email while populating the appropriate fields?

I put it in resources as a string and I figured I'd just do a replace of the specific [fields]. This starting becoming tedious and seemed sloppy:

var body = SuburbanHUB.Properties.Resources.ForgotPasswordEmailBody.Replace("[USERNAME]", username).Replace("[EMAIL]")...

I'm sure there is a better way of doing this but I have not the experience.

=== CLARIFICATION ===

I am using razor to call a WCF service written in C#. The service that is being called is where the email is being sent from and not the view. I am using Razor with MVC with C# as the underlying code.

like image 802
ErocM Avatar asked Dec 30 '25 22:12

ErocM


2 Answers

I'm in the middle of trying to do the same thing. We're using Razor templates to generate the email, and passing in a Model which has all the various variables to fill it out. This lets us include everything that Razor and MVC support, including @if and @Html.Partial, which lets us construct an email from pieces.

The answer we just went with involves running an internal MVC webserver, requesting pages from it with the Model as a parameter, and capturing the response text, but there's variants on my question that are more self contained. Take a look and see if any of the other answers or comments help you.

like image 50
Bobson Avatar answered Jan 01 '26 11:01

Bobson


I had similar requirement and I end up using NVelocity and it worked great for me. Please see this article for a workthrough http://www.codeproject.com/Articles/12751/Template-merging-with-NVelocity-and-ASP-NET. You have to modify your fields in the template according to the velocity templating language. Download NVelocity from http://nvelocity.sourceforge.net/.

like image 40
Tariqulazam Avatar answered Jan 01 '26 11:01

Tariqulazam