views:

69

answers:

5

I have an application allows a user to copy paste html into a form. This html gets sent as an email, and the email server will not allow more than 1000 characters per line. So, I'd like to insert line breaks (\r\n) into the html after the user has hit submit. How can I do this without changing the content?

My idea is this:

html.replace('<', '\r\n<');

But is that guaranteed to not change the result? Is '<' not allowed in attributes?

Edit: I'm actually thinking this will not work because the html could have a script block with something like if(x < 3). I guess what I need is an html pretty printer that works in either js or C#.

A: 

I am not sure how you are delivering your email... if it is handed off to a php script that then send it to a mail server or uses the mail() method, then this link might help.

http://php.net/manual/en/function.wordwrap.php

If not, can you clarify your question a bit?

Another simply thought, is that you could use: html.replace('','\r\n'); or: html.replace('',''+String.fromCharCode(13));//inserts a carriage return

However, since the will ideally be parsed in the browser, inserting "\r\n" may not be effective and may actually just display as "\r\n"....

Hope any of this is helpful.

exoboy
+1  A: 

Email MIME standard uses transfer encoding techniques to solve this problem. Ideally you would be using a mail library that takes care of this for you, so you can insert lines of any length.

Using the System.Net.Mail.MailMessage class in C#, you should be able to construct a normal message and it will transfer-encode it for you. If that doesn't work, you can also construct a multi-part message with a single System.Net.Mail.AlternativeView and set the transfer-encoding explicitly.

Here is a sample I am currently using (note it has a character encoding bug, so your body text must be a unicode string):

private void Send(string body, bool isHtml, string subject, string recipientAddress, string recipientName, string fromAddress)
{
    using (var message = new MailMessage(new MailAddress(fromAddress),
                                    new MailAddress(recipientAddress, recipientName)))
    {
        message.Subject = subject;
        var alternateView = AlternateView.CreateAlternateViewFromString(body, message.BodyEncoding,
                                                                        isHtml ? "text/html" : "text/plain");
        alternateView.TransferEncoding = TransferEncoding.QuotedPrintable;
        message.AlternateViews.Add(alternateView);

        var client = new SmtpClient();

        client.Send(message);
    }
}
roryf
I wish we could do that. Unfortunately our software is deployed onto a client's server and hooks into their email server, which we have no control over.
Mike Blandford
Wait, instead of sending this message, could I return it as a string?
Mike Blandford
You can configure SMTP settings in the web.config file to point to any mail server, with credentials.
roryf
+1  A: 

If you Base64 encode the content, then you can break up the content into however many lines you want.

Collin
So, the idea is to send an html email that they just open. If I base64 encode it, is that still going to work, or is the content going to be an attachment? Do all email servers automatically know to base64 encode/decode?
Mike Blandford
I was assuming that you were sending the form submission back to the server which had the 1000 character per line restriction. Sorry about the misunderstanding. If you are still sending the form submission back to the server, you can parse the markup and then pretty print it. If you're sending directly from the form (can you do this?) then perhaps you could parse the markup with jQuery and reformat it -- I'm not sure if that's possible, though.
Collin
A: 

You're getting into dangerous territory attempting to parse HTML with a replace function. The easiest method would be to just display a warning box on the form that tells the user that lines cannot be longer than 1000 characters, and return an error message if they attempt to submit content with lines over that length.

Otherwise, you could insert a linebreak after X number of characters, and insert some special markup (like <!--AUTO-LINEBREAK-->, or similar) that informs whoever is receiving the e-mail that an automatic line break was inserted.

Brad
Haha I thought I saw that answer before about parsing html. Unfortunately, we have to do some post processing on the user's entered HTML that inserts the html into a div's innerHTML. The innerHTML likes to format the html with its own line break settings.
Mike Blandford
+1  A: 

Hi,
Add normal line breaks where you think they should be. For example:

Off the top of my head, find all <p>, <table>, <tr>,<td>,<br>, and <div> tags and add a \r\n right before them.

Once that is done, loop through all the lines one more time. If there are any that are still 1000+ characters long, I would insert a \r\n in the whitespace.

Aslo, you should be removing any script tags from the HTML email body. Having script tags can cause all types of problems (marked as spam, marked as a virus, blocked, etc..).

hth,
Dave

dave wanta