Broad Network


Email Structure and Perl

Sending Email with Perl – Part 1

Perl Course

Foreword: In this part of the series I talk about email structure basics.

By: Chrysanthus Date Published: 23 Nov 2015

Introduction

This is part 1 of my series, Sending Email with Perl. In this part of the series I talk about email structure basics. I assume that you have been sending emails and you are already familiar to some of the basic terms like email address. For this series, I will talk mainly about sending email; I will not talk about replying or forwarding email.

The aim of the series is to teach webmasters how to send emails from web page forms. There are many email agents or software (including web mail) today that will receive, reply and forward emails for you. As a webmaster, your primary aim with emails is how to send emails from HTML (web) forms.

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 .

Pre-Knowledge
At the bottom of this page, you have links to the different series you should have read before coming here. This series is a continuation.

Client and Server
When you sit in your home or office and send an email, the computer you use is the client computer. The email will pass through the Internet wire (and space) and arrive at another computer that keeps and maintains emails. This second computer is the server computer. The program in the server computer that actually does the keeping and maintenance of emails is called the email server. So both a computer and a program can be called a server. When the person to whom the email was sent thinks he has an email, he would logon to the server and he would see the email you sent. There are three computers here: the client computer, which you use, the server computer and another client computer, which the email receiver uses.

The focus in this series is on the email message format, and not on the network of computers or movement of email from one computer to the next.

Example Email
Imagine that you were to type your email in a text editor. Also imagine that what you type is what has to be sent through the Internet wire from the client computer to the server computer. The following is an example of what you would type:

From: John Smith <jsmith@myserver.com>
To: Mary Taylor <maryt@herserver.net>
Subject: Payment of Dues
Date: Fri, 21 Nov 2010 09:55:00 -0000
Cc: The President <president@boss.com>
Bcc: Police <pol@theaudit.com>

Dear Madame,
I am by this email reminding you to pay your dues. I also want to let you know that an email message is made up of lines, and after each line you must press the enter key of your keyboard if you are typing the email in a text editor and if the email will be sent as typed, through the Internet without modification.
Sincerely,
John Smith

Email Outline
An email message is made up of lines of characters. These lines are grouped into two sections: the header section of the message and the body of the message. The header section is separated from the body by a blank line. To achieve this you have to press the enter key of the keyboard twice just as you finish typing the last line of the header, if you are typing the email in a text editor and if the email message will be sent through the Internet, without modification. You can clearly see the header section and the body of an email message in the above sample. An email message consists of the header section and the body. The body is optional. So you can actually send an email that has only the header section.

Each line you type should not be more than 78 characters long. So each header line should not be more than 78 characters long and each body line should not be more than 78 characters long. These 78 characters include the space characters created by pressing the spacebar on the keyboard.

In this part of the series I assume that you are typing your email with a text editor and the email will be sent through the Internet without modification. So a line is achieved when you press the enter key of the keyboard. So while typing you should be counting the characters and you press the enter key as soon as you have typed 78 characters. Some lines are shorter than 78 characters. In that case you press the enter key after typing the short line. 78 is the upper limit of a line. Most header lines are naturally shorter than 78 characters.

In the body of the email, paragraphs are separated by blank lines. In each paragraph no line is longer than 78 characters. To achieve a blank line with your text editor, press the enter key twice (just as you finish typing the last line). In email, a sentence is not a line. A line can be made up of more than one sentence; but a line should not be more than 78 characters long. A sentence can be split in two lines in a paragraph. Note: in other forums a paragraph is a line, but not with emails.

A blank line is achieved when you press the enter key twice at the end of a previous line; technically, when you do this, the second line (press) is a line of zero length, still shorter than 78 characters, as required. After the header section you need a blank line; the other possible blank lines in the message body do not cause any wrong interpretation of the structure of the email.

When you press the enter key, you insert the internal characters, “\r\n” in your text editor. You do not see these characters as you type. In some operating systems it is just “\n” that is inserted (you do not see it). These internal characters are not part of the 78 characters.

The message body has the personal information you want to send, while the header has technical information such as the email address.

The Header Section
Each line in the header section is called a field and it is made up of two parts: the field name and the field body. These two parts are separated by a colon (:). The word, body is used in two places in the email message: you have a header field body and the message body.

