How to use email txt file and keep formatting?

I am using a preformatted text file as a template for emails. There are line breaks in the file where I want them. I would like to use this template to send a simple text message, but when I do, I lose all formatting. Line breaks are separated.

How do I parse this file and keep the line breaks? I don't want to use a tag <pre>

because I want to send text emails.

I am 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?

0


source to share


3 answers


The code you are showing should not block line breaks. The problem is probably related to the email generation part. Could you show this part?



Is the message Content-Type: text / plain?

+1


source


here's what i did ...

I take a text or HTML file (I will show the text since its smaller, but the same code applies) and I put the well-known values ​​into a text file that I can replace later.

- Start 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 of text file



Then it's a simple question to create a list of key / value pairs with the text to replace and the value you replaced. 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 variable textfromResources) and it will contain all the line breaks and formatting you need.

As I said, you can do this in the same way as HTML files, or any type of file you want.

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

+3


source


0


source







All Articles