Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Attaching PDFs in e-mail with php

Attaching PDFs in e-mail with php
Thread Tools
jon31
Junior Member
Join Date: May 2003
Status: Offline
Reply With Quote
Dec 19, 2003, 05:40 PM
 
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
www.jmarus.com - kicks by JM
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Dec 20, 2003, 09:02 AM
 
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...
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Dec 21, 2003, 01:30 PM
 
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]
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Dec 21, 2003, 01:40 PM
 
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.
     
madmacgames
Grizzled Veteran
Join Date: Oct 2003
Status: Offline
Reply With Quote
Dec 21, 2003, 01:53 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 04:32 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,