In the above sample, there are six header lines and so six field names and six field bodies. The six field names are From, To, Subject, Date, Cc, and Bcc

The From Field
An example of the From field is,

    From: John Smith <jsmith@myserver.com>

You begin with the word, From. This is followed by a colon. After this colon, you can have the actual name of the person who is writing the mail. The presence of this name is optional. After the optional name you must have the email address of the person writing the mail in angle brackets. After typing any header field you must press the enter key.

The To Field
An example of the To field is,

    To: Mary Taylor <maryt@herserver.net>

You begin with the word, To. This is followed by a colon. After this colon, you can have the actual name of the person who will receive the mail. The presence of this name is optional. After the optional name you must have the email address of the person who will receive the mail in angle brackets.

The Subject Field
An example of the Subject field is,

    Subject: Payment of Dues

You begin with the word, Subject. This is followed by a colon. After this colon, you have the text for the subject of the message body content.

The Date Field
An example of the Date field is,

    Date: Fri, 21 Nov 2010 09:55:00 -0000

You begin with the word, Date. This is followed by a colon. After this colon, you have the text for the Date and time the person writes the email. This text must be in a special format. In this article I give you the format for U.S.A. For simplicity we assume that the date and time you will type here is what you read from your wristwatch or clock.

You begin the text with one of the following days of the week:  Mon, Tue, Wed, Thu, Fri, Sat and Sun; each is three letters long. After this you have the comma. Then a space and then the day number of the month. You have a space again and then one of the months of the year in three letters: Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, and Dec. Then you have a space and the year in 4 digits.

After the year you have a space and then the time. For this article the date and time is what you read from your wristwatch and type. This is called the local time. Of course the time on your wristwatch should be correct. You begin the time with the hour in two digits - the 24-hour clock (any number less than ten begins with zero). Next you have a colon. After the colon you have the minutes in two digits (any number less than ten begins with zero). Then you have a colon again and then two zeros. Allow these two zeros for simplicity as you can have other digits there. Then you have a space, the negative sign and then four zeros. Let us allow this last section as it is for simplicity as you can also have a plus sign with four other digits.

The Cc Field
When you send an email to a person, you may also wish to send a copy of the same email to another person. Cc stands for carbon copy. An example of the Cc field is,

    Cc: The President <president@boss.com>

You begin with the word, Cc. The rest of the syntax in the field is as in the case of the To field.

The Bcc Field
Bcc stands for Blind Carbon Copy. When you send an email to somebody, you can choose to send a copy to another person. Both the primary recipient and the person who gets the copy will be aware that both of them have read the same information. It is possible to send another copy to a third person and neither the primary recipient nor the person who officially gets the copy would know that a third person has received a copy. You use the Bcc field for this purpose. The person whose email address is in the Bcc field will know that you have sent the email to the primary recipient and a copy to somebody, but neither the primary recipient nor the copy receiver would know the person who has received a Bcc copy; neither may even know that a Bcc person even exists (while he exists).

An example of the Bcc field is,

    Bcc: Police <pol@theaudit.com>

You begin with the word, Bcc. The rest of the syntax in the field is as in the case of the To field.

Obligatory Header Fields
From the Email specification, an email must have a From and a Date field. The other header fields are optional. You and me know that if there is no To field the email will not arrive "anywhere". So, let us say that an email should have at least three header fields, which are the From, To and Date fields.

Order of Header Fields
You can type the header fields in any order, not necessarily in the order given in the above sample. For example you can start by typing the Date field, then the To and then the From field; and the rest still in any order. The three fields mentioned can still be typed in any order and have other fields between them. Just remember that after typing a header field, you have to press the enter key.

Multiple Email Addresses
Apart from the Date and Subject fields above, each of the fields can have more than one email address. In each of the field body, you separate the addresses with commas. The following sample illustrates this:

From: <onef@onef.com>, <twof@one.com>, <threef@onef.com>
To: <onet@onef.com>, <twot@onef.com>, <threet@onef.com>
Subject: Payment of Dues
Date: Fri, 21 Nov 2010 09:55:00 -0000
Cc: <onec@onef.com>, <twoc@onef.com>, <threet@onec.com>
Bcc: <oneb@onef.com>, <twob@onef.com>, <threeb@onef.com>

