Broad Network


Date and Email-Line Wrapping for Perl

Sending Email with ActivePerl – Part 3

Forward: In this part of the series we look at Date and Email-Line Wrapping for Perl.

By: Chrysanthus Date Published: 28 Jul 2012

Introduction

This is part 3 of my series, Sending Email with ActivePerl. In this part of the series we look at Date and Email-Line Wrapping for Perl. When you fill an HTML form and send the information as Email, it is advisable for the date the email is sent (written) to appear in the Email. The User does not usually fill the date in the form. The local server date and time is normally sent, automatically. I show you how that is done in this part of the series. No line, including header lines of an Email message should be longer than 78 characters (including the space characters). The email section header lines and the non-form message fields are not usually longer than 78 characters. By non-form message fields, I am referring to form fields like first and second name fields. However, the message field (Text Area) from the form may have lines longer than 78 characters. Such lines have to be broken down into lines of not more than 78 characters long. I show you how to do that in this part of the series.

You need basic knowledge in Perl regular expression, Perl and HTML in order to understand this article.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Everything said in this article is applicable to traditional Perl. However, with traditional Perl you have to precede your code with something like, #!/usr/bin/perl .

The Perl localtime Function
Perl has a predefined function called localtime. In simple terms the function returns a list as follows:

    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();

There are nine elements in the list. The values of the variables in the list are numbers. $sec is second as you would read from a digital clock (it may be one or double digit). $min is minutes as you would read from a digital clock (it may be one or double digit). $hour is hour (24-hour-clock) as you would read from a digital clock (it may be one or double digit).

$mday is the day number of the month and can be from 1 to 31. $mon is the month number and can be any number from 0 to 11, where 0 means January and 11 means December.

$year is the number of years since 1900. The year today is 2010. If you were to get the local time today, $year would be 110. You have to add the value of $year to 1900 to obtain the four-digit year.

$wday is the day number of the week and can be any number from 0 to 6, where 0 is Sunday and 6 is Saturday. $yday is the day umber of the year and it can be a number from 0 to 364 or 365 depending on whether you are dealing with a leap year or not. $isdst is true (1) if the specified time occurs during Daylight Saving Time, or false (0) otherwise.

The return value of the function, localtime can be assigned to an array. The following code illustrates a simple use of the function. Save the code as a Perl file in a web server. Call the file from a browser.

use strict;
print "Content-Type: text/html\n\n";

my @time = localtime();

foreach (@time)
    {
        print $_ . "<br />";
    }

The nine values displayed should be numbers as explained above.

Email Date and Time Format
Consider the following date-time example:

    Fri, 21 Nov 2010 09:55:13

This is an example of an email (U.S.A) date-time format. It needs a space, a negative sign and four digits to be complete (see below). Above, you have the day of the week in letters, a comma, the day number of the month, the month in letters, the four-digit year, the hour, minutes and seconds. Date and time are usually displayed in a particular format as in the above example.

To use the localtime function and finally display local time (date-time) in the above format, there are two main problems: the day of the week and month of the year are given as three-letter words; secondly the year is given as the four-digit year. The other values of the format can be read from the returned list of the localtime function. Since hour, minutes and seconds of an email have to consists of 2 digits, when these are read from the array returned by the localtime function, you have to check if they are two digits. If they are not you have to make them so by preceding the single digit with zero; that is not difficult (see below). For these quantities, numbers less than 10 come from the localtime function as 1 digit.

Here is how we shall link the letter days of the week and the letter months of the year to their corresponding numbers from the returned list: For the days of the week there will be an array of seven elements; the values of this array are the three-letter words for the days of the week. Each of the indices of the array element is a number that can return the three-letter day of the week. Index 0 corresponds to Sun, index 1 to Mon, index 2 to Tue, etc. For the months of the year there will be an array of 12 elements; the values of this array are the three-letter words for the months of the year. Each of the indices of the array element is a number that can return the corresponding three-letter month of the year.

