Hello all,
I've absolutely no idea what I'm doing with PHP...
I'm a fairly intelligent bloke, and all I need is one simple thing to add to my site, and I'm a happy bunny. So I managed to get some scripts and bastardise them to get something that I want.
The idea is I have a form and want to send the details plus attachments in an email to myself when it is filled out.
I know its a multi part mime encoding for text and attachments.
I completely understand the sending text part of it... thats cool.
Its the attachment part that has me baffled!
It send the data, but it all ends up in the email body as base64 coded gibberish! 😕
I'm sure I'm doing something really simply wrong, but I have been searching for 3 days now for answers (I was determined to work it out myself) but I now have to ask people for help.
Here is the code I have so far... (unfinished for my form, but has the basis of what I need... and please... no comments if it looks bad or it stinks as codes go, hey I don't know what I'm doing!):p
<?php
$mailto = 'simon@peekabou.com' ;
$subject = "New Registration" ;
$formurl = "http://www.peekabou.com/registration.htm" ;
$errorurl = "http://www.peekabou.com/error.htm" ;
$thankyouurl = "http://www.peekabou.com/thanks.htm" ;
$mime_boundary = "==Multipart_Boundary_x".md5(mt_rand())."x";
$headers = "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed;\r\n" .
"boundary=\"{$mime_boundary}\"" ;
$tmp_name = $FILES['fileatt']['tmp_name'];
$type = $FILES['fileatt']['type'];
$name = $FILES['fileatt']['name'];
$size = $FILES['fileatt']['size'];
$name = $POST['name'] ;
$aka = $POST['aka'] ;
$address = $POST['address'] ;
$city = $POST['city'] ;
$country = $POST['country'] ;
$postcode = $POST['postcode'] ;
$email = $_POST['email'] ;
$http_referrer = getenv( "HTTP_REFERER" );
if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (empty($name) || empty($email) || empty($address) || empty($city) || empty($country) || empty($postcode)) {
header( "Location: $errorurl" );
exit ;
}
if (file_exists($tmp_name)){
}
if(is_uploaded_file($tmp_name)){
$file = fopen($tmp_name,'rb');
$data = fread($file,filesize($tmp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
}
$message =
"This message was sent from:\n" .
"$http_referrer\n" .
"-------------- Registration information -------------\n\n" .
" Name: $name\n".
" AKA: $aka\n".
" Address: $address\n".
" City: $city\n".
" Country: $country\n".
" Postcode: $postcode\n".
" Email: $email\n".
"\n\n----------------------------------------------------------------\n" ;
$message = "This is a multi-part message in MIME format.\r\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
"name=\"{$fileatt}\"\n" .
"Content-Disposition: attachment;\n" .
"filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
mail($mailto, $subject, $message, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 4.0" );
header( "Location: $thankyouurl" );
exit ;
?>
Well thats it.. please somebody help.. I would love you forever 😃
Simon.