Dear Colleagues,

We are by this email reminding you to pay your dues. We also want to let you know that an email message is made up of lines, and after each line you must press the enter key of your keyboard if you are typing the email in a text editor and if the email will be sent as typed, through the Internet without modification.

Sincerely,
Higher-Up Team

Note the use of the commas in the above sample. Also note in the above sample that the optional email names that would precede email addresses are not given. This time the message body has blank lines to create paragraphs. The blank line between the header section and the body still has to be there.

In the above email samples the message body lines may not respect the 78 characters limit. This is because I did not have the time to count the characters.

Coding with Perl
Assume that you have a server and the server can run Perl scripts and the server has what is called the sendmail program. To send the first email sample above, you would type,

open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
                        or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: John Smith <jsmith\@myserver.com>
To: Mary Taylor <maryt\@herserver.net>
Subject: Payment of Dues
Date: Fri, 21 Nov 2010 09:55:00 -0000
Cc: The President <president\@boss.com>
Bcc: Police <pol\@theaudit.com>

Dear Madame,
I am by this email reminding you to pay your dues. I also want to let you know that an email message is made up of lines, and after each line you must press the enter key of your keyboard if you are typing the email in a text editor and if the email will be sent as typed, through the Internet wire without modification.
Sincerely,
John Smith
EOF
close(SENDMAIL)     or warn "sendmail didn't close properly";

The above is the first email sample but with Perl code fitted to it. It is still assume that you type the above code including the Perl features in a text editor. The first line begins with the Perl open function. It has two arguments. A statement in Perl ends with a semicolon. The first statement above may take two lines. There are two parts to the first statement. The first part is the open function. If the open function fails, then the second part will report the error. Do not worry too much about this second part, but have it in your own code.

Let us look at the arguments in the open function. The first argument, SENDMAIL is an example of what is called a filehandle. A filehandle is a special type of variable. At the moment the Perl code email is assumed to be in a server (e.g. web server). This email code will have to travel through the Internet network to arrive at some email server. In order for that movement to take place, you need a filehandle. The filehandle above is declared and indirectly assigned there, as given; you do not see the assignment. You can give whatever name you want for this filehandle. Do not precede it with the $ sign; it is a special type of Perl variable.

The second argument is in quotes; it begins with the character |. That is followed by a path to the program, sendmail. The path ends with the name of the program, which is sendmail. This program is what actually sends the email to the email server. Now, Perl is case sensitive, so SENDMAIL and sendmail are two different things. The sendmail program may have a name different from sendmail. Ask your system administrator (of the server where the Perl email to be sent currently resides) for the path to the sendmail program and the name of the sendmail program; make sure you type the | character before you type the path. Note the forward slashes in the path. -oi and –t are options you should type in the second argument, as type above. I will not explain the meaning here.

The second statement (ending with semicolon) in the Perl email code is,

    print SENDMAIL <<"EOF";

This is the print function (without parentheses) followed by the filehandle in the open function. Then you have a space and then << and then "EOF". This means, print (send) what follows into the filehandle, SENDMAIL until you meet the text EOF. What follows is the sample email exactly as was typed above with the only difference that the @ symbol for the email addresses is preceded by \ . This symbol is escaped, because in Perl @ is used to declare an array; the above code is a Perl code.

Note the blank line, which is necessary between the header section and the body section in the email message in the code. Also note the EOF text, which is in one line at the end of the email message.

The first statement above opens the filehandle and the last statement closes it. If the closing process fails then the second part of the last statement reports the error. Do not worry too much about this second part but have it in your own code.

If you omit the second part of the first and last statements, your code should still work, but it is advisable to have them.

That is it for sending simple emails with Perl. So far as email is concerned there is a lot more to the header section than I have given, but as a Webmaster you do not need all that for sending email from your web server to an email server. This is because the receiver will likely have an email agent (program) that he will use to open the email and be able to send whatever complicated reply he wants to send. His reply will not come to your web server; it will go to the email server that maintains (keeps) your email. His reply will come to your email server, because you type the email address of your email server in the From field in the code.

Time to take a break. Let us stop here for this part of the series. We continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message