millercj Posted April 4, 2008 Share Posted April 4, 2008 What i need to do is have an html form where the user selects a local file on their system and it goes emailed via a php script. I've got this script but now do i adapt it to accept an input from a form, will the $_POST variable from the form suffice for the file location and how do I accept any type of file the user submits...not just PDF's <?php $to = "Chris Miller <[email protected]>"; $from = "John-Smith <[email protected]>"; $subject = "Here is your attachment"; $fileatt = "logo.pdf"; $fileatttype = "application/pdf"; $fileattname = "newname.pdf"; $headers = "From: $from"; $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: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; if( mail( $to, $subject, $message, $headers ) ) { echo "<p>The email was sent.</p>"; } else { echo "<p>There was an error sending the mail.</p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/ Share on other sites More sharing options...
stuffradio Posted April 4, 2008 Share Posted April 4, 2008 You need to edit a couple of things here. You should make a case for several file types, so for $fileattype you'd do switch($fileatt) { case application/pdf; // Blah break; } Just start by working on that, the make the fileatname to the value that was posted from the form, and maybe someone else can add to what I suggested Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/#findComment-508915 Share on other sites More sharing options...
millercj Posted April 4, 2008 Author Share Posted April 4, 2008 is there a place where i can find all the file types or are they all just like application/pdf application/jpg application/png... Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/#findComment-508926 Share on other sites More sharing options...
stuffradio Posted April 4, 2008 Share Posted April 4, 2008 You could see if there is a complete list at php.net/mail but I think that part of the site is down right now... not sure though. Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/#findComment-508929 Share on other sites More sharing options...
millercj Posted April 4, 2008 Author Share Posted April 4, 2008 Ok i've done this switch but it's not functioning, it always goes to the default case...any ideas? <?php $to = "Chris Miller <[email protected]>"; $from = "John-Smith <[email protected]>"; $subject = "Here is your attachment"; $fileatt = "image.png"; switch ($fileatt) { case 'application/pdf': $fileatttype = "application/pdf"; $fileattname = "document.pdf"; break; case 'image/jpg': $fileatttype = "image/jpg"; $fileattname = "image.jpg"; break; case 'image/jpeg': $fileatttype = "image/jpeg"; $fileattname = "image.jpeg"; break; case 'image/png': $fileatttype = "image/png"; $fileattname = "image.png"; break; case 'image/gif': $fileatttype = "image/gif"; $fileattname = "image.gif"; break; case 'application/zip': $fileatttype = "application/zip"; $fileattname = "picfile.zip"; break; default: echo'theres a problem'; } $headers = "From: $from"; $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: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; if( mail( $to, $subject, $message, $headers ) ) { echo "<p>The email was sent.</p>"; } else { echo "<p>There was an error sending the mail.</p>"; } ?> Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/#findComment-508957 Share on other sites More sharing options...
millercj Posted April 4, 2008 Author Share Posted April 4, 2008 any ideas? Link to comment https://forums.phpfreaks.com/topic/99462-help-with-php-email-wattachment/#findComment-509004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.