views:

640

answers:

3

I'm using a preformatted text file as a template for emails. The file has line breaks where I want them. I'd like to use this template to send a plain text email, but when I do I'm losing all formatting. Line breaks are stripped.

How do I parse this file and retain line breaks? I don't want to use a <pre> tag because I want to send plain text emails.

I'm using the classic ASP ReadAll method to pull the template into a string:

      Dim TextStream
  Set TextStream = FSO.OpenTextFile(Filepath, ForReading, False, TristateUseDefault)

  ' Read file in one hit
  Dim Contents
  GetTemplate = TextStream.ReadAll ' return file contents

What am I missing?

+1  A: 

this is what I have done...

I take a text, or HTML file, ( I'll show text, since its smaller, but the exact same code applies ), and I put well know values into the text file that I can later replace.

-- Begin Text File

We've generated a new password for you at your request, you can use this new password with your username to log in to various sections of our site.

Username: ##UserName##
Temporary Password: ##Password##

To use this temporary password, please copy and paste it into the password box.

Please keep this email for your records.

-- End Text File

Then its a simple matter of creating a list of key/value pairs, with the text to be replaced, and the value your replacing it with. Load the file into memory as a string, and loop through your key/value pair replacing your text values.

ListDictionary dictionary = new ListDictionary
                                            {
                                                {"##UserName##", user.BaseUser.UserName},
                                                {"##Password##", newPassword}
                                            };


            string fromResources = GetFromResources("forgotpasswordEmail.html");
            string textfromResources = GetFromResources("forgotpasswordEmail.txt");
            foreach (DictionaryEntry entry in dictionary)
            {
                fromResources = fromResources.Replace(entry.Key.ToString(), entry.Value.ToString());
                textfromResources = textfromResources.Replace(entry.Key.ToString(), entry.Value.ToString());
            }

Then you can email the text, ( in this case the textfromResources variable ), and it will contain all of the necessary line breaks and formatting.

Like I said, you can do this eact same thing with HTML files, or any type of file you want to.

Although my example is in C#, ( I don't have any classic ASP code handy, sorry ), the concept of finding and replacing values will apply to classic ASP.

Russ
You described exactly how I have it setup now. Vote up for the thorough explanation though!
Cory House
+1  A: 

The code you show shouldn't strip any line breaks. The problem is probably on the email generation part. Could you show that part?

Is the mail's Content-Type: text/plain?

angus
Woo hoo! Right on the money! You made my day. I was convinced I was missing something in the way i was reading the file. But yup, I was accidentally sending the email in HTML format! Thanks again!
Cory House
A: 

I would recommend using http://www.webdevbros.net/2007/06/28/template-component-for-classic-asp/

Michal