stargeek
PHP news website logo.
home    PHP scripts    articles    seo tools    links    search    contact    shop    realtors


Making a PHP Email Contact Form


After noticing that my PHP email form was my most popular script and reading all of the emails from people having problems with earlier versions of my script, I decided to write a brief tutorial on the implementation of my php email form script. You can try a worling demo of this script on my contact page.

The first thing that will be nessecary to allow php to send email is to get the path to the sendmail (on *nix) binary on your web server. You can view your server's current default sendmail path configuration my constructing a phpinfo() page. If this is not set or you are not sure if it correct you should contact your web server's administrator. The line of code in my script that stores the path to sendmail, and should be changed to reflect your environment is this:

$mail_path = "/usr/sbin/sendmail -i -t" ;

Next you should replace the various configuration variables that come after the sendmail path line. These include the email address the script will be sending emails to (your email address), the subject line of the emails sent to that address, and the name of the buisness or website where this script is being used (it is used for the confirmation messgage after an email is sent). I get countless emails everyday from people who have forgotten to change the the mail to address when installing this script on thier site. The lines that you'll need to change look like this:

$mail_to = "zan@stargeek.com";
$mail_subject = "Contact Form";
$buisness_name = "Stargeek";

Next you can customize the html of the script to match your site's look and feel, html is contained in a few places in this script. The form used to submit messages to be emailed is contained in the last config variable $form_html, be careful not to change the form method, action or any of the input types or name unless you know what you are doing. More HTML can be found on this line:

$html = 'Thank you for contacting '.$buisness_name.','.$_POST['userName'].'.<br/>Your email was sent <br/>';

Again be careful not to change variables, just the text inside single quotes. The last bit of HTML is the wrapper inside of which the form or confirmation message is displayed. There is a place holer ">-{Title Here}-<" that should be replaced with a suitable title for the page. The HTML here can all be changed except for the <?=$html?> PHP variable.