Form to Email

the mail() function

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Fri May 27, 2011 8:14 pm

Form to Email

Postby steiger16 » Fri May 27, 2011 8:21 pm

Hello php experts! I have a form on a website that gets submitted to my email for a contest. It is currently returning the Full Name field and the Your Email field in the email it generates but I cannot get it to also return the Coupon Name and the Dog Name fields. Any help would be appreciated. I also want to add captcha to this to prevent spam. Code is below and the site for the form is at Thanks!

HTML Form Code

Code: Select all

<form action="contestmailer.php" method="post" id="contactform">
              <label for="name">Full Name <span class="red">(required)</span></label>
              <input id="name" name="name" class="text" />
              <label for="email">Your email <span class="red">(required)</span></label>
              <input id="email" name="email" class="text" />
              <label for="coupon">Coupon Name <span class="red">(required)</span></label>
              <input id="coupon" name="coupon" class="text" />
              <label for="dog">Dog Name <span class="red">(required)</span></label>
              <input id="dog" name="dog" class="text" />
            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
              <div class="clr">By submitting the form you agree to the <a href="Character Contest Rules.pdf">Contest Rules</a></div>

PHP mailer code

Code: Select all


if(!$_POST) exit;

$email_field = $_POST['email'];
$name_field = $_POST['name'];
$coupon_field = $_POST['coupon'];
$dog_field = $_POST['dog'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email_field )){
   $error.="Invalid email address entered";
if($errors==1) echo $error;
   $values = array ('name','email','coupon','dog');
   $required = array('name','email');
   $your_email = "";
   $email_subject = "June Contest Entry: ";
   $email_content = "Contest Entry:\n Name: $name_field\n Email: $email_field\n Coupon Name: $coupon_field\n Dog Name: $dog_field\n";
   foreach($values as $key => $valuse){
      if ($key != 'dog' && $key != 'coupon') {
        if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
      $email_content .= $values.': '.$_POST[$coupon_field]."\n";
   if(@mail($your_email,$email_subject,$email_content)) {
      echo 'Message sent!';
   } else {
      echo 'ERROR!';

User avatar
New php-forum User
New php-forum User
Posts: 84
Joined: Sat May 21, 2011 10:26 pm

Re: Form to Email

Postby webinfopedia » Sun May 29, 2011 6:57 pm

I have explained the PHP email function in below article.
Hope that it will help you. If any doubt let us know

Thank you

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 3 guests