Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Reply email to sender of form

Hopefully an easy question for someone!

     
2:50 pm on Nov 20, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts: 414
votes: 2


I've inherited a website to work on that has a number of php forms already set up. The owner wants the forms to send an autoreply saying "thanks for the message we''l be in touch soon" - standard stuff.
I've tried several coding options that I've read on this site and others but they don't seem to work. can anyone help me with this? I'm sorry I'm rubbish at php - on my long list of stuff to learn!

The code on the processor file is this:

<?

if (empty($_POST)) {
print "Error with form, please contact the webmaster.";
} else {

// Configuration Settings
$SendFrom = "Website Contact Form <info@example.co.uk>";
$SendTo = "info@example.co.uk";
$SubjectLine = "example";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value)
$MsgBody .= "$Field: $Value\n";
$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);
header("Location: $ThanksURL");
}

?>

7:51 pm on Nov 20, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Add a line, switch the to's froms:

mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);

mail($SendFrom, $SubjectLine, $MsgBody, "From: " . $SendTo);

But this drives me nuts. :-) Not picking on you, everyone does it.

header("Location: $ThanksURL");

Why can't you make your response customized and helpful? Even as simple as

header("content-type:text/html");
echo "<p>Thank you for your inquiry, an email has been sent to $SendFrom. Below are the details of your inquiry.</p>";
echo $MsgBody;
exit;

It will need some work, you might have to add HTML headers to the emails for formatted HTML email (which will make them look better,) or go with <pre> on output response, and admittedly it's not all that helpful - but it CAN be, and this would at least be a start toward eliminating a generic "thank you."

3:45 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


Thanks for the help, but it's not working, I'm sure it's my fault. As I say I've inherited this form - I usually use an auto-generated script that works just fine - but not suitable for this project.

Basically I think you're saying replace
mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom); with
mail($SendFrom, $SubjectLine, $MsgBody, "From: " . $SendTo);

That doesn't do it. I know nothing about php - what in the code is saying where the email address is that the confirmation needs to send to?

Apologies for ignorance

4:17 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Thanks for the help, but it's not working

Could you define 'not working', because it looks like it should and it's a fairly standard bit of code....

4:54 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


Well, when i complete the form and fill in my email address in the form ( a different address to the one the form is sent to) I don't get a confirmation to say i sent the email
5:04 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


It's because I didn't look at the variables close enough...
The $SendTo and $SendFrom are the same.

You will need to look at the form you are using, find the name='' field where the person enters their e-mail address, the set a variable like $CCUser=$_POST['YourFormEMailFieldNameHere'];

mail($CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);

Sorry I didn't notice before I posted the first time.

Here's what you're looking for:
<input type='text' name='YourFormEMailFieldNameHere'>

Put whatever YourFormEMailFieldNameHere is on your form between the quotes in the $_POST['YourFormEMailFieldNameHere'] on the PHP page... IOW the input field name for the e-mail and the $_POST[''] between the quotes need to match.

5:37 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


I may have misunderstood your instructions - this is what i have - the field name on the original form is "Email". I still don't receive a confirmation.
<?

if (empty($_POST)) {
print "Error with form, please contact the webmaster.";
} else {

// Configuration Settings
$SendFrom = "Website Contact Form <info@blank.co.uk>";
$SendTo = "info@blank.co.uk";
$SubjectLine = " Web Site";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value)
$MsgBody .= "$Field: $Value\n";
$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

$CCUser = $_POST ["Email"];

mail($CCUser, $SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);
header("Location: $ThanksURL");
}
?>

5:56 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


You have one too many parameters:

mail($CCUser, $SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);

This is what you are looking for:

mail($CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);

5:57 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


Thanks I'll try that
6:01 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


OOps - still not working

Did I do this right?
$CCUser = $_POST ["Email"];

6:15 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Looks good, but let's see what you're actually getting:

$SendFrom = "Website Contact Form <info@blank.co.uk>";
$SendTo = "info@blank.co.uk";
$SubjectLine = " Web Site";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value)
$MsgBody .= "$Field: $Value\n";
$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

$CCUser = $_POST ["Email"];
echo $CCUser;
exit;

If you don't see the e-mail address, something is wrong in setting the value or the form passing it, which generally the name in the POST array piece... I'd use single quotes and no space too, just to make sure there's no 'glitch' in there: $_POST['Email'];

If you want to see everything you're getting, change this line:
$MsgBody .= "$Field: $Value\n";

to

echo $MsgBody .= "$Field: $Value\n";

7:11 pm on Nov 24, 2009 (gmt 0)

Full Member

10+ Year Member

joined:June 1, 2007
posts:201
votes: 0


What about setting up a plain old autoresponder on the receiving mail server?

I also agree about just displaying a success message on the form page once it is submitted, that way you are reducing your exposure to sending backscatter and getting blacklisted in an RBL. If you are responding with the submitted contents you are leaving yourself open to those sorts of misuse.

8:13 pm on Nov 24, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 0


Auto responders are a very bad idea on many levels. I wish they would outlaw them. :-) Once a spammer finds out you have an autoresponder, they start sending crap to you that bounces back to the intended target, and it looks like it came from you. One more hole to plug . . .

Thanks for the help, but it's not working,

Sorry lol . . . yes you missed it. See my original post on page 1: what you want it two lines just like I posted.

mail (TO COMPANY, FROM SUBMITTOR);
mail (TO SUBMITTOR, FROM COMPANY);

That's really all there is to it. :-)

9:27 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Actually rocknbil,

The original coder got us both...
I missed this at first too:

$SendFrom = "Website Contact Form <info@example.co.uk>";
$SendTo = "info@example.co.uk";

They coded the to and from as the same stinking address, neither of which is the submitter of the form!

9:40 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


I did try the two lines from rocknbill originally and it didn't work. Is that because what madscientist has spotted. Bearing in mind you're dealing with a php ignoramus, can you tell me in simple words what i need to do now.

PS - I do appreciate your help here.

9:45 pm on Nov 24, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Yes, the to and the from are the same in the original code, so by switching them and sending again all you do is send a duplicate to info@example.co.uk, which does you no good and is why you need to find the e-mail address passed by the form to get the sender a copy of it.
11:16 pm on Nov 24, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


And what exactly do i need to do with the code now?
6:55 pm on Nov 25, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


You need to troubleshoot to find out if you are getting the email address from the form and the easiest way is to use the code I posted previously...

Here's the full code I would use:

<?

if (empty($_POST)) {
print "Error with form, please contact the webmaster.";
} else {

// Configuration Settings
$SendFrom = "Website Contact Form <info@blank.co.uk>";
$SendTo = "info@blank.co.uk";
$SubjectLine = " Web Site";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value) {
$MsgBody .= "$Field: $Value\n";
echo '<br>Field:'.$Field.' Value: .'$Value.'<br>';
}

$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

$CCUser = $_POST['Email'];
echo '<br><br>E-Mail Address Stored as $CCUser:'.$CCUser;
exit;

mail($CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);
header("Location: $ThanksURL");
}
?>

You should see a white screen with plain text on it.
If the line that says E-Mail Address Stored as $CCUser: is blank after the : then there is something wrong with the variable. If not then there is something wrong with your mail() send function.

10:27 pm on Nov 25, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


When I use that I get this message..
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/fhlinux174/a/etc

If I delete that line altogether I get
E-Mail Address Stored as $CCUser:Email a@myemailaddress.co.uk
but it doesn't send me the email

10:41 pm on Nov 25, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


OK, so we know you're getting the e-mail address, now, let's see if it sends:

<?

if (empty($_POST)) {
print "Error with form, please contact the webmaster.";
} else {

// Configuration Settings
$SendFrom = "Website Contact Form <info@blank.co.uk>";
$SendTo = "info@blank.co.uk";
$SubjectLine = " Web Site";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value) {
$MsgBody .= "$Field: $Value\n";
}

$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

$CCUser = $_POST['Email'];
// echo '<br><br>E-Mail Address Stored as $CCUser:'.$CCUser;

mail($CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);
mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);
header("Location: $ThanksURL");
}
?>

The error was a . and a ' in the wrong order in the line... I removed it too, because it was really just for troubleshooting.

7:36 am on Nov 26, 2009 (gmt 0)

New User

5+ Year Member

joined:Nov 26, 2009
posts:1
votes: 0


My suggestion is to try those remotely hosted contact form services. Some of them offer autoresponders in the free plan, but it helps a lot if you don't know that much programming. Have a look at Freedback and 123ContactForm.
6:14 pm on Nov 29, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


