Jump to content

email blobs as attachments


danbuntu

Recommended Posts

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';
}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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