Daddio has asked for the wisdom of the Perl Monks concerning the following question:

I've been working on a relatively simple CGI to accept text in two textareas (one for plain-text and one for HTML). The basic use of this is, as you can probably guess, to format a multi-part email message. I have most of this working, but a "late requirement" was that the text in both sections wrap at ~72 characters.

So, I employed Text::Correct's wrap function to handle the wrapping, since the documentation sounded like a good match for what I wanted.

I can get the code to take the input and it seems to wrap it just fine, but when I look at the output file or the source in the browser, I see strange things.

This is the output as it is read directly from the CGI. Note that it is two long lines, with a blank line between (three lines total).

# Plain text output: This is some text to test the wrapping feature of Text::Correct's wra +p(), using MIME::Lite to create the message. I really just want to have two lines of data to test the functions and + want them both to be over the 72 character limit. # HTML output: <p>This is some text to test the wrapping feature of Text::Corr +ect's wrap(), using MIME::Lite to create the message.</p> <p>I really just want to have two lines of data to test the func +tions and want them both to be over the 72 character limit.</p>

Then there is the post-Text::Correct::wrap() output:

# Plain text output: This is some text to test the wrapping feature of Text::Correct's wrap(), using MIME::Lite to create the message. I really just want to have two lines of data to test the functions and want them both to be over the 72 character limit. # HTML output: <p>This is some text to test the wrapping feature of Text::Cor +rect's wrap(), using MIME::Lite to create the message.</p> <p>I really just want to have two lines of data to test the functions and want them bot +h to be over the 72 character limit.</p>

My problem is that some of the lines wrap properly, and some don't. In the plain text output, there is a short-wrapped line ("I really just want to"), and the HTML output has one, too ("<p>I really just").

When this gets MIME'd up, it gets even worse:

Content-Type: text/plain This is some text to test the wrapping feature of Text::Correct's I really just want toite to create the message. have two lines of data to test the functions and want them both to be over the 72 character limit. ------ Content-Type: text/html &lt;p&gt;This is some text to test the wrapping feature of Text::Corr +ect's &lt;p&gt;I really justME::Lite to create the message.&lt;/p&gt; want to have two lines of data to test the functions and want them bot +h to be over the 72 character limit.&lt;/p&gt;

I'm at a loss. I have tried everything I can think of to get this to work, but I am very new (this is the second day now :) to both of these modules, so I could be missing something. Here is most the code (didn't put in the non-related stuff), complete with all the prints (STDOUT and file). I use strict and -w, but am not even getting an error in the logfile.

#!/usr/local/bin/perl -w use CGI; use strict; my $q = new CGI; my $emailoutput = '/tmp/email.data'; use MIME::Lite; use Text::Correct qw{ wrap }; my $tp = $q->param('textPLAIN')."\n"; my $th = $q->param('textHTML')."\n"; # Section just for debugging (output to files and browser) print $q->header; print "plain pre:<br>\n $tp<hr>\n"; print "html pre:<br>\n $th<hr>\n"; open O,">/tmp/o1"; print O $tp; close O; open O,">/tmp/o2"; print O $th; close O; my $wtp = wrap(' ','',$tp); my $wth = wrap(' ','',$th); # More debugging (output to files and browser) open O,">/tmp/o3"; print O "$wtp"; close O; open O,">/tmp/o4"; print O "$wth"; close O; print "plain post:<br>\n $wtp<hr>\n"; print "html post:<br>\n $wth<hr>\n"; # Start the message my $msg = MIME::Lite->new( From =>$q->param('emailFrom'), To =>$q->param('emailTo'), Subject =>$q->param('emailSubject'), Type =>'multipart/alternative', ); $msg->attach( Type =>'TEXT', Data =>"$wtp\n", ); $msg->attach( Type =>'text/html', Data =>"$wth\n", Encoding =>'7bit', ); # Add character set (required header) $msg->attr("content-type.charset" => "US-ASCII"); # Remove headers (another requirement) $msg->scrub(['content-transfer-encoding','content-disposition']); # Final output of script to file (yet another requirement) open(OUTFILE,">$emailoutput") or die "Unable to open $emailoutput: $!" +; $msg->print(\*OUTFILE); close OUTFILE;

If anyone can suggest a better way to do this, please feel free - it all helps the learning :) Thanks!

D a d d i o

Replies are listed 'Best First'.
Re: Unexpected results using Text::Correct and MIME::Lite
by chipmunk (Parson) on Jul 02, 2001 at 00:00 UTC
    At first I thought this might be a bug in Text::Correct. Then I thought it might be the intended behavior, but you needed to split the text into lines before passing it to wrap.

    Then I actually downloaded Text::Correct, and got the proper behavior in my testing. So, now I have a third theory:

    The text you are wrapping probably has "\r\n" line endings (since it comes from CGI data), which Text::Correct doesn't account for. Try deleting the \r characters from the text before you wrap it:

    $tp =~ tr/\r//d; $th =~ tr/\r//d;
    I hope that helps!

      Thanks, chipmunk. This did the trick. It's kind of funny, but at one point I actually saw the %13%10 (or whatever the encoding is) on the end of the lines, and thought nothing of it. I added your tr/\r//d to the code, and it works like a champ! Thanks again!

      D a d d i o

Re: Unexpected results using Text::Correct and MIME::Lite
by bikeNomad (Priest) on Jul 01, 2001 at 23:36 UTC
    You might want to give Text::Autoformat a try. Note that by default it only formats the first paragraph (read the manpage).

      bikeNomad, thanks for that suggestion. I am still investigating CPAN, and hadn't run in to Text::Autoformat yet, but I will definitely be looking that one over...seems pretty powerful! For this project, it might be a little overkill, but I do have another that this would fit into rather nicely. Thanks again!

      D a d d i o