Jump to content

Sending attachments with mail() function


austin350s10

Recommended Posts

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 = "test@test.com"; 
$from = "test@test.com"; 
$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>"; } 

?>

Link to comment
Share on other sites

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"; 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.