|
|
Attaching PDFs in e-mail with php
|
|
|
|
Junior Member
Join Date: May 2003
Status:
Offline
|
|
Hello all,
I've created a php function to send a PDF file in an e-mail, but it's corrupting the file. I'm not sure if I'm setting up the headers properly.
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="c503c6493ff27db57891b1ecc9ebb5d2"
--c503c6493ff27db57891b1ecc9ebb5d2
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
A message here.
--c503c6493ff27db57891b1ecc9ebb5d2
Content-Type: application/pdf; name="fishhouse_menu.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
file code here
--c503c6493ff27db57891b1ecc9ebb5d2--
I don't know if this really helps, but do you guys see anything I might be doing wrong?
Thanks, and merry christmas!
-jon
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by jon31:
Hello all,
I've created a php function to send a PDF file in an e-mail, but it's corrupting the file. I'm not sure if I'm setting up the headers properly...
At a quick glance it does look ok... Kind of hard to know where to start.
I had been having some problems with the same thing about a month or two back, but I started using the Mail_Mime class from PEAR and it's so damn nifty that I haven't looked back. No matter what you're looking to do, there's always someone who's already done it (and a good chance they've also written it better than you can as well!!! )
Other than that, simply look through the raw source of an email that someone's sent you with exactly the same file and do a quick comparison (perhaps you need to explicitly name the file, maybe you need a couple of extra pre-MIME information headers, etc...)
Merry Xmas to ya!
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
how are you inserting the file contents? I have a similar script where I send all kinds of files and also use base64 encoding, but have to chunksplit the data in the email, like:
[php]
chunk_split(base64_encode($file_code))
[/php]
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
PS. I just sent a PDF with mail.app and looking at the raw source, this is how it it sent it (using your example as a reference):
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="c503c6493ff27db57891b1ecc9ebb5d2"
--c503c6493ff27db57891b1ecc9ebb5d2
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
A message here.
--c503c6493ff27db57891b1ecc9ebb5d2
Content-Type: application/pdf;
name="fishhouse_menu.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="fishhouse_menu.pdf"
file code here
--c503c6493ff27db57891b1ecc9ebb5d2--
Whether the content-disposition being inline or attachment matters, I dunno (I use attachment in my email scripts); but you are missing a filename="fishhouse_menu.pdf" that goes with your content-disposition.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Oct 2003
Status:
Offline
|
|
And if the above 2 do not help you, here is the function I use. It may be of some help (I had to play with the MIME boundary to get it to work, some email programs did not like too simple boundaries)
[php]
////
// Send a file via email as an attachment
//
// @$sender format = "Sender Name < [email protected]>"
// @$recipient format = "Recipient Name < [email protected]>"
// Seperate multiple addresses with commas
function email_attachment($sender, $recipient, $subject, $text_message, $attachment_name, $filecode, $content_type = "application/octet-stream") {
$mime_boundary = "_----=_NextPart_".md5(time())."_";
$headers .= "From: $sender\n";
$headers .= "MIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n--{$mime_boundary}\nContent-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n{$text_message}\n--{$mime_boundary}\nContent-Type: $content_type;\n name=\"{$attachment_name}\"\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment;\n filename=\"{$attachment_name}\"\n\n" . chunk_split(base64_encode($filecode)) . "\n--{$mime_boundary}--";
if (!mail($recipient, $subject, $message, $headers)) die("Mailing of the file failed");
}
[/php]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|