PHP联系表单不验证或发送 [英] PHP contact form not validating or sending
问题描述
好的...所以我从几个不同的网站和前辈的组合中抓取了这个PHP联系表单。我一直在摔跤数小时,无法弄清楚。
平心而论,知道PHP并不是我的强项,(我是客户端程序员)。以下是下面的代码,如果可以的话,请帮助。谢谢!
以下是我见过的一些错误消息:
最近一次:
*解析错误:语法错误,第11行的/ home / [...]中的意外T_IF *
是错误消息,但我相信我修复了空白字段部分:
看起来您留下了空白字段。
请确保您填写您的全名,电子邮件地址,主题和详细信息。
点击后退箭头返回联系表。
回复:...列表:语法非法收件人地址
X-Mailer:... List :;对于收件人地址非法的语法
谢谢,[姓名],联系我们!
以下是HTML :
< form action =contactus.php method =postclass =create>
< fieldset>
< legend align =center>请填写下方的详细资料并点击提交< / legend>
< div>
< label for =fullnameclass =fixedwidth>全名< / label>
< input type =textname =fullnameid =fullnameclass =input2/>
< / div>< br />
< div>
< label for =emailclass =fixedwidth>电子邮件< / label>
< input type =textname =emailid =emailclass =input2/>
< / div>< br />
< div>
< label for =subjectclass =fixedwidth> Subject< / label>
< input type =textname =subjectid =subjectclass =input2/>
< / div>< br />
< div>
< label for =detailsclass =fixedwidth> Body< / label>
< / div>
< div class =buttonarea>
< input type =submitname =submitid =submitvalue =Submit/>
< / div>
< / fieldset>
< / form>
...以下是contactus.php文件:
<?php
$全名= $ _POST ['fullname'];
$ subject = $ _POST ['subject'];
$ details = $ _POST ['details'];
$ email = $ _POST ['email'];
// ***检查电子邮件地址的函数* /
函数checkemail($ email){
$ regex ='/ ^ [_ a-z0-9 - ] + (\ [_ A-Z0-9 - ]。+)@ [A-Z0-9 - ] +(\ [A-Z0-9 - ]。+)。(\ [AZ] {2,3} )$ /'
if(eregi($ regex,$ email))
{
return true;
}
else
{
return false;
}
}
// ***检查电子邮件地址是否有效* /
else if(checkemail($ email)== false) {
echo< br />< br />< p>< center>您输入的电子邮件地址无效。< br />请再次查看您的电子邮件。 < / p>中;
}
// ***邮件处理* /
if($ _POST ['submit']){
//检查空白字段
$ if $($ fullname!==&& $ email!==&& $ subject!==&& $ amp; $ details!==){
echo < br>< br>< br />< p>< center>您似乎留下了一个空白字段< br />
请确保填写您的全名,电子邮件地址,主题和详细信息< br />
点击后退箭头返回到联系表单。< / p>;
}
// ***发送邮件** /
$ to ='sgraffito22@gmail.com';
$ fullname = $ _POST ['fullname'];
$ subject = $ _POST ['subject'];
$ details = $ _POST ['details'];
$ headers ='From:'。$ email。\r\\\
。
'回复:'。$ email。\r\\\
。
'X-Mailer:PHP /'。 phpversion();
邮件($ to,$ fullname,$ subject,$ details,$ headers);
echo< br>< br>< p>< center>谢谢您,$ fullname,联系我们!< / p>< br>< br>
}
?>
由于正则表达式解决方案已经由Sean给出, p>
这是我以前使用的项目正则表达式,
$ regex = /^[\.a-z0-9_\-]+[@][a-z0-9_\-]+([.][a-z0-9_\-]+)+[az] {1,4} $ / I;
这是PHP邮件的另一种替代方法,您需要下载phpmailer插件才能发送此邮件
<?php
//邮件配置开始
$ emailAddress = 'youremailhere';
$ replyAdress ='yourreceipientemailhere';
$ replyName ='yourreceipientname';
$ msgSubject ='yoursubjecthere';
//邮件配置结束
//包含类,
需要phpmailer / class.phpmailer.php;
//在此键入您的信息:
$ msg ='blablayourmessagehere';
//邮寄过程开始!
$ mail = new PHPMailer();
$ mail-> IsMail();
$ mail-> AddReplyTo($ replyAdress,$ replyName);
$ mail-> AddAddress($ emailAddress);
$ mail-> SetFrom($ replyAdress,$ replyName);
$ mail-> Subject =New:。$ replyName。已发送。$ msgSubject。消息;
$ mail-> MsgHTML($ msg);
$ mail->发送();
?>
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>";
}
?>
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();
?>
这篇关于PHP联系表单不验证或发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!