Sending Web Form Data Set as Email with PHP
Sending Email with PHP – Part 2
Forward: In this part of the series we see how to send web form data set as email.
By: Chrysanthus Date Published: 29 Jul 2012
Introduction
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.
You need basic knowledge in HTML (or XHTML) and PHP in order to understand this article. If you do not have basic knowledge in HTML then read the series I wrote titled, XHTML Basics. To arrive at the series, type the title and my name Chrys in the Search Box of this page and click Search.
Example Email
Consider the following PHP email ready to send from a web site server:
<?php
$to = " <maryt@herserver.net>";
$subject = "Payment of Dues";
$message = "First_Name: John\r\nSecond Name: SmithnMessage: \r\nDear Madame,\r\n\r\nThe above salutation line is one paragraph from the point of view of\r\nthe layman, because a blank line follows it. This is the third\r\nparagraph from the layman’s point of view and no line here should be\r\nlonger than 78 characters including the spaces. The first three lines\r\nof the body of this message also form a paragraph from the point of\r\nview of the layman.\r\n\r\nNo line in the email header section should also be longer than 78\r\ncharacters. In the body of an email the paragraphs of the layman are\r\nseparated by blank lines. To achieve a paragraph (for layman), type\r\nthe enter key twice immediately you complete the last line of the\r\nprevious paragraph. The following two readable lines of this email form a paragraph\r\n\r\nSincerely,\r\nJohn Smith\r\n";
$additional_headers = "From: <jsmith@myserver.com>\r\nDate: Fri, 21 Nov 2010 09:55:00 -0000\r\nCc: <president@boss.com>\r\nBcc: <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.";
}
?>
Retype the above $message content pressing the enter key instead of typing n to appreciate how the receiver will read the email body.
The information in the body of the email message above can be filled in a web (HTML) form to be sent by email. In order for the email receiver (human) to reply your web form information, the web form should have a field for you to fill in your email address. The reply will go to your email box and not to the web server that hosts the web form and its web site. The reply will not go to your web server even though your email PHP script is in your web server.
In order for the information to go to the receiver’s email box, his email address also has to be in the web form. This is usually hard coded in hidden input type control in the web form. The form for the above email is as follows:
<form action="email.php" method="post">
<h3>Form Heading here</h3>
<input type="hidden" name="Recipient" value="maryt@herserver.net"><br /><br />
*First Name: <input type="text" name="First_Name"><br /><br />
Last Name: <input type="text" name="Last_Name"><br /><br />
*Email: <input type="text" name="Email"><br /><br />
Subject: <input type="text" name="Subject"><br /><br />
*Message: <textarea cols="50" rows="3" name="Message"></textarea><br /><br />
<button type="submit">Send</button>
</form>
The value of the action attribute is email.php. This is the name of the PHP file script that will receive your data set from the user’s browser, convert it to an email and send the email to the email server. You can give whatever name you want for the file, but end it with the extension, .php. For a data set, the name of a form control and its value are sent when the submit button is clicked. So, make sure each of the important controls of your form has a name. A control name, such as “First_Name” can be important text in the body of the email message. Note that the user types only his first name and he does not type the string, “First_Name”. This string is the name of a control and should be important text in the body of the message. Precisely, the body of the message will have,
First Name: John
Here, the string “First Name” is coming (read) from the name of a form control, while “John” is the value typed in the control’s field.
In the form code, the hidden input control has the email address of the email box of the person who will receive the email. Hidden input controls are not displayed when the web form is displayed. You have three input text controls in the form code. Unlike with the hidden control, these text controls will be filled by the Internet user at his client browser. He has to fill his first name, he has to fill his last name and he has to fill his email address in order to have a reply. The name of the first input text control is, “First_Name”; the name of the second input text control is “Last Name”; and the name of the last input text control is “Email”.
The user will type his email address in the Email input text control. In the Text Area control the user will type his message (personal message). What he types here will not include his first name, last name or email address. When converting the data set into an email, our PHP script in the web server will put the user’s email address in the header section of the email message. The email message consists of the email header section and the email body. The form message is only what you type in the Text Area control. This will be the main part of the email body.
When the form is displayed, it looks as if the first name, last name and email address will go into the email header section. The email address will go into the email header section but the first and last names will go to the body section of the email. Names (user names) are not part of the email header section.
Do not confuse between the form (user’s) message, the email message and the email message body. The form message is what the user types in the Text Area control field of the form. The email message consists of the header section and body section of the email. The email body has the sender’s credentials and the sender’s personal (Text Area) message.
There are three main code segments of the PHP script. The first obtains the values of the controls in the data set sent when the user clicked the submit button. Remember, the PHP script is in the web server that receives the form information. The second main code segment validates the values received to see if they were well type; if not, error message is sent back to the user for retyping and the email is not sent to the email server. If there is no typing error, the third main segment prepares and sends the email; otherwise the third main segment does not send the email. The third main code segment will also send a feedback to the user whether the email has been sent to the email server or not, since it is the sendmail program that effectively sends the email, not the PHP script.
There are three possible feedbacks to the user. If he made any mistake when filling the form then he would receive a feedback saying he made a mistake (his email will not be sent to the email server). If he filled the form correctly, and if the sendmail program operated correctly, he will receive feedback saying his email has been sent to the email server. If he filled the form correctly, but if the sendmail program did not operate correctly, then he would receive a feedback saying his email could not be sent to the email server.
The First Main Code Segment
This is the first main code segment for the PHP script for the above email:
$recipientVal = $_POST['Recipient'];
$fnameVal = $_POST['First_Name'];
$lnameVal = $_POST['Last_Name'];
$emailVal = $_POST['Email'];
$sbjVal = $_POST['Subject'];
$msgVal = $_POST['Message'];
PHP has a global predefined array called $_POST. If your HTML form uses the post method then this array will have all the form control values as its values. The names of the form controls will be the corresponding keys of the array. So, when the HTML form is sent from the browser, the $_POST array for the PHP script at the web server will receive the data set. To get the value of each control in the script, you need the name of the control. You assign the return value (for the above statements) to a variable with any name you want.
These assigned variable names will be used when forming the PHP email code. For example, $recipientVal above will be used in the email code in place of the To email address. The control names, First_Name, Last_Name and Message of the HTML form will also be used in the email body. That is how you do it. You have to use the assigned variable names and some of the control names in the email code as will be illustrated below.
The second main code segment can be very large. It uses the assigned variables to determine if the values of the HTML form controls were properly typed. It they were not properly type, it sends error messages back to the browser and the third main code segment that forms and sends the email is not executed. You should consult other documents in this blog to see how to write the second main code segment effectively; I will show you a simple form of that in the fourth part of this series.
The Third Main Code Segment
The third main code segment is the formation of the PHP email code. It needs the assigned variables and some of the control names. This is the main function of the third main code segment for the above form and supposed email (see details in part 4):
mail($to, $subject, $message, $additional_headers)
It is good to know the content of $message: It has the First_Name and Last_Name Form control names and values. Each of these pairs will be in its own line. It also has the Message (Text Area) Form control name and value.
We shall see how the complete message string is formed in the next part of the series.
So if the third main code segment above is executed, the email will be sent to the email box of the recipient. Let us end here and continue in the next part of the series.
Chrys
Related Links
Major in Website DesignWeb Development Course
HTML Course
CSS Course
ECMAScript Course
NEXT