[Tutorial] Contact form using php

Example of PHP code.

Moderator: Moderators

[Tutorial] Contact form using php

Postby DooBDee on Sun Mar 27, 2005 10:50 pm

Hi all.

I would firstly like to point out, that i did not make this code / script at all - but was infact made by "Useless", a coder that belongs to phpBBstyles that is infact "usefull".

As many people have asked this question, including myself, i thought that i would post it up (courtesy of "useless"


Code: Select all
<?php
// Form Emailer

// Specify the address you want the email sent to below, along with a suitable subject.
$mail_to = 'feedback@example.com';
$mail_subject = 'Site Feedback';

// Page to be redirected to if the mail is sent OK.
$page_sentok = 'http://www.example.com/thankyou.html';

// The following 3 lines figure out the name of the file this script is in as it is
// used in the form later on in the script... therefore it calls itself.. ;)
//
$filepath = $_SERVER['PATH_TRANSLATED'];
$pathparts = pathinfo($filepath);
$scriptname = $pathparts['basename'];

if( isset($_POST['submit']) )
{
   if( !empty($_POST['mail_name']) && !empty($_POST['mail_from'])  && !empty($_POST['mail_message']) )
   {
      $mail_name = stripslashes($_POST['mail_name']);
      $mail_from = stripslashes($_POST['mail_from']);
      $mail_message = stripslashes($_POST['mail_message']);

      $headers  = "MIME-Version: 1.0\r\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
      
      $headers .= "From: " . $mail_name . " <" . $mail_from . ">\r\n";
      $headers .= "Reply-To: " . $mail_from . "\r\n";

      $result   = mail($mail_to, $mail_subject, $mail_message, $headers );
      
      if($result)
      {
         // email sent ok, redirect to specified page
         header( "Location: $page_sentok" );
         exit;
      }
      else
      {?>
      
      <html><head>
      <title>Error Sending Email</title>
      </head>
      
      <body>
      <h3>Error Sending Email</h3>
      </body>
      </html>
<?php
      exit;
      }
   }
}
else
{
?>
<html>
<head>
<title>Feedback Form</title>
<script language='JavaScript' type='text/javascript'>
<!--
var f_name, f_email, f_msg;

function processform(thisform) {
   f_name = thisform.mail_name.value;
   f_email = thisform.mail_from.value;
   f_msg = thisform.mail_message.value;

   reqField = "Please enter your name, email address and a message" + "\n";
   reqField = reqField + "before clicking the Submit Button.";

   if ((f_name == null || f_name == "") && (f_email == null || f_email == "") && (f_msg == null || f_msg == ""))
   {
      msg = reqField;

      alert(msg);

      thisform.mail_name.focus();
      thisform.mail_name.select();
      return false;

   }

}
-->
</script>
</head>

<body>
   <table width="100%" border="0" cellspacing="0" cellpadding="0">
   <tr>
   <td valign='middle' align='center'>
      <table width='100%' cellspacing='0' cellpadding='2' border='0'>
      <tr><td align='center' height='25'>
         Feedback Form.
      </td></tr>
      </table>
   </td>
   </tr>
   </table>

   <table class='tableline' width='100%' cellpadding='2' cellspacing='1' border='0'>
   <tr><td align='left' valign='middle'>

   <form method="post" action="<?php echo $scriptname; ?>">
   
      <table align='center' cellpadding='3' cellspacing='2' border='0'>
      <tr><td align='right' width='150'>Your Name:&nbsp;</td>
      <td align='left'><INPUT TYPE='TEXT' NAME='mail_name' SIZE='30'>
      </td></tr>

      <tr><td align='right' width='150'>Your Email:&nbsp;</td>
      <td align='left'><INPUT TYPE='TEXT' NAME='mail_from' SIZE='60'>
      </td></tr>
   
      <tr><td align='right' valign='top'>Message:&nbsp;</td>
      <td align='left' valign='top'><textarea class='post' name='mail_message' cols='46' rows='5'></textarea>
      </td></tr>
   
      <tr><td>&nbsp;</td>
      <td align='center'>

         <table width='100%' align='center' cellpadding='0' cellspacing='0' border='0'>
         <tr><td align='right'><input type='submit' name='submit' value='Submit' ONCLICK='return processform(this.form);'>&nbsp;&nbsp;</td>
         <td>&nbsp;&nbsp;<input type='reset' name='reset' value='Reset'></td>
         </tr>
         </table>
   
      </td></tr>
      </table>
   </form>

   </td></tr>
   </table>
<?php
}
?>


Thanks

And thankyou useless.
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby DooBDee on Sun Apr 03, 2005 8:45 pm

Greetings all.

Another way of a contact form to be handled:

CREATE A NORMAL FORM as shown below:

Code: Select all
<form action="processform.php" method="post">
<p><div class="postbody">Your Name: </div><input type="text" name="name" /></p>
<p><div class="postbody">Your Email: </div><input type="text" name="email" /></p>
<p><div class="postbody">Subject: </div><input type="text" name="subject" /></p>
<p><div class="postbody">Message: </div><textarea rows="7" name="message" cols="40"></textarea></p>
<p><input type="submit" /></p>
</form>


Then create processform.php containing the following:

Code: Select all
<p>Hello, <b><?php echo $_POST['name']; ?></b> and thankyou for your enquiry<br>
A message has been sent from <b><?php echo $_POST['email']; ?></b> to contact@yourdomain.com</p>
<br>
<p><u>Subject</u><input type="text" name="subject" size="42" value="<?php echo $_POST['subject']; ?>"></p>
<br>
<p><u>Message</u><textarea rows="4" name="message" cols="49"><?php echo $_POST['message']; ?></textarea></p>
<?php
if(!empty($_POST['message'])) 

mail('contact@yourdomain.com', stripslashes($_POST['subject']), stripslashes($_POST['message']), 'From: ' . stripslashes($_POST['name']) . ' <' . stripslashes($_POST['email']) . '>'); 
}
?>
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby DooBDee on Sat Jul 09, 2005 5:41 pm

