moe180 Posted June 10, 2009 Share Posted June 10, 2009 Hi, I am very new to php, and cannot get this code to work. I do not know what I have done wrong and as I said I am not very good at php yet, the problem what I want the code to do is send an email along with attachment to direct to my email address! obviously it does not work can someone please help me. yours in desperation Moe. <html> <head> <title> attachment sender </title> </head> <body> <?php $to_email = 'michael<moescreations2009@googlemail.com>'; $name = $_POST['name']; $telephone = $_POST['telephone']; $message = $_POST['message']; $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if (is_uploaded_file($fileatt)) { $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $data = chunk_split(base64_encode($data)); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } $ok = @mail($to,$name,$address,$message, $headers); if ($ok) { echo "<p>Your details have been sent.</p>"; } else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/ Share on other sites More sharing options...
haku Posted June 10, 2009 Share Posted June 10, 2009 Get rid of the @ mark so you can see what the error is. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853150 Share on other sites More sharing options...
Maq Posted June 10, 2009 Share Posted June 10, 2009 Get rid of the @ mark so you can see what the error is. That, and echo out the variables before you pass them to the mail function to see what's actually being passed. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853152 Share on other sites More sharing options...
moe180 Posted June 10, 2009 Author Share Posted June 10, 2009 Ok, I took the @ out, and ran it again and this is the resulting messages. Notice: Undefined variable: headers in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 38 Notice: Undefined variable: to in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62 Notice: Undefined variable: address in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62 Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62 Mail could not be sent. Sorry! could you let me know how to correct it so I can use it and then someone else may also use it and not have the same problems as me. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853172 Share on other sites More sharing options...
haku Posted June 10, 2009 Share Posted June 10, 2009 It says what the problem is right there: Undefined variable: headers You haven't defined the variable $headers. But you are passing it to the mail function. The way your script is set up, $headers is only defined when there is an upload. So you need to define an empty variable outside the 'if' statement like this: $headers = ''; Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853387 Share on other sites More sharing options...
moe180 Posted June 11, 2009 Author Share Posted June 11, 2009 <?php $to = "michaelpowens@hotmail.co.uk"; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if (is_uploaded_file($fileatt,'rb') $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $headers = ' '; $semi_rand = md5(time(); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; { $ok = mail($to, $name, $subject, $message ); if ($ok) } echo "<p>Thankyou your mail has been sent</p>"; else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?> ok so after alot of fiddling around I am now down to this and now I keep getting this error message_ Parse error: syntax error, unexpected T_VARIABLE in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 12 So I looked up t variables and although it says line 12 it does not mean it will be,(I know you guys know this but I would like you to know I am actually trying to fix this myself and not just hoping you will do it for me) So I have been through and tried to match everything up and make sure its right, but when I change one it moves on to another and so on and so forth and I am now gettin confused, obviously I am going to keep trying to sort it myself and I also removed the $header and $address and $phone after finding out I can actually only have five when I was trying to use 6 and this sorted half my problem. thankyou for all your help it is much appreciated. if you can keep helping me I would be very greatful. thankyou, moe Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853424 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 You are missing a bracket on your if statement. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853441 Share on other sites More sharing options...
Ken2k7 Posted June 11, 2009 Share Posted June 11, 2009 Is it me or are there curly braces opening and closing in odd places? Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853539 Share on other sites More sharing options...
haku Posted June 11, 2009 Share Posted June 11, 2009 Who knows. He isn't using code tags, so its really hard to read. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853577 Share on other sites More sharing options...
moe180 Posted June 11, 2009 Author Share Posted June 11, 2009 Hi, sorry I did not realise I had made it difficult to read, hope this is better. <?php // Read POST request params into global vars $to ="michaelpowens@hotmail.co.uk"; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; $headers = " "; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if (is_uploaded_file($fileatt)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Generate a boundary string $semi_rand = md5(time(); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // Send the message $ok = mail($to, $name, $subject, $message, $headers); if ($ok) { echo "<p>Thankyou your mail has been sent</p>"; } else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853657 Share on other sites More sharing options...
Daniel0 Posted June 11, 2009 Share Posted June 11, 2009 It was difficult to read because you didn't use tags. It's mentioned in this document which you were required to read BEFORE you even created an account here. I've edited your latest post. Next time, do it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853659 Share on other sites More sharing options...
moe180 Posted June 11, 2009 Author Share Posted June 11, 2009 Thankyou, I was confused as to what was mean't by [tags] I will learn how to do them, again thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853681 Share on other sites More sharing options...
moe180 Posted June 11, 2009 Author Share Posted June 11, 2009 Hi, i was really excited this afternoon as i got it working and managed to send myself 5 email then this happened and now will no longer send!!! Warning: mail() [function.mail]: SMTP server response: 501 Bad address syntax in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 61 If it is bad syntax why would it send a few times then stop, I have tried googling it and the general sound seems that some mime do not accept full length email addresses but the ways they mention of curing the problem I do not understand. ( and agin why work for a few goes then stop?)can you please explain what I need to do and how I go about curing the problem or help if possible. <?php error_reporting(E_ALL); ini_set('display_errors', true); // Read POST request params into global vars $to_email = 'michael<moescreations2009@googlemail.com>'; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; $to ='michael<moescreations2009@googlemail.com>'; $headers = "From: $from"; if (is_uploaded_file($fileatt)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // Send the message $ok = mail($to, $subject, $message, $headers); if ($ok) { echo "<p>Your details have been sent, thankyou.</p>"; } else { echo "<p> sorry you details could not be sent, please retry, Thankyou. </p>"; } ?> I think I have put tags in if not please do not edit for me, can you just explain exactly how to add the tags, thanks I need to learn if I am goiong to use this forum properly. thanks moe. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853877 Share on other sites More sharing options...
moe180 Posted June 11, 2009 Author Share Posted June 11, 2009 Hi guys thanks for all your help it seems to be working again?? I assume I must have sent too many emails in quick succession. The code is there so if anyone wishes to use it please feel free. And for all who have helped thankyou very much before I found this site I was on the verge of giving up, thankyou very much guys. Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853904 Share on other sites More sharing options...
Maq Posted June 11, 2009 Share Posted June 11, 2009 The code is there so if anyone wishes to use it please feel free. Is that the final version? If not, do you mind posting it? Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-853908 Share on other sites More sharing options...
moe180 Posted June 12, 2009 Author Share Posted June 12, 2009 I will post it below Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-854322 Share on other sites More sharing options...
moe180 Posted June 12, 2009 Author Share Posted June 12, 2009 No the code above is the basic working version, I need to try and make it as secure as I can to stop spammers (well make it difficult for them anyway). The code above does now work once I have done a version with security which I shall be doing now I will post that up as well, I hope people find a use for it. Haku, ken, daniel, thankyou all very much for your help and patience. Moe. <?php // Read POST request params into global vars $to ="michaelpowens@hotmail.co.uk"; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; $headers = " "; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; if (is_uploaded_file($fileatt)); { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Generate a boundary string $semi_rand = md5(time(); $mime_boundary = "=Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // Send the message $ok = mail($to, $name, $subject, $message, $headers); if ($ok) { echo "<p>Thankyou your mail has been sent</p>"; } else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161691-solved-what-is-wrong-with-this-please-help/#findComment-854327 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.