Sending e-mail with PHP

Although one single line of PHP could do this (send email) but it will be helpful if I could make this program a little detail. Let’s assume that your client want you to program his website feedback or contact form to send or submit visitor’s comment or question they supply via the form to his e-mail, although this data can also be stored in the database but I’ve seen that some clients, most especially the busy or “technophobia” ones would prefer to access this important information using their email which is a technology they are already use to as compare to any other “new” thing you want to introduce them to.

Below is the assumed contact form.

<TITLE> Company Feedback </TITLE>
<form method="post" action="contact.php">
<tr><td colspan="2"><b>FEEDBACK FORM</b></td></tr>
     <tr><td><strong> Name</strong></td>
<input name="names" type="text" id="names" size="30"/>
<input name="email" type="text" id="email" size="30" />
<textarea name="comment" cols="30" rows="5" id="comment">
<td><input type="submit" name="Submit" value="Submit" /></td>

This HTML code is a simple form, I structured it using table, as you can see the table is inside the form tag, and it hold or structured the text and form elements, below is the preview of this form.


Below is the PHP page (contact.php) that the contact.htm form will submit to, the page does the task of forwarding the information supplied to an e-mail address.

<TITLE> Company Feedback </TITLE>

// keep the form data in variable   

//the sender email

// building the message or email body
$msg='New feedback message from my website,'."\r\n";
 $msg .='Name :' .$name. "\r\n";
$msg .='Email :' .$email. "\r\n";
$msg .='Comment :'.$comment;

//company email address, to send the feedback to

// the email title
$title=' New feedback message from my website ';

// some header properties of the email
 $headers = "From: ". $from ."\r\n";
 $headers .= "Reply-To: ". strip_tags($reply_to) . "\r\n";
 $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-Type: text/plain; charset=utf-8\r\n";

// send the email

print "Thanks for your feedback; we will get back to you as soon as possible, if required of us. ";

I believe the PHP code above is quite explanatory because of the comment lines, although every line is important but one line I want you to take note of is the one in bold mail($to,$title,$msg,$headers); because this line that actually sent the email , if you look carefully at each line of code above you will notice that they only store or hold the required data into some variables, which were now used by this mail() line.

Note: if you test the program on the localhost, you will get error message as we have below.


This is because there is no email server setup to handle this task, although this will work fine if uploaded to your remote or hosted server online, although I downloaded an Email server program and set it up locally to test, if you care for this, download and setup hMailServer

To test this example using a local email, for example hMailServer, I have to edit the email address to sent the email to, to ben@localhost, see the code below, note I actually commented the former line…


Note: I’ve already created the local email address ben@localhost in my hMailServer.

 Then I fill the feedback form, as I did below.


When I submit the form, no error is shown since the mail server and email exists.


Then I reach to the location of the hMailServer directory where the emails for ben@localhost is kept, this can be a long journey as you can see on the address bar. 


Because the email is kept as raw file, a unique folder is created automatically for each email that comes in, so I assumed that 6A, is where the most recent email is kept.


And when I opened the folder, the email file also have a unique name, note the file has .EML file extension, so I had a problem opening it (in Windows 7, but not Windows XP), so I quickly Google for an EML viewing application and download one for free and make it the default program to use in opening such file. So when I double click the email file it opened with the EMLViewer, as we have below.


That all, it works fine! However note that you don’t have to go through all this to test as I said earlier just upload it to your hosting web space online if you have one, the hosting company already have everything taking care of,  I just love to see how things work behind the surface.


Return to PHP Sample Main Page