Reputation: 1
Ive been using a php form for years, but know very little.. which is dangerous... (It took my two days to get the Google recaptcha verification to work a few months back but the form worked correctly).
I've now notice when I get a response in the from field in outlook 2016 the senders email appears twice without any spaces in the header section.
Therefore when I reply I have the email twice in the to field.
Ive asked the hosting company but they say they cant help.
With the header in the php is as follows: Example1:
$headers = "From: $email";
$headers .= " $email";
I get the double email in the form field.
-----Original Message-----
From: [email protected] [email protected] [mailto:[email protected] [email protected]]
Sent: 08 November 2015 12:04
To: [email protected]
Subject: Contact Form
name: Mark
email: [email protected]
phone_number: 12345
message: hi
If however I change the header as per another post
example2:
$headers .= "Reply-To: $email";
$headers = "From: $email";
The form field then changes to that of my [email protected] but not twice
-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: 08 November 2015 12:00
To: [email protected]
Subject: Contact Form
name: Mark
email: [email protected]
phone_number: 12345
message: hi again
The form used to work without any issues and have not changed anything.
Could a server php update have caused my code to now work incorrectly?
All I am looking to do is be able to reply to the sender without having to remove the second email. And for the senders email address to be in the sender field and not [email protected]
Any help is welcome.
Thanks Mark
A website contact form is used to send to the php code which Ive now provided below.
<?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 = "[email protected]";
$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);
}
?>
Here is the code used on the website
<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="[email protected]" 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>
Upvotes: 0
Views: 150
Reputation: 33813
The following might be written on two lines but will, in effect, concatenate the email address to the previous email address.
$headers = "From: $email";
$headers .= " $email";/* try commenting out this line */
as is shown in
From: [email protected] [email protected] [mailto:[email protected] [email protected]]
and in the following you overwrite the header variable on the second line because you do not concatenate the strings.
$headers .= "Reply-To: $email";
$headers = "From: $email";
Try changing that to:
$headers .= "Reply-To: $email";
$headers .= "From: $email";
Based upon your original, and because it's raining here in Scotland and I'm bored, I wrote this and I hope it will help. I think one of the problems was the line endings used in the header and, as noted in a comment below, there seemed something a little odd with the syntax when accessing the return value of the captcha check.
On the page with the form to get this all to work in my tests I used:
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='[email protected]';
$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: [email protected]";
$headers[]="Bcc: [email protected]";
*/
/* 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' );
}
?>
Upon successful validation and form submission the output from the debug was:
mail( "[email protected]", "Contact Form Enquiry", "email:[email protected] name:joe bloggs phone_number:0141 353 3789 their-message:Hello, I need your help... So long and thanks for all the fish", "From: [email protected] Reply-To: [email protected]" )
What the example output doesn't show is the line-endings which are \r\n
. Incidentally, I assume that the url www.website.co.uk
is simply used as an example ~ like example.com
? If not there is a typo in the form's action.
Upvotes: 1