Broad Network


Email Structure and PHP

Sending Email with PHP – Part 1

Forward: In this part of the series, I give you the structure of email and how to use PHP to send email.

By: Chrysanthus Date Published: 29 Jul 2012

Introduction

This is part 1 of my series, Sending Email with PHP. In this part of the series we shall look at the email structure. I assume that you have been sending emails and you are already familiar to some of the basic terms like email address. You also need basic knowledge in PHP in order to understand this series. If you do not have that knowledge, then type, “Getting started with PHP” and my name, “Chrys” in the search box of this page and click Search. That will take you to the series on the basics of PHP. Well, in this article, I give you the basics of Email structure. For this series, I will talk only about sending email; I will not talk about receiving, replying or forwarding email.

If you do not have basic knowledge in PHP then read the series I wrote titled, “Basics of PHP”. To arrive at the series, type the title and my name Chrys in the Search Box of this page and click Search.

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.

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.

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 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.

Our 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 wire 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 wire, 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 70 characters long. This is the limit imposed by PHP. So each header line should not be more than 70 characters long and each body line should not be more than 70 characters long. These 70 characters include the space characters created by pressing the spacebar on the keyboard. They do not include the internal characters generated when you press the enter key.

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

In the body of the email, paragraphs are separated by blank lines. In each paragraph no line is longer than 70 characters. To achieve a blank line with your text editor, press the enter key twice (just as you finish typing the last line of your previous paragraph). 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 70 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. The word, “paragraph” does not have a precise definition in programming.

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 70 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, “rn” in your text editor. You do not see these characters as you type. In some cases it is just “n” that is inserted (you do not see it). These internal characters are not part of the 70 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 letter 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 must 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 cable 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 70 characters limit. This is because I did not have the time to count the characters.

Coding with PHP
To test the PHP code samples in this series, you will need a browser, a web server and an email server. The code samples in this series assume that your PHP interpreter and web server have been configured to work together and your PHP code will implicitly (without you knowing) call a program called sendmail. Many PHP installations behave like this. If in doubts, check with the system administrator of your commercial web server if the installation is different. It is the sendmail program that will actually send the email to the email server.

Note: For this series, the PHP email script is in the web server.

PHP Predefined Mail Function
PHP has a predefined function called the mail function. In simple terms the syntax is:

bool mail(string $to, string $subject, string $message [, string $additional_headers])

It returns a Boolean true if it succeeds in sending the email to the email server through the sendmail or a Boolean false, if it fails. The To field body e.g. “John Smith <jsmith@myserver.com>” will be the string value of the $to argument of the mail function above. The Subject field body will be the string value of the $subject argument of the email function above. The email message body will be the string value of the $message argument of the function above. This value will have lines of not more than 70 characters long, including the spaces. The From Field, the Date Field, the Cc field and the Bcc field will together form the string value for the $additional_headers argument of the above function. The fields for this value are separated by “rn”.

You do not need the email field names in order to use the $to, $subject and $message arguments for the function. However, you need the complete header field (field name and field body) for the headers that go to the $additional_headers argument; separate the fields with “rn”. The email message body does not have any equivalent field name.

The following PHP code will produce and send the first email sample above to the email server.

<?php

$to = "Mary Taylor <maryt@herserver.net>";
$subject = "Payment of Dues";
$message = "Dear Madame,nI am by this email reminding you to pay your dues. I also want to letnyou know that an email message is made up of lines, and after eachn line you must press the enter key of your keyboard if you are typingn the email in a text editor and if the email will be sent as typedn through the Internet wire without modification.nSincerely,nJohn Smith";

$additional_headers = "From: John Smith <jsmith@myserver.com>rnDate: Fri, 21 Nov 2010 09:55:00 -0000rnCc: The President <president@boss.com>rnBcc: Police <pol@theaudit.com>";

if (mail($to, $subject, $message, $additional_headers))
    {
        echo "Email has been sent";
    }
else
    {
        echo "Email could not be sent! Contact your system administrator.";
    }

?>

The email features are first formed as strings before being used in the mail function. Note that in the email message body, the lines are separated by n and none of the lines is longer than 70 characters. The mail function is in an if-condition. If the email is sent by the function, the user is told that the email has been sent, echoed to his browser by the if-block. If the function does not succeed in sending the email, an error message is echoed to the user by the else block.

That is it for sending simple emails with PHP. 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 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 go to the email server, because you type the email address of your email server in the From field in the above code.

Let us stop here for this part of the series. In the next part we shall see how to send web form information to an email box using Perl.

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