The order of the elements of the email example time above is not the same order in the list returned by the localtime function. So the code to produce a local time string of the above example format has to do some reordering. It also has to select particular element from the returned list of the localtime function. These are implicitly done (see below).

Note the use of a comma, spaces and colons of the above example email format.

Implementation
The following code displays a string of the server local time according to the format given in the example above:

use strict;
print "Content-Type: text/html\n\n";

my @time = localtime();
my @week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
my @mYear = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

my $serverDateStr;

my $year = 1900 + $time[5];
my $hr = $time[2];
my $mn = $time[1];
my $sc = $time[0];
if (length($hr) == 1)
    {
        $hr = "0" . $hr;
    }
if (length($mn) == 1)
    {
        $mn = "0" . $mn;
    }
if (length($sc) == 1)
    {
        $sc = "0" . $sc;
    }
$serverDateStr = $week[$time[6]] . ", " . $time[3] . " " . $mYear[$time[4]] . " " . $year . " " . $hr . ":" . $mn . ":" . $sc;

print $serverDateStr;

Read through the above code.

Wrapping User Sent Message
The message the user sends may have lines that are longer than 78 characters. You produce text of not more than 78 characters long by inserting the Perl newline character (\n) within the text, in such a way that you cannot count up to 78 characters without seeing the newline character (\n). The code I use for this is as follows:

sub textWrap
    {
        $msgVal  =~ s/^ +//; #remove leading space characters
        my @arr = split (/\r\n|\n/, $msgVal); #split at new lines
        my $str = ""; #final string
        foreach (@arr)
           {#form the final string inserting \n
                if (length($_) <= 78)
                    {
                        $str = $str. "$_\n";
                    }
                else
                   {#get 12 words sequences ending them with \n
                       while ($_ =~ /(S+( S+){1,11})/g)
                           {
                                $str = $str . $1 . "\n";
                           }
                   }
            }
        return $str;
    }

Above we have a function that is called when the user message text has to be wrapped. The variable that holds the user’s Text Area message is $msgVal. The first line in the function removes any leading space in the user’s message. The user himself may create lines, even blank lines, as he types in the message box of the HTML form. The second line in the above function code, sends all the lines the user created into an array, where each element of the array is one of the user’s lines. Here, these user lines are sent to the array, without the rn or n that the browser might have inserted when marking the users lines. Note that some of his lines may be more than 78 characters long.

The third line in the function code declares a string variable that will hold the final string in which no line will be longer than 78 characters. The foreach loop above takes care of this.

There is an if-construct in the foreach loop. The if-part of the construct checks if any line of the array is already less than or equal to 78. If any line is less or equal, it sends it into the string, $str, ending it with n. The else part of the construct breaks down any of the user’s line that was longer than 78 characters to a number that is equal to or less than 78. It does this by just selecting the next 12 words including their spaces. I assume that 12 words including their spaces cannot be more than 78 characters long. Note the use of regular expressions in the function code.

At the end of the function code, the string in which no line is longer than 78 characters is formed. Each line ends with n. The ending of the user’s lines marked with rn or n by his browser are preserved. However, any rn inserted by the browser has been replaced by n. The formed string is now held in $str and is returned.

The Complete Email Message String
The following segment shows how the complete email string is formed:

my $wrappedText = textWrap();
my $emailMessage = "From: <$emailVal>\nTo: <$recipientVal>\nSubject: $sbjVal\n$serverDateStr -0000\n\nFirst Name: $fnameVal\nSecond Name: $lnameVal\nMessage: \n\n$wrappedText";

The first line in the code segment calls the textWrap() function above and assigns the returned string to the new variable, $wrappedText. The second line forms the complete string for the email beginning from the From field. Note the variables in the string that were obtained from the Perl query object (in the previous part of the series). Note the $serverDateStr variable for the date we obtained above. Note the text “ -0000” just after the variable which is used when the email time is local (server) time. After that you have \n\n (double newlines). This will create the blank line you need for the separation between the email header section and the email body. The other double newline characters you see toward the end of the string formation is for formatting in body content; it is not indispensable as the previous.

Let us stop here and continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message