austin350s10 Posted March 14, 2012 Share Posted March 14, 2012 I found the below code so I can attach a file to a email message, but there is a small problem I need some help with. The script works beautifully except for the fact that when message is received the attached file does not contain a file extension. I am able to open the attachment but I am receiving the windows dialog box that asks: "Choose the program you want to open this file". I intend on only using this script to attach HTML files so I am wondering if there is a way to make the file extension of the attachment read as .html? That way when the user receives the attachment it will automatically open with there default browser. <?php $to = "[email protected]"; $from = "[email protected]"; $subject = "AHC"; $message = "hello world"; $fileatt = $_SERVER['DOCUMENT_ROOT']."/home-care-information/test.html"; $fileatt_type = "multipart/alternative"; //This is where the problem is I think $fileatt_name = "Application"; $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: {$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, $subject, $message, $headers); if ($ok) { echo "<p>Mail sent! Yay PHP!</p>"; } else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258933-sending-attachments-with-mail-function/ Share on other sites More sharing options...
DavidAM Posted March 14, 2012 Share Posted March 14, 2012 I have not worked with this in a while, but I think the problem is in $fileatt = $_SERVER['DOCUMENT_ROOT']."/home-care-information/test.html"; $fileatt_name = "Application"; # ... $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"; [i broke it up a little to make it easier to see] Where you assign the "name=" and "filename=", you are giving it the value "Application", which does not have an extension. I think you want to put the actual filename here (without any path) as a "suggestion" of the name to save as. So, perhaps $destFile = basename($fileatt); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$destFile}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$destFile}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; Quote Link to comment https://forums.phpfreaks.com/topic/258933-sending-attachments-with-mail-function/#findComment-1327437 Share on other sites More sharing options...
austin350s10 Posted March 15, 2012 Author Share Posted March 15, 2012 Thank you sooo much. That was the trick!! Quote Link to comment https://forums.phpfreaks.com/topic/258933-sending-attachments-with-mail-function/#findComment-1327627 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.