Colombo 05, Sri Lanka
24-07-2010
Colombo, Sri Lanka
16-06-2010
Colombo, Sri Lanka
21-05-2010
Dehiwala, Sri Lanka
21-05-2010
PHP

by Subhash Vithanapathirana

Introduction

When do you want to send emails from your script? Well, there can be number of instances you need this functionality in your website/system. Imagine a “contact us” feature of your website; when your visitor fills in his/her contact info and comments, the feedback will be sent to you via an email. You've got a user registration system in your site, you want to send your new members a welcome email with login credentials; also to send users their forgotten passwords... list goes on and on. Let's see how to build a basic comments form today.

Construction of HTML form

First of all, we need to build the form to capture user's input. This example is going to be a simple one with only Name, Email and Comments fields – you can have any number of fields with suitable form elements, it does not effect the output of the email.

Name the above script any name you like (eg: contact.php, comments.php …). I assume you remember the usage of $_SERVER['PHP_SELF'] which was explained in a previous article. It outputs the filename of the current script. The idea is we want to submit the form to the same PHP page, instead of a separate form handling script.

Email sending script

Since we have the form ready, we just need to write the form handling script which send an email to the site administrator (probably you) with the details. PHP's in-built mail() function allows sending email directly. See the table below for explanation of mail() function. mail(to,subject,message,headers,parameters)

 

I have written the code below as an example to send the captured data of above form.

Place this piece of PHP code right above the opening tag of form. Note the the condition to check whether $_POST['Submit'] variable is defined. (see isset()) This is to ensure that this PHP code will only execute when the form is submitted (not on the first page load - when displaying the form). This is a cool way of writing form handlers. However, focusing on the important section of the code – it's the use of mail(). As you see it's just a matter of passing the parameters in order. You're free to format the message body the way you want. This code can be improved in many ways. For instance you may want to provide a From: address to the email you receive so that there is a clear indication that it is from your website. To do that specify the fourth parameter:

Note that the above code sends a plain-text email, therefore you can only use limited amount of formatting. In case you want to include complex formatting such as text formatting, images, hyperlinks, table structures etc you need to send an HTML email. To achieve this, add extra headers to your mail to indicate it's HTML.

mail() function returns a boolean value depending on the whether the email has been accepted successfully for delivery(TRUE) or not(FALSE). Therefore, you may use the $result variable to display a message to user regarding their request. That is how we build a simple comments form with PHP. Wasn't it easy than you expected? I think so! Oh, by the way don't forget to validate user input before you send the email or else you'll end up receiving whole bunch of useless junk emails in your inbox. Think about validating required fields, email address format, minimum lengths of comments etc. See you next time; till then enjoy - Don't SPAM! :-)

 

Previous Article

Your rating: None Average: 5 (1 vote)

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.