我多年来一直在使用php表单,但知道的很少......这很危险......(我花了两天的时间让Google重新验证验证工作几个月,但表单工作正常)。< / p>
我现在已经注意到,当我在Outlook 2016的from字段中收到回复时,发件人电子邮件出现两次,标题部分没有任何空格。
因此,当我回复时,我在电子邮件地址中有两次电子邮件。
我问托管公司,但他们说他们无法帮助。
用php中的标题如下: 例1:
$headers = "From: $email";
$headers .= " $email";
我在表单字段中收到了双重电子邮件。
-----Original Message-----
From: sendersemail@gmail.com sendersemail@gmail.com [mailto:sendersemail@gmail.com myemail@gmail.com]
Sent: 08 November 2015 12:04
To: myemail@gmail.com
Subject: Contact Form
name: Mark
email: sendersemail@gmail.com
phone_number: 12345
message: hi
但是,如果我按照另一个帖子更改标题
例2:
$headers .= "Reply-To: $email";
$headers = "From: $email";
表单字段然后更改为我的username@whm4.bargainhosts.co.uk但不是两次
-----Original Message-----
From: username@whm4.bargainhosts.co.uk [mailto:username@whm4.bargainhosts.co.uk]
Sent: 08 November 2015 12:00
To: myemail@gmail.com
Subject: Contact Form
name: Mark
email: sendersemail@gmail.com
phone_number: 12345
message: hi again
用于工作的表格没有任何问题,也没有改变任何内容。
服务器php更新是否导致我的代码现在无法正常工作?
我要做的就是能够回复发件人而无需删除第二封电子邮件。并且发件人的电子邮件地址位于发件人字段中,而不是username@whm4.bargainhosts.co.uk
欢迎任何帮助。
谢谢Mark
网站联系表单用于发送到我现在提供的php代码。
<?php
$email = $_POST['email'];
$after = "http://www.website.co.uk/thankyou.htm";
$oops = "http://www.website.co.uk/error.htm";
if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") {
exit("<p>You did not press the submit button; this page should not be accessed directly.</p>");
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">";
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=##########secretcode#########&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">";
}else
{
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">";
}
if( isset($_POST['submit'])) {
$recipient = "myemail@gmail.com";
$title = "Contact Form";
$message .= "name: {$_POST['name']} \n";
$message .= "email: {$_POST['email']} \n";
$message .= "phone_number: {$_POST['phone_number']} \n";
$message .= "their-message: {$_POST['their-message']} \n";
$headers .= "Reply-To: $email";
$headers .= "From: $email";
mail($recipient,$title,$message,$headers);
}
?>
以下是网站上使用的代码
<div id="contact_form">
<form name="form" id="ff" method="post" action="http://www.webwsite.co.uk/c/form2015.php">
<legend>Please help us by completing all fields</legend>
<br>
<label> <span>Name*:</span><br>
<input type="text" placeholder="Please enter your name" name="name" id="name" required>
</label>
<label> <span>Email*:</span><br>
<input type="email" placeholder="youremail@gmail.com" name="email" id="email" required>
</label>
<label> <span>Phone:</span><br>
<input type="phone_number" placeholder="Please enter your phone" name="phone_number" id="phone_number">
</label>
<label> <span>Message*:</span><br>
<textarea name="their-message" rows="5" required id="their-message" placeholder="Please enter your message"></textarea>
</label>
<div class="g-recaptcha" data-sitekey="################################################"></div>
<br />
<input name="Reset" id="reset" type="reset" class="reset" value="Reset" />
<input name="submit" type="submit" class="submit" value="Send" />
</form>
</div>
答案 0 :(得分:1)
以下内容可能会写在两行,但实际上会将电子邮件地址连接到以前的电子邮件地址。
$headers = "From: $email";
$headers .= " $email";/* try commenting out this line */
如
所示From: myemail@gmail.com myemail@gmail.com [mailto:myemail@gmail.com myemail@gmail.com]
并在下面覆盖第二行的头变量,因为你没有连接字符串。
$headers .= "Reply-To: $email";
$headers = "From: $email";
尝试将其更改为:
$headers .= "Reply-To: $email";
$headers .= "From: $email";
基于你原来的,因为它在苏格兰下雨,我感到无聊,我写了这篇文章,我希望它会有所帮助。我认为其中一个问题是标题中使用的行结尾,如下面的评论中所述,在访问验证码检查的返回值时,语法似乎有点奇怪。
在包含表单的页面上,我使用了以下所有测试:
head
----
<script type='text/javascript'>
var key='<?php echo GOOGLE_RECAPTCHA_KEY; ?>';
/* this is the public key */
function verifyCaptcha(){
grecaptcha.render( 'grc', {
'sitekey' : key,
'theme' : 'light',
'size' : 'compact'
});
}
</script>
<script src='https://www.google.com/recaptcha/api.js?onload=verifyCaptcha&render=explicit' async defer></script>
body
----
<form name='mailtest' method='post' action='/test/target.php'>
<h1>Google reCaptcha form mail test</h1>
<input type='text' name='name' placeholder='Please enter your name' required />
<input type='text' name='phone_number' placeholder='Please enter your phone' required />
<input type='text' name='their-message' placeholder='Please enter your message' required />
<input type='email' name='email' placeholder='Please enter your email address' required />
<!-- empty placeholder for re-captcha -->
<div id='grc'></div>
<!-- A new form element will be created by the recaptcha if successful -->
<!--
g-recaptcha-response
-->
<input type="submit" value="Send email" />
</form>
form action script
------------------
<?php
$after = "http://www.website.co.uk/thankyou.htm";
$oops = "http://www.website.co.uk/error.htm";
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* Config: edit these to suit */
$recipient='myemail@gmail.com';
$subject='Contact Form Enquiry';
$google_secret='abc-def-123-456xxxxxxxxx';
/* Container in which to build message */
$message=array();
$headers=array();
/* Fields of relevance to the email */
$fields=array( 'email', 'submit', 'name', 'phone_number', 'their-message' );
foreach( $fields as $field ){
/* Assign as a variable variable */
$$field=( isset( $_POST[ $field ] ) && !empty( $_POST[ $field ] ) ) ? trim( filter_input( INPUT_POST, $field, FILTER_SANITIZE_STRING ) ) : false;
/* Add to message */
if( $$field ) $message[]="{$field}: {$$field}";
}
/* Add elements to the headers array */
$headers[]="From: {$email}";
$headers[]="Reply-To: {$email}";
/*
These are pertinent to Outlook so could be set too
--------------------------------------------------
$headers[]="Importance: Normal";
$headers[]="Sensitivity: Private";
$headers[]="Priority: Urgent";
$headers[]="Comments: an email from x";
$headers[]="Keywords: x,y,z";
$headers[]="Cc: user@example.com";
$headers[]="Bcc: user@example.com";
*/
/* Get submitted captcha image data */
$captcha=isset( $_POST['g-recaptcha-response'] ) && !empty( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : false;
if( !!$captcha===false ) header( 'location: '.$oops.'?captcha=empty' );
$response=file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=".$google_secret."&response=".trim( $captcha )."&remoteip=".$_SERVER['REMOTE_ADDR'] );
/* decode */
$response=json_decode( $response );
/* The syntax you had seemed incorrect as it used a dot notation */
if( $response->success ){
/* To debug output and expected mail send parameters */
exit("mail( \"$recipient\", \"$subject\", \"".implode( PHP_EOL, $message )."\", \"".implode( "\r\n", $headers )."\" )");
/* Send the mail*/
$res=@mail( $recipient, $subject, implode( "\n", $message ), implode( "\r\n", $headers ) );
/* Redirect based upon mail sending success / failure */
if( $res ) header( 'location: '.$after.'?mailsent=true' );
else header( 'location: '.$oops.'?mailsent=false' );
} else {
/* Get the reason straight from the horses mouth */
$reason=implode('_',$response->{'error-codes'});
/* Redirect because capture failed */
header( 'location: '.$oops.'?captcha=failed&reason='.$reason );
}
} else {
/* Redirect - incorrect method */
header( 'location: '.$oops.'?method=bad' );
}
?>
成功验证和表单提交后,调试的输出为:
邮件(&#34; myemail@gmail.com",&#34;联系表格查询&#34;, &#34;电子邮件:joe.bloggs@example.com名称:joe bloggs phone_number:0141 353 3789他们的消息:您好,我需要您的帮助......这么久,谢谢 所有的鱼&#34;,&#34;来自:joe.bloggs@example.com回复: joe.bloggs@example.com" )
示例输出未显示的是{-1}}的行尾。顺便提一下,我假设网址\r\n
只是用作例{〜www.website.co.uk
?如果没有,表格的行动就会出现拼写错误。