[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Notice: in file [ROOT]/includes/session.php on line 2208: Array to string conversion
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4688: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4690: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4691: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4692: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3823)
Poco Forums • View topic - A script to wrap long lines in emails

A script to wrap long lines in emails

Scripting questions and ideas

Moderators: Eric, Tomas, robin, Michael

A script to wrap long lines in emails

Postby Hogyt » Tue Aug 31, 2004 10:36 am

This script is an update to Split.poc and will wrap long lines so they are not longer than a certain length.

Code: Select all
{ Wrap lines.poc v1.02 by Hogyt
{ Purpose:   Wraps lines longer than a given length (set in "Setup Script") in the raw body of the selected email
{            Saves a copy and leaves original email intact
{ Updates:   http://www.pocosystems.com/forum/viewtopic.php?p=2882#2882
{ Changelog: v1.01 - Tidied up script, added error handling
{                  - Max length allowed in a line is set in Scripts->Setup Script
{            v1.02 - Works better with html email

Set $OnErrorGoTo "ErrorHandler"

External $userdata1 "Max length allowed" 70

Set #length $userdata1

ReadHeader $contenttype "Content-Type:" %message
LowerCase $contenttype
StringPos #htmlpos "html" $contenttype
Set &nonhtml #htmlpos = 0

{ Check #length is valid
If #length > 0 Then Continue
Messagebox "#length must be a number greater than 0\nSet it in Scripts->Script Setup"
Exit

:Continue
  Inc #length
  ReadRawBody $body %message
  LineCount #maxpos $body
  Set &nochange True
  Set #pos 0
  Set $newbody ""

{ Loop through raw body a line at a time
:Loop
  GetLine $line #pos $body

{ Repeatedly split the line until it isn't longer than #length
:Split
  CharCount #len $line
  If #len < #length Then Skip
  Set $copy $line

{ Delete from #length chars onwards
  ChopString $copy #length #len
  If &nonhtml Then NoTag2
  StringPos #opentagpos "<" $copy
  If #opentagpos = 0 Then NoTag

  CharCount #thislen $copy
:BackLoop
  Char $thischar #thislen $copy
  If $thischar = ">" Then NoTag
  If $thischar = "<" Then NoClosing
  Dec #thislen
  If #thislen > #opentagpos Then BackLoop

:NoClosing
  If #thislen < 2 Then NoTag
  Set $newcopy $copy
  ChopString $copy #thislen #len

:NoTag
  SubStrings $line $copy
  If &nonhtml Then Append
  AddStrings $copy "<br>"

:Append
  AppendBody $newbody $copy

  Set &nochange False
  Goto Split

:Skip
  AppendBody $newbody $line

  Inc #pos
  If #pos < #maxpos Then Loop
  If &nochange Then End

  AssignBody %message $newbody
  SaveMessage %message $CurrentMailbox

:End
Exit

:ErrorHandler
   Set $message "An error has occurred\n"
   AddStrings $message "$ErrorResult: " $ErrorResult " $ErrorLine: " $ErrorLine
   MessageBox $message
Exit


This updated version checks the content-type of the email and if it contains html it'll try to do a better job than the previous version by not splitting lines in the middle of tags and by adding <br> to the end of each line.

Set the max length in a line in "Setup Script". Strictly speaking the html version will go over this by 4 characters because of adding <br>

Some examples. Original plain-text email:

This is a test message to see if the Wrap lines script works. This is
a test message to see if the Wrap lines script works. This is a test
message to see if the Wrap lines script works. This is a test message
to see if the Wrap lines script works.


New wrapped email, with wrapping length set to 20:

This is a test messa
ge to see if the Wra
p lines script works
. This is
a test message to se
e if the Wrap lines
script works. This i
s a test
message to see if th
e Wrap lines script
works. This is a tes
t message
to see if the Wrap l
ines script works.


Original styled email:

This is a styled test email. This is a styled test email. This is a styled test email. This is a styled test email.


New wrapped email, with wrapping length set to 20 (the tags, excluding the added <br>'s, are included in this length):

This is
a styled
tes
t
email.
This is a styled
test
email. This i
s a styled
te
st
email.
This is a styled
test
email.


In practise you'll probably want to wrap it with more characters to each line but the examples demonstrate how it works clearly.

The original message is not overwritten and a new wrapped email is saved. If there was no wrapping to be done (ie. all lines are shorter than the length you set) then no email will be saved. Wrapping html email is slower than plain text because of the scanning for tags required, but it still only takes a few seconds.
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby robin » Tue Aug 31, 2004 8:10 pm

Hey Mat - you're turning into something of a script specialist :D

It would look neater if you can nadger the script to get it to wrap between words rather than within words. Having found the "end", perhaps it needs to backtrack to the previous space and then wrap there.

This of course doesn't help with the really long URLs that people have been reporting and only duplicates Poco's behaviour; so maybe it has to wrap if it finds no spaces within the line (i.e. the backtrack takes it back to the beginning of the line).
robin
 

Postby Hogyt » Tue Aug 31, 2004 8:34 pm

Thanks for your kind words Robin :oops: :D

You're right that would be neater to wrap between words! I'll have a try doing implementing that. I doubt theres much that can be done about the long URLs, they'll just have to wrap wherever!
Mat
Hogyt
Poco Enthusiast
 
Posts: 241
Joined: Thu Jul 29, 2004 11:22 am
Location: England

Postby robin » Tue Aug 31, 2004 10:00 pm

Hogyt wrote:I doubt there's much that can be done about the long URLs, they'll just have to wrap wherever!
Which is the whole point of course - they are the ones that cause people problems and therefore the ones that have to wrap.
robin
 


Return to PocoScript Help and How-To

Who is online

Users browsing this forum: No registered users and 1 guest

cron