danbuntu Posted February 17, 2012 Share Posted February 17, 2012 Hello I have a database table containing users uploaded word docs as long blobs (I know in an ideal world these should be in a filesystem, not a table but that wasn't possible) I'd like to be able to generate an email with php and send out these files as attachments. I have pear mail installed and can get this to send out emails with attachments form a file share no problems. Getting it to work with blobs escapes me though. Is this possibile of do I need a different class? include('../db_connection.php'); $query1 = "SELECT id, name, type, content, size FROM upload WHERE lref='" . $_POST['lref'] . "'"; $result2 = $mysqlilsr->query($query1) or die ($mysqlilsr->error); list($id, $name, $type, $content, $size) = $result2->fetch_array(); //echo $name . '<br/>'; //echo $content . '<br/>'; //echo $type . '<br/>'; //$fileattname = file_get_contents($_POST['attachment']); $fileattname = file_get_contents($_POST['attachment']); $from_mail = "email address"; $replyto = "email address"; //phpinfo(); //mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message); $headers = array( 'From' => $from_mail, 'Subject' => $subject, 'Cc' => $replyto, ); $body = $header . $spacer .$message . $spacer . $footer; $mime = new Mail_mime(); $mime->setTXTBody($body); $mime->addAttachment(base64_encode($content), $name, $type, "iso-8859-1"); $body = $mime->get(); $headers = $mime->headers($headers); $mailer =& Mail::factory('mail'); $res = $mailer->send($_POST['email'], $headers, $body); if (PEAR::isError($res)) { echo 'error'; } else { 'success'; } Quote Link to comment https://forums.phpfreaks.com/topic/257189-email-blobs-as-attachments/ Share on other sites More sharing options...
dazman Posted February 18, 2012 Share Posted February 18, 2012 im sorry i have not used your email add-in before. but with php mailer this can be done. (unless your email sending is running on a cron task) then you NEED to write a temporary file to the disk, and pick it up from there. The most important thing here is the mimetype. They way i did it was inside the PHP mailer object it has some member function like addAttachment( I basically went and read the code on what the object looked like, then instead of it doing its usual thing. i overrode the function to use the content/mimetype/filename/filesize from the database instead of the normal way. But the mailer did all sorts of email encoding. So when it added attachments i had stuff outputted to the email content like Content-Type: Content-Transfer-Encoding: Content-ID: Content-Disposition: filename= blah But most of the encoding was base64. it also seemed to support 7bit/8bit/binary and a few others. I suggest you crack open the mailer object, and see what it does with the data it gets from the filepath. Then possibly extend the mailer class, and override the functions you need to and leave the rest. That way if bugs are fixed in the mailer class, then you can still update the class and most of the time your code will be sweet. Quote Link to comment https://forums.phpfreaks.com/topic/257189-email-blobs-as-attachments/#findComment-1318553 Share on other sites More sharing options...
danbuntu Posted February 18, 2012 Author Share Posted February 18, 2012 thanks for your reply. I ended up getting this working with swiftmailer: http://swiftmailer.org/ This was pretty simple once I'd read the documentation and let me assign the blob to a varible and then pass that with the name and mime type to an array with the other info and send it. I'm defiantly adding swift mailer to my arsenal. Quote Link to comment https://forums.phpfreaks.com/topic/257189-email-blobs-as-attachments/#findComment-1318634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.