Broad Network


Date and Email-Line Wrapping for PHP

Sending Email with PHP – Part 3

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

By: Chrysanthus Date Published: 29 Jul 2012

Introduction

This is part 3 of my series, Sending Email with PHP. In this part of the series we look at Date and Email-Line Wrapping for PHP. 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 70 characters (including the space characters). The email section header lines and the non-form message fields are not usually longer than 70 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 70 characters. Such lines have to be broken down into lines of not more than 70 characters long. I show you how to do that in this part of the series.

You need basic knowledge in PHP 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.

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.

The day, e.g. Fri, is a three-letter word. The month e.g. Nov, is a three-letter word. If the number for the hour is less than 10, then it has to be preceded by zero. If the number for minutes is less than 10, then it has to be preceded by zero. If the number for seconds is less than 10, then it has to be preceded by zero. Such a zero is called a leading zero. The day number for the month does not have a leading zero.

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

The PHP date Function
PHP has a predefined function called, the date function. In simple terms the syntax for the date function is,

    string date(string $format)

The date-time returned by this function is the local server date-time. The return date is in string form. The argument $format is a string that dictates the format in which the string date should be returned.

The following expression will return the email date-time format above for the local date-time of the computer (server having the PHP script):

    date("D, j M Y H:i:s")

The returned string date has the format dictated by the format argument of the function. The letters in the argument are case sensitive. In this argument, D returns a three-letter day at its position. The next thing in the argument string is a comma. The comma is returned as comma in that position. Next you have a space in the argument. The space is returned as space in that position. Next you have j. j returns a month day number without leading zero. Next you have a space, which is returned in its position and then M. M returns the month in three letters. Next you have a space, which is returned in its position. Then you have Y. Y returns the four-digit year. Then you have a space returned as space at its position.

After that you have H, which returns the 24-hour with leading zero. Next you have the colon, which is returned at its position. Then you have i that returns the minutes number with leading zero. Next you have a colon, which is returned at its position. Then you have s, which returns the seconds number with leading zero.

Try the following code, which displays date-time in email (U.S.A) format.

<?php
    echo date("D, j M Y H:i:s");
?>

Line Wrapping
The lines of the header section of an email are not usually longer than 70 characters. In many cases you do not need to worry about the length of the header lines and it would suffice to just terminate a header line with rn. If you prepare your email as I described in part one of this series, then putting rn between the header lines would suffice assuming that no header line is longer than 70 characters.

The message from the Form Text Area may have lines longer than 70 characters. The user may also use blank lines to create paragraphs in the Form Text Area; that is alright. Any blank line the user might have inserted is equivalent to two newline characters. Everything being equal these newline characters shall be preserved when the receiver gets the email.

PHP has a predefined function called, wordwrap. This function would wrap a string into lines delimited by \n. In simple terms, the syntax of the function is,

string wordwrap ( string $str [, int $width])

Its first argument is the string, which you want to convert into another string of lines. It returns the string of lines. Its second argument is an integer, which gives you the number of characters (including spaces) that will be in each line. The newline character used to end each of the lines is \n.

In the previous part of the series, the variable in the script that holds the Form Text Area message, is $msgVal. So the following expression will wrap all the Text Area message into a string of 70 lines; 70 here does not include the newline character, \n :

    wordwrap($msgVal, 70)

The return value will be a string in which no line is longer than 70 characters.

The $message Content
The value of $message is formed as follows:

$message = "First Name: $fnameValnLast Name: $lnameVal \nMessage: \n\n" . wordwrap($msgVal, 70);

The \n\n you see is there for formatting purposes and not to separate the email header section from the email body section. PHP inserts the blank line between the header section and the body automatically (without you knowing). The variables in the string above will be replaced by their values.

Well, we can take a break 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