Forum Moderators: coopster

Message Too Old, No Replies

I need a little help,

php, bad header, mail command

         

diegomh7

9:06 am on Mar 12, 2007 (gmt 0)

10+ Year Member



Morning campers,

Im having a little trouble with a script and was wondering if you guys could help me out,

Ive written an online registration form that sends 12 variables to a page called registration2.php

This form then inserts the info into a mysql database after doing a few checks i.e if the username has already been used or not then (should) send out an email, this is where I get my error.

its a great error aswell.

"
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
Apache/2.0.55 (Red Hat) Server at www.example.com Port 80
"

-------------------------------------------------

The server log says

"
malformed header from script. Bad header=/home/sites/example.com/de: registration2.php, referer: http://www.example.com/registration.php

"

Heres the script.

<?php
## include the connection file
include("includes/conn.php");

$forename = $_POST["forename"];
$surname = $_POST["surname"];
$street_address = $_POST["sreet_address"];
$town = $_POST["town"];
$region = $_POST["region"];
$postcode = $_POST["postcode"];
$tel = $_POST["telephone"];
$email = $_POST["email1"];
$username = $_POST["createid"];
$password = $_POST["password1"];
$secret = $_POST["secret_q"];
$answer = $_POST["secret_a"];
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];
$terms = $_POST["terms"];
$marketing = $_POST["marketing"];
$eighteen = $_POST["over_18"];
$date = date('d-m-Y');
$ip = $_SERVER['REMOTE_ADDR'];
$sess = session_id();

$user_exist = "SELECT * from members where username = '$username'";
$exist_record = mysql_query($user_exist);
$num = mysql_num_rows($exist_record);

if ($num > 0){
$erm = "
<script>
alert('That username has already been taken please choose another');
history.go(-1);
</script>
";
}

if ($erm == ""){

mysql_query("INSERT into members (id,username,password,forename,surname,street_address,town, region,postcode,tel,email,secret_q,secret_a,d_dob,m_dob,y_dob, agreement,marketing,eighteen,active,payment_key,signup_date,ip_signup_address, opened_shop,sess) VALUES ('','$username','$password','$forename','$surname','$street_address', '$town','$region','$postcode','$tel','$email','$secret','$answer','$day', '$month','$year','$terms','$marketing','$eighteen','0','0','$date','$ip','0', '$sess')") or die(mysql_error());

}

if ($erm == ""){

$to = '$email';
$knownsender = 'admin@example.com'; //this should be an email address that is known to the server
//server
$subject = 'LOCAL HEAT CONFIRMATION';
$message = "message <br /><a href='http://www.example.com/email_confirm.php?confirmation=$sess'>Please click this link to confirm your email address.</a>";
$headers = 'from: $knownsender\r\n';
$headers .= 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';

mail($to, $subject, $message, $headers);
}

?>
<html>
<head>
<? echo "$erm"?>
</head>
<body>
</body>
</html>

The thing is, it wasnt working, I changeed the html in the HEADER section for the email and it fixed it, I left it for an hour and then i broke again,

Ive been to the hosting company to see if there is a problem and get a little support and they said they dont offer support on external sripts, very friendly i thought ;)

Ok so, heres the challenge should you choose to accept it,

1. Where am I going wrong?
2. Do you know how to fix it?
3. Do you want dinner in return for fixing it?

thanx in advance people,

Diegomh7

[edited by: dreamcatcher at 10:14 am (utc) on Mar. 12, 2007]
[edit reason] Exemplified urls, fixed side scroll. [/edit]

RonPK

12:00 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>
$headers = 'from: $knownsender\r\n';

Change that to

$headers = "From: $knownsender\r\n";

Note the double quotes. Variables inside a single-quoted string will not get parsed, and \r\n will be used literally. (the same goes for the next lines).

Also, you really should look into preventing SQL injections. Always validate user input, and use mysql_real_escape_string() [php.net] on strings you insert into the database.

diegomh7

3:07 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Thanx for the input,

ive done this

if ($erm == ""){

$to = "$email";
$knownsender = "admin@example.com"; //this should be an email address that is known to the server
//server
$subject = "LOCAL HEAT CONFIRMATION";
$message = "message <br /><a href='http://www.example.com/email_confirm.php?confirmation=$sess'>Please click this link to confirm your email address.</a>";
$headers = "From: $knownsender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";

mail($to, $subject, $message, $headers);
}

Still same problem?

any more ideas im running out,

thanx in advance,

Diego

[edited by: eelixduppy at 3:57 pm (utc) on Mar. 12, 2007]
[edit reason] exemplified domain [/edit]

cmarshall

3:33 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



FWIW, I never use the "message" parameter. I always use the "header" parameter for the main message.

I'll SM you a link to an example that I send to others.

RonPK

4:38 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$headers = "From: $knownsender\r\n";  
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";

FWIW, \r\n should be used to separate headers. So there is no need for it on the last line.

diegomh7

8:50 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



not working, anymore help anyone?

plzzzzz

cmarshall

8:54 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's possible your server is bad.

If that script I SMed you isn't working, then that's a real possibility. That script is pretty vanilla, and works on several servers (it may need some work on Windows servers, which have funny line endings).

diegomh7

9:50 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



your script seems to be working,

erm,

this is what i got now,

$to = "$email";
$subject = 'LOCAL HEAT CONFIRMATION';
$message = "message <br /><a href='http://www.example.co.uk/email_confirm.php?confirmation=$sess'>Please click this link to confirm your email address.</a>";

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";

// Additional headers
$headers .= 'From: admin@example.co.uk <admin@example.co.uk>' . "\n";
//$headers .= 'Bcc: $Bcc' . "\r\n";
## Do the Email Action
mail($to, $subject, $msg, $headers) or die (mysql_error());

still same internal server error,

I also managed to piss the hosting company off,

diegomh7

10:14 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Ok ive narrowed it down,

the $to = '$email';

is the problem

the server throws an error up unless i manually type in 'me@myemail.co.uk'

erm, trying to strip slashes and allsorts with no joy,

how can i get the posted variable called $email to be sent in the $to vriable without throwing me an error up?

ideas?

cmarshall

10:15 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If that script works, then I would take out the "\r\n", and replace them with a simple "\n".

I'd also move the body into the headers. That script is a complete solution. It contains everything you need to build an email with one attachment and send it.

cmarshall

10:26 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That script I sent has an email validation routine in it. You may want to make sure the to address is valid before sending it to the server; especially if you are causing your hosting company agita.

diegomh7

10:27 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



I do appreciate the link you gave me, it is really good mate, ill be ale to use it on another project aswell that im working on,

its a little complicated for me to go through and figure out for this site though,

why does the $email variable not get parsed properly, why do I have to manually input a $to 'email'?

this works,

$to = 'email@email.com';

this doesnt

$to = '$email';

nor this

$to = "$email";

nor this

$to = $email;

feck!

im getting tired of it now,

cmarshall

10:34 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



print_r ( $email );

Does it contain what you expect?

If you use that script, or take the sending function out, you can display the email, instead of sending it.

Absolutely required. I spent a long time tweaking the MIME separators before I got it right.

diegomh7

10:37 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



OMG!

I AM SUCH A NOOB!

HAAHAHAHAHAAHH

Turns out the file cant send the form because the email I put in the form to test wasnt valid,

i was typing "dfasdfa" into it.

SORRY WORLD!

by the way the combination i used to make it work after that mistake was

$to = $email; //no apostophies.

BYeeeeeee