Forum Moderators: coopster
for some reason my phpmail form is not processing, I have an else statement at the bottom of the code to output an error message if mail not sent and I keep getting that message.. PLEASE HELP! lol
here is the code:
<?php
// your name
$recipientname = "company";
// your email
$recipientemail = "d#*$!.com";
// subject of the email sent to you
$subject = "Online-Form Response";
if(isset($_POST['submit'])){
$Name = $HTTP_POST_VARS['Name'];
$adr = $HTTP_POST_VARS['adr'];
$adr2 = $HTTP_POST_VARS['adr2'];
$adr3 = $HTTP_POST_VARS['adr3'];
$city = $HTTP_POST_VARS['city'];
$zip = $HTTP_POST_VARS['zip'];
$phone = $HTTP_POST_VARS['phone'];
$email1 = $HTTP_POST_VARS['email1'];
$creditcardnum = $HTTP_POST_VARS['creditcardnum'];
$cvc = $HTTP_POST_VARS['cvc'];
$note = $HTTP_POST_VARS['note'];
$to = "email";
$subject ="Online form";
$name = $_POST['name'];
$email = $_POST['email1'];
//$company = $_POST['company'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$option = $_POST['method'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$similar_sites = $_POST['similar_sites'];
$description = $_POST['description'];
$headers = 'From: email.com' . "\r\n" .
'Reply-To: email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSmail-Priority: Normal\n";
$headers .= "X-mailer: php\n";
/*
foreach($_POST['annual'] as $method){
$check_msg .= "Contact Method: $method\n";
}
*/
// check the check boxes
foreach($_POST['checkbox'] as $value){
$check_msg .= "Checked: $value\n\r";
}
// $body = "Name: $name\n Address: $adr\nAddress2: $adr2\n Address3: $adr3\n City: $city\n State: $state\n zip: $zip\n Phone: $phone\n Email: $email1\n Credit Card Number: $creditcardnum\n Exp Month. :expmo$\n Exp Year: $expyr\n $check_msg Option: $budget\n Timeframe: $timeframe\n sites: $similar_sites\n Message: $note\n";
mail($to,$subject,$body);
} else {
echo "We can Not Process your request at this time. ";
}
?>
btw I did try this:
$to = // put in your email address
$send = mail($to, 'TEST', 'Is this thing working?', 'From: chimp@example.com'."\r\n");
if ($send) {
echo 'mail is working';
}
else {
echo 'mail not working';
} this worked.. so the mail settings works.. something whith my script.
HTTP_POST_VARS [php.net]
Change all those to $_POST['varname'] and that part should be good.
Yes, headers are optional in PHP, but some mail server's won't send a mail with an invalid "from." Using a no-headers method causes a mail to come from "root@example.com" or "apache@example.com."
You've already composed headers there, use them.
Also add this, which is a little better way of trapping the mail failure:
if (mail($to,$subject,$body,$headers)) {
echo "Success";
}
else {
echo "Mail did not send."
}
The only other thing I see is you need a valid email address, guessing you just did this for a public post:
$to = "email";
S/B
$to = "email@example.com"; // or whatever
The last think I'll add, which you probably won't like, this script is horribly vulnerable to injection, I don't see any cleansing of the input being done anywhere. Project for another day, I guess . . .
<?php
// your name
$recipientname = "company";
// your email
$recipientemail = "example@example.com";
// subject of the email sent to you
$subject = "Online-Form Response";
if(isset($_POST['submit'])){
$Name = $_POST['Name'];
$adr = $_POST['adr'];
$adr2 = $_POST['adr2'];
$adr3 = $_POST['adr3'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email1 = $_POST['email1'];
$creditcardnum = $_POST['creditcardnum'];
$cvc = $_POST['cvc'];
$note = $_POST['comment'];
$to = "example@example.com";
$subject ="Online form";
$name = $_POST['name'];
$email = $_POST['email1'];
//$company = $_POST['company'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$option = $_POST['method'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$similar_sites = $_POST['similar_sites'];
$description = $_POST['description'];
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: example@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSmail-Priority: Normal\n";
$headers .= "X-mailer: php\n";
/*
foreach($_POST['annual'] as $method){
$check_msg .= "Contact Method: $method\n";
}
*/
// check the check boxes
foreach($_POST['checkbox'] as $value){
$check_msg .= "Checked: $value\n\r";
}
// $body = "Name: $name\n Address: $adr\nAddress2: $adr2\n Address3: $adr3\n City: $city\n State: $state\n zip: $zip\n Phone: $phone\n Email: $email1\n Credit Card Number: $creditcardnum\n Exp Month. :expmo$\n Exp Year: $expyr\n $check_msg Option: $budget\n Timeframe: $timeframe\n sites: $similar_sites\n Message: $note\n";
mail($to,$subject,$body,$headers);
} else {
echo "We can Not Process your request at this time. ";
}
?>
So:
if (mail($to,$subject,$body,$headers)) {
echo "Success";
}
else {
echo "Mail did not send.";
} And you should be good.
[EDIT]
I just noticed this on the code you posted above:
mail($to,$subject,$body,$headers);
} else {
echo "We can Not Process your request at this time. ";
} You have 2 closers } but only one opener {
You also need "if(" - and to double close the brackets at the end of the if argument there, and to do away with the semi colon as the brackets close
-----
Also, as Rocknbil pointed out above, you're vulnerable to to an injection attack on your database. Change everywhere you have something like this:
$_POST['Name'] to this:
mysql_real_escape_string($_POST['Name']) To afford yourself some security.
Thanks for your answer... I still can't get it to worl grrrr so annoying.... Please let me know what I am doing wrong.. btw this form is not connected to a database.
Here is the code:
<?php
// your name
$recipientname = "company";
// your email
$recipientemail = "example@example.com";
// subject of the email sent to you
$subject = "Online-Form Response";
if(isset($_POST['submit'])){
$Name = $_POST['Name'];
$adr = $_POST['adr'];
$adr2 = $_POST['adr2'];
$adr3 = $_POST['adr3'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email1 = $_POST['email1'];
$creditcardnum = $_POST['creditcardnum'];
$cvc = $_POST['cvc'];
$note = $_POST['comment'];
$to = "example@example.com";
$subject ="Online form";
$name = $_POST['name'];
$email = $_POST['email1'];
//$company = $_POST['company'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$option = $_POST['method'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$similar_sites = $_POST['similar_sites'];
$description = $_POST['description'];
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: example@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSmail-Priority: Normal\n";
$headers .= "X-mailer: php\n";
// check the check boxes
foreach($_POST['checkbox'] as $value){
$check_msg .= "Checked: $value\n\r";
}
$body = "Name: $name\n Address: $adr\nAddress2: $adr2\n Address3: $adr3\n City: $city\n State: $state\n zip: $zip\n Phone: $phone\n Email: $email1\n Credit Card Number: $creditcardnum\n Exp Month. :expmo$\n Exp Year: $expyr\n $check_msg Option: $budget\n Timeframe: $timeframe\n sites: $similar_sites\n Message: $note\n";
if (mail($to,$subject,$body,$headers)) {
echo "sent!";
}else{
echo "Mail did not send.";
?>
if(isset($_POST['submit'])){
$Name = $_POST['Name'];
$adr = $_POST['adr'];
$adr2 = $_POST['adr2'];
$adr3 = $_POST['adr3'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email1 = $_POST['email1'];
$creditcardnum = $_POST['creditcardnum'];
$cvc = $_POST['cvc'];
$note = $_POST['comment']; Is not closed, you need to add a } at the end there. That might fix it, I'll take a look for more mistakes
-----
foreach($_POST['checkbox'] as $value){
$check_msg .= "Checked: $value\n\r"; it's not closed
-----
if (mail($to,$subject,$body,$headers)) {
echo "sent!";
}else{
echo "Mail did not send."; still not closed. If you have a { you need to have a matching } somewhere - PhP is very unforgiving with syntax errors
[edited by: Readie at 12:19 am (utc) on Jan. 13, 2010]
<?php
// your name
$recipientname = "company";
// your email
$recipientemail = "example@example.com";
// subject of the email sent to you
$subject = "Online-Form Response";
if(isset($_POST['submit'])){
$Name = $_POST['Name'];
$adr = $_POST['adr'];
$adr2 = $_POST['adr2'];
$adr3 = $_POST['adr3'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email1 = $_POST['email1'];
$creditcardnum = $_POST['creditcardnum'];
$cvc = $_POST['cvc'];
$note = $_POST['comment'];
$to = "example@example.com";
$subject ="Online form";
$name = $_POST['name'];
$email = $_POST['email1'];
//$company = $_POST['company'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];
$option = $_POST['method'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$similar_sites = $_POST['similar_sites'];
$description = $_POST['description'];
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: example@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-MSmail-Priority: Normal\n";
$headers .= "X-mailer: php\n";
}
// check the check boxes
foreach($_POST['checkbox'] as $value){
$check_msg .= "Checked: $value\n\r";
}
$body = "Name: $name\n Address: $adr\nAddress2: $adr2\n Address3: $adr3\n City: $city\n State: $state\n zip: $zip\n Phone: $phone\n Email: $email1\n Credit Card Number: $creditcardnum\n Exp Month. :expmo$\n Exp Year: $expyr\n $check_msg Option: $budget\n Timeframe: $timeframe\n sites: $similar_sites\n Message: $note\n";
if (mail($to,$subject,$body,$headers)) {
echo "sent!";
}else{
echo "Mail did not send.";
}
?>
It looks like it could be in the HTML...
I would also look closely at the $_POST['submit'].
The first thing I would do is add these lines:
if(isset($_POST['submit'])){
echo 'Here, Passed Submit Check';
exit();
If you get the echo then start echoing out the variables you need to send the mail and make sure they are being set correctly...
If you can't get it working, please either post the HTML or you can send it to me if you like and I'll have a look at it... Just so I know, are you posting an array() for the checkboxes and are you double checking you are getting the information? print_r($_POST['checkbox']); It doesn't seem to have anything to do with your current issue, but would be good to check and make sure it's getting there... I've had an issue with certain versions of PHP not interpreting POSTed arrays correctly.