form services. Some of them offer autoresponders in the free plan, but it helps a lot if you don't know that much programming. Have a look at Freedback and 123ContactForm.

I usually use an app that creates the code for the form and processor for me. In this case that's not appropriate.

Still can't get this thing to work though!

8:26 pm on Nov 29, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Try changing these two lines:

mail($CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);
mail($SendTo, $SubjectLine, $MsgBody, "From: " . $SendFrom);

To this:
mail($SendTo.','.$CCUser, $SubjectLine, $MsgBody, "From: " . $SendFrom);

I don't see any reason why it wouldn't work the way it's written, so make sure you're using two distinct e-mail addresses, entering the e-mail address into the form correctly, and NOT getting caught in a spam filter on either of them.

The way it's written, if there's an error in the code the e-mail to the website will not be sent. You know you are getting an e-mail address posted to the form. You know the second mail is sending. The first mail() cannot have any type of critical error in it or the second mail would not be sent. The most likely conclusion I can draw is the mail() to the person completing the form (you in this case) is getting caught in the spam filter of the e-mail it's being sent to and is either in the spam box or being automatically discarded...

6:09 pm on Nov 30, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


Themadscientist.
I've made that change - I don't think it's a spam filter.
I've tried moving the whole thing on to my own website and using my own email addresses. Then when i complete the form online it sends one email to the email address i've put as $sendto.
The address I put into the form doesn't get an email. However, the email I receive is addressed to "info@domain.co.uk; Email jsmith@whatever.co.uk."
I'm interested in why it says "Email" in the middle there and if this is a clue to the problem.
8:22 pm on Nov 30, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Yeah, that's probably the issue, and this is not the 'most correct' way to fix it, the issue should be found and fixed rather than what I'm suggesting, but I've already spent quite a bit of time on this and I'm sure you've spent much more, so let's not worry about why it's happening right now and just try to get the stinking e-mail to send to both addresses. :)

I'm just going to replace the word Email in the address it's showing in with "" (nothing), rather than trying to figure out why it's there and removing it the 'more correct' way.

<?

if (empty($_POST)) {
print "Error with form, please contact the webmaster.";
} else {

// Configuration Settings
$SendFrom = "Website Contact Form <info@blank.co.uk>";
$SendTo = "info@blank.co.uk";
$SubjectLine = " Web Site";
$ThanksURL = "thankyou.html"; //confirmation page
$Divider = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Build Message Body from Web Form Input
$MsgBody = @gethostbyaddr($REMOTE_ADDR) . "\n$Divider\n";
foreach ($_POST as $Field=>$Value) {
$MsgBody .= "$Field: $Value\n";
}

$MsgBody .= $Divider . "\n" . $HTTP_USER_AGENT . "\n";
$MsgBody = htmlspecialchars($MsgBody); //make content safe

// Send E-Mail and Direct Browser to Confirmation Page

$CCUser = $_POST['Email'];

// echo '<br><br>E-Mail Address Stored as $CCUser:'.$CCUser;
// Email below should be exactly (including case) what you would like to remove from the variable...

mail($SendTo.','.str_replace('Email','',$CCUser), $SubjectLine, $MsgBody, "From: " . $SendFrom);
header("Location: $ThanksURL");
}
?>

If it's still there for some reason, remove it from both:
mail(str_replace('Email','',$SendTo).','.str_replace('Email','',$CCUser), $SubjectLine, $MsgBody, "From: " . $SendFrom);

Like I said above, this is not the 'correct' way to fix the issue, and there are some members who will probably remind you of what I'm stating too, but IMO sometimes when you're talking about the value of time this far into a project a little 'get the job done' solution you can correct later is the best answer.

8:31 pm on Nov 30, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


If neither of those work call your host and tell them the situation...
10:39 pm on Nov 30, 2009 (gmt 0)

Preferred Member

10+ Year Member Top Contributors Of The Month

joined:May 1, 2005
posts:414
votes: 2


Whoa! Success.
Many thanks for all your time - I really thought this was going to be an easy one. I Do appreciate it.
10:56 pm on Nov 30, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


I'm glad you finally got it working and you'll probably know more (or better) about the headaches possibly involved the next time you inherit a project. ;)