PHP联系表单未验证或发送

时间:2012-08-29 01:55:38

标签: php html email-validation contact-form

好的......所以我从几个不同网站和前任网站的组合中抓取了这个PHP联系表单。我已经和它搏斗了好几个小时,无法弄明白。

公平地说,知道PHP根本不是我的强项,(我是客户端程序员)。这是下面的代码,如果可以请帮助。谢谢!

以下是我看过的一些错误消息:

最近的: *解析错误:语法错误,第11行的/ home / [...]中的意外T_IF *

之前,这是错误信息,但我相信我修复了“空白字段”部分:

您似乎留下了一个空白字段。

请确保填写您的全名,电子邮件地址,主题和详细信息。 单击后退箭头返回联系表单。

来自:...列表:;收件人地址的语法非法

回复:...列表:;收件人地址的语法非法

X-Mailer:...列表:;收件人地址的语法非法

感谢您,[姓名],联系我们!

这是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>

...这里是contactus.php文件:

<?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>";

}

?> 

2 个答案:

答案 0 :(得分:0)

您在

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

From Reply-to和X-Mailer之间切换可能导致'"$headers语法错误>

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

尝试更改为 -

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

答案 1 :(得分:0)

由于Sean已经提供了正则表达式解决方案,

这是我之前使用的项目正则表达式,

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

这是PHP邮件的另一种选择,你需要下载phpmailer插件才能使这个邮件发挥作用。

<?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();

?>