Subhash AKA “CodeKadiya” is a Software Engineer who is specializing in Web technologies. He has over 7 years of experience in PHP both in industry and freelance work providing web-based solutions for a wide range of clients from many parts of the world. His expertise in programming spans into XHTML/CSS, Apache, MySQL, Javascript/jQuery and AJAX technologies as well. During the past couple of years he has been developing web applications using popular industrial PHP frameworks such as Symfony, CodeIgniter and Zend. He is a FOSS enthusiast and currently employed at Latitude655. Subhash developed a passion for programming at the age of 13 and his swift efficient coding was appreciated by the community of Codewalkers.com. Subhash obtained his Bachelors degree from British Computer Society (BCS). He is currently engaged in a Masters in IT at University of Colombo. He draws extreme pleasure out of listening to music at all times including when he is programming.
 

PHP

11/25/2009 4:32 am By Subhash Vithanapathirana | Articles: 9

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

Share/Save
No votes yet