margaux Posted April 2, 2012 Share Posted April 2, 2012 A few questions on sending different file formats 1.How do you send a csv file as an attachment? I've created a csv file so just need to learn how to sendi it. 2. Are there any books/tutorials you would recommend for creating and sending different file formats? Thanks Link to comment Share on other sites More sharing options...
Larry Posted April 2, 2012 Share Posted April 2, 2012 What do you mean by "send as an attachment"? In an email? Link to comment Share on other sites More sharing options...
margaux Posted April 2, 2012 Author Share Posted April 2, 2012 yes, as an attachment in an email. Sorry, should have been more specific. Link to comment Share on other sites More sharing options...
Larry Posted April 2, 2012 Share Posted April 2, 2012 Well, I normally use a HTML Mime Mail class to help with advanced email types. Are you using one already? Link to comment Share on other sites More sharing options...
margaux Posted April 3, 2012 Author Share Posted April 3, 2012 If I've understood what I've found online so far, I have 2 options: use PEAR addAttachment() - I haven't understood from the PEAR documentation how/where to implement this function (I have installed PEAR).What does "this function can not be called statically" mean? do I call it within the message part of my mail() function? [*]use mail() with the appropriate headers. The file I am attaching is created within the php script that sends the email. Is it a case of applying the correct headers within the mail() function? I found these online $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $header = "From: ".$from_name." <".$from_mail.">\r\n"; $header .= "Reply-To: ".$replyto."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; Presumably once I've encoded my file using chunk_split function with base64_encode, i'm good to go. The second option may be better for me as I haven't done much oop, but happy to give it a go if I could find some more documentation on it. Thanks for your help with this - I'm trying to solve a specific problem as well as learn more generally for future projects. Link to comment Share on other sites More sharing options...
abigail Posted April 3, 2012 Share Posted April 3, 2012 I only have limited experience with file types so I hope I'm not causing you more trouble than help. And I haven't used this for sending emails. But I let my admin user download from the database by making a csv file. These are the headers I use and I'm thinking this is what you need also: $file = 'orders'; $filename = $file."_".date("Y-m-d_H-i",time()); header("Content-type: application/vnd.ms-excel"); header("Content-disposition: csv" . date("Y-m-d") . ".csv"); header( "Content-disposition: filename=".$filename.".csv"); Link to comment Share on other sites More sharing options...
margaux Posted April 4, 2012 Author Share Posted April 4, 2012 Here's what I used which worked. $to = EMAIL; $subject = 'Training requirements submitted'; $header = 'From: ' . $_SESSION['uemail'] . "\r\n"; $content = chunk_split(base64_encode($fileData)); $uniqident = md5(uniqid(time())); $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uniqident."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uniqident."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uniqident."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uniqident."--"; mail ($to, $subject, $message, $header); Any recommendations for learning more about sending and downloading file types as I would like to be able to email attachments of different file types? Thanks. Link to comment Share on other sites More sharing options...
Larry Posted April 4, 2012 Share Posted April 4, 2012 I always use a class like PEAR or Zend Framework to do anything other than simple emails. It's just too complicated of a standard to get right easily and it's impossible to test for multiple clients. Link to comment Share on other sites More sharing options...
Recommended Posts