homepage Welcome to WebmasterWorld Guest from 54.205.188.59
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP MAILFORM wont process
php,mail,form, process,send, can not,server,contact,feedback
andy106




msg:4059315
 8:25 pm on Jan 12, 2010 (gmt 0)

Hi everyone;

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.

 

rocknbil




msg:4059371
 9:58 pm on Jan 12, 2010 (gmt 0)

The problem really is your aren't using the mail headers you create. Compare your "this worked" against your script. But an important note, and easy to fix, http_post_vars is long deprecated.

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 . . .

andy106




msg:4059399
 10:29 pm on Jan 12, 2010 (gmt 0)

Hi Thanks for your help, but still gives me the same error here is the new updated 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";

/*
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. ";
}

?>

andy106




msg:4059404
 10:31 pm on Jan 12, 2010 (gmt 0)

when I add this if (mail($to,$subject,$body,$headers)) {
echo "Success";
}
else {
echo "Mail did not send."
} i got an error on line 68

Readie




msg:4059432
 11:14 pm on Jan 12, 2010 (gmt 0)

You need a semi colon ; after echo "mail did not send."

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.

andy106




msg:4059463
 11:46 pm on Jan 12, 2010 (gmt 0)

Hi Readie,

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.";

?>

andy106




msg:4059464
 11:47 pm on Jan 12, 2010 (gmt 0)

I did add the } at the end:
if (mail($to,$subject,$body,$headers)) {
echo "sent!";
}else{
echo "Mail did not send.";
}

Readie




msg:4059465
 11:48 pm on Jan 12, 2010 (gmt 0)

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]

andy106




msg:4059466
 11:51 pm on Jan 12, 2010 (gmt 0)

I closed is right after the $headers:
'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";
} <-- closed

Readie




msg:4059467
 11:53 pm on Jan 12, 2010 (gmt 0)

please see my edits :)
Also, it is not closed on the code you posted above

andy106




msg:4059476
 12:08 am on Jan 13, 2010 (gmt 0)

Thanks for all your help.. if you can get this form to work for me I will gladly compensate you... it's still not working:

<?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.";

}

?>

andy106




msg:4059478
 12:10 am on Jan 13, 2010 (gmt 0)

let me know and I will send you the html too... I need it like asap though.

Thanks.

TheMadScientist




msg:4059534
 1:38 am on Jan 13, 2010 (gmt 0)

Are you still needing help with this?

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.

andy106




msg:4059559
 2:10 am on Jan 13, 2010 (gmt 0)

TheMadScientist if you want to help I can email you the html file too.

Please give me ur email.

Thank you,

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved