Jump to content

Simple email attachment works but doesn't work!


barryhayden

Recommended Posts

I have a script that I've pieced together that attaches a pdf file and emails it to an address given. The script works, but when you try to open the file I'm told that it is either not supported or it has been damaged.

 

Also, even though the script tries to send the pdf (1.21 MB) I get the following when running the script... "Warning: filesize() [function.filesize]: stat failed for http://www.inkiosk.com/muncie/content/attachments/Muncie_Rate_Card.pdf in C:\xampp\htdocs\muncie\content\php\attachment.php on line 17

 

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\xampp\htdocs\muncie\content\php\attachment.php on line 17

Your file has been sent."

 

I would really appreciate some help figuring out where I've gone wrong here. I'd like to understand this but I can't think of anything else to try with it. Thanks in advance for any help. I've attached the script below as it stands now.

 

$fileatt = "http://www.inkiosk.com/muncie/content/attachments/Muncie_Rate_Card.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "Muncie Rate Card With Kiosk.pdf"; // Filename that will be used for the file as the attachment

$email_from = "[email protected]";
$email_subject = "The information you requested.";
$email_message = "Thank you for your information request. You'll find the information you requested attached.\n\n";
$email_message .= "Sincerely,\n";
$email_message .= "Barry Hayden";

$email_to = "[email protected]";

$headers = "From: ".$email_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}\"";
$headers .= "X-Mailer: PHP/" . phpversion() . "\n";
$headers .= "X-Priority: 1";

$email_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" .
$email_message .= "\n\n";

$data = chunk_split(base64_encode($data));
ob_start(); //Turn on output buffering

$email_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($email_to, $email_subject, $email_message, $headers);

if($ok) {
       echo "Your file has been sent.";
} else {
       die("Sorry but the email could not be sent.");
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.