Greetings,

Second script updated to version 2.0.

Full package download, available from http://www.doobdee.net/site/viewpage.php?p=4

Code: Select all
<?php
/*   Title: contact.php
*   Version: 2.0.0
*   Author: DooBDee
*   Website: http://www.doobdee.net
*   Description: Makes fields required, and email validation.  Once valid, script will mail form
*/


//   Your email
$contact_email = 'your@domain.com';

$name = isset($_POST['name']) ? stripslashes($_POST['name']) : ''; 
$email = isset($_POST['email']) ? stripslashes($_POST['email']) : ''; 
$subject = isset($_POST['subject']) ? stripslashes($_POST['subject']) : ''; 
$message = isset($_POST['message']) ? stripslashes($_POST['message']) : ''; 

$required = array("name"  => "Your Name",
                  "email" => "Email Address",
                  "subject" => "Your Subject",
                  "message" => "Your Message",

);

foreach($required as $field => $label)
{
   if (!$_POST[$field])
{
   $warnings[$field] = "Required";
   }
}

if ($_POST["email"] &&
   !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"]))
   $warnings["email"] = "Invalid email";

if (count($warnings) > 0) {

?>

<FORM ACTION="register.php" METHOD=POST>
<TABLE BORDER=0>
<TR>
   <TD>Your Name</TD>
     <TD><INPUT TYPE=TEXT SIZE=30 NAME="name" VALUE="<?php echo $_POST["name"];?>"></TD>
     <TD><?php echo $warnings["name"];?></TD>
</TR>
<TR>
   <TD>Email Address</TD>
   <TD><INPUT TYPE=TEXT SIZE=30 NAME="email" VALUE="<?php echo $_POST["email"];?>"></TD>
   <TD><?php echo $warnings["email"];?></TD>
</TR>
<TR>
   <TD>Subject</TD>
     <TD><INPUT TYPE=TEXT SIZE=30 NAME="subject" VALUE="<?php echo $_POST["subject"];?>"></TD>
     <TD><?php echo $warnings["subject"];?></TD>
</TR>
<TR>
   <TD>Message</TD>
     <TD><INPUT TYPE=TEXT SIZE=30 NAME="message" VALUE="<?php echo $_POST["message"];?>"></TD>
     <TD><?php echo $warnings["message"];?></TD>
</TR>
</TABLE>
<INPUT TYPE=SUBMIT VALUE="Register">
</FORM>

<?php
}
else
{
mail ($contact_email, $subject, "Name: $name\nEmail: $email\nSubject: $subject\nMessage: $message", "FROM: {$email}");

}
?>
DooBDee
Website Ninja
DooBDee.net
User avatar
DooBDee
Moderator
Moderator
 
Posts: 3530
Joined: Tue Mar 01, 2005 6:28 pm
Location: DooBDee.net

Postby internetbrother on Sat Oct 22, 2005 8:19 pm

I found this script , which i need but does any body know how I can make it send the email in HTML :cry:

please see the code

Code: Select all
<?php

$mail         = htmlspecialchars($mail);
$title        = "Visit our site";
$Email        = "webmaster@blabla.com";
$usrnme       = htmlspecialchars($usrnme);
$invnme       = htmlspecialchars($invnme);
$message      = "

Hi $invnme ,

Your friend $usrnme has invited you to join our site at BlaBla.com";

$Parms = "&mail=$mail&Email=$Email&title=$title";

$Parms = "&Validity=Invalid&Action=$Action";

if ($Email != "") mail($mail, $title, $message, "From: $Email");

?>

I need to add the follwoing as well to solve the language view :(
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
internetbrother
Registered User
Registered User
 
Posts: 252
Joined: Sat Oct 15, 2005 10:36 am


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron