Home > Courses > Web development using PHP and MySQL > Sending e-mail with PHP (Feedback form)

Sending e-mail with PHP (Feedback form)

Subject: Web development using PHP and MySQL
In the example, we we create a simple feedback form to send or submit user comment or question via the form to our e-mail (contact@tealearn.org).

Below is the code for the contact form.

contact.htm

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


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. You can also use CSS for this form structure or styling.


Below is the PHP file (contact.php) that the contact.htm form will submit to, the program send the information supplied to our e-mail.

contact.php

<HTML>
<HEAD>
<TITLE> Company Feedback </TITLE>
</HEAD>
<BODY>

<?PHP
// keep the form data in variable   
$name=$_POST['names'];
$email=$_POST['email'];
$comment=$_POST['comment'];

//the sender email
$from=$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
$to='contact@tealearn.org';

// 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
 mail($to,$title,$msg,$headers);

print "Thanks for your feedback; we will get back to you as soon as possible, if required of us. ";
?>
</BODY>
</HTML>


Note:
mail($to,$title,$msg,$headers) line of code i.e mail() function sent the email using the required parameters stored in some variables.

This program will work if you have your email server setup, which must have already been done if you have hosted website online.













By: Benjamin Onuorah

Comments

No Comment yet!

Login to comment or ask question on this topic


Previous Topic Next Topic

Supported by