PHP contact form not validating or sending

谁说胖子不能爱 提交于 2020-01-15 11:58:10

问题


Ok...so I grabbed this PHP contact form from a combination of a few different websites and from a predecessor. I've been wrestling with it for hours and can't figure it out.

In all fairness, know that PHP is not my strong suit, at all, (I'm a client-side programmer). Here's the code below, please help if you can. Thank you!

Here are some of the error messages I've seen:

The most recent: *Parse error: syntax error, unexpected T_IF in /home/[...] on line 11*

Before, here was the error message, but I believe I fixed the "blank field" part:

It appears that you left a blank field.

Please make sure you fill in your full name, email address, subject, and details. Click the back arrow to return to the contact form.

From:... List:; syntax illegal for recipient addresses

Reply-To:... List:; syntax illegal for recipient addresses

X-Mailer:... List:; syntax illegal for recipient addresses

Thank you, [name], for contacting us!

Here's the HTML

<form action="contactus.php" method="post" class="create">
     <fieldset>
    <legend align="center">Please fill out details below and click "Submit"</legend>
    <div>
     <label for="fullname" class="fixedwidth">Full Name</label>
     <input type="text" name="fullname" id="fullname" class="input2"/>
    </div><br/>
    <div>
     <label for="email" class="fixedwidth">Email</label>
     <input type="text" name="email" id="email" class="input2"/>
    </div><br/>
      <div>
     <label for="subject" class="fixedwidth">Subject</label>
     <input type="text" name="subject" id="subject" class="input2"/>
    </div><br/>
    <div>
    <label for="details" class="fixedwidth">Body</label>
     <textarea id="details" name="details" cols="62" rows="20"></textarea>
    </div>
    <div class="buttonarea">
        <input type="submit" name="submit" id="submit" value="Submit"/>
    </div>

    </fieldset>
   </form>

...and here's the contactus.php file:

<?php

 $fullname = $_POST['fullname'];
 $subject = $_POST['subject'];
 $details = $_POST['details'];
 $email = $_POST['email'];

//*** Function to check email address */
 function checkemail($email) { 
  $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$/'
   if (eregi($regex ,$email))
   { 
    return true; 
   } 
    else
   { 
    return false; 
   } 
 }

//*** Check to see if the email address is valid */
 else if (checkemail($email) == false) { 
  echo "<br/><br/><p><center>It appears that you have entered an invalid email address.<br/> Please check your email again.</p>";
 }

//*** Mail Processing */
 if ($_POST['submit']) {
 //Check for blank fields
  if ($fullname !== "" && $email !== "" && $subject !== "" && $details !== "") {
   echo "<br/><br/><p><center>It appears that you left a blank field.<br/> 
   Please make sure you fill in your full name, email address, subject, and details.<br/>
   Click the back arrow to return to the contact form.</p>";
  }

//*** Send Mail**/
 $to = 'sgraffito22@gmail.com';
 $fullname = $_POST['fullname'];
 $subject = $_POST['subject'];
 $details = $_POST['details'];

 $headers = 'From: '.$email."\r\n".
 'Reply-To: '.$email."\r\n" .
 'X-Mailer: PHP/' . phpversion();

 mail($to, $fullname, $subject, $details, $headers);
 echo "<br><br><p><center>Thank you, $fullname, for contacting us!</p><br><br>";

}

?> 

回答1:


You are missing a ; at the end of

$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$/'

Your From, Reply-to, and X-Mailer syntax errors could be caused by switching between ' and " in $headers

$headers = 'From: '.$email."\r\n".
  'Reply-To: '.$email."\r\n" .
   'X-Mailer: PHP/' . phpversion(); 

try changing to -

$headers  = "From: ".$email."\r\n";
$headers .= "Reply-To: ".$email."\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();



回答2:


Since regex solution has been given by Sean,

Here is my previous project regex I used,

$regex= "/^[\.a-z0-9_\-]+[@][a-z0-9_\-]+([.][a-z0-9_\-]+)+[a-z]{1,4}$/i";

This is another alternative for PHP mailing, you will need to download phpmailer plugin in order for this mailing to work.

<?php
// mail config start 

$emailAddress = 'youremailhere';
$replyAdress ='yourreceipientemailhere';
$replyName = 'yourreceipientname';
$msgSubject= 'yoursubjecthere';


// mail config end 

// Include the class,
require "phpmailer/class.phpmailer.php";

//Type your message here:
$msg = 'blablayourmessagehere';

//The mailing process begins!!
$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($replyAdress, $replyName);
$mail->AddAddress($emailAddress);
$mail->SetFrom($replyAdress, $replyName);
$mail->Subject = "New: ".$replyName." have sent a ".$msgSubject." message";

$mail->MsgHTML($msg);

$mail->Send();

?>


来源:https://stackoverflow.com/questions/12169912/php-contact-form-not-validating-or-sending

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!