Author Topic: Problem with uploading and sending attachments  (Read 1316 times)

0 Members and 1 Guest are viewing this topic.

eldorado

  • Guest
Problem with uploading and sending attachments
on: Sun, 11. February 2007 - 00:25:08
First it took me a while to figure out that I need to use  $_FILES[filename][tmp_name] & $_FILES[filename][name] to obtain the information about the attachment from a form with the post method. After a lot of playing around I finally figured out that the attach function in the vlibmail class will simply not process the attachment if $filename = $_FILES[filename][name]. It will however process the attachment if $filename = $_FILES[filename][tmp_name]. But that leaves one final issue which is the attachment in the receiving email is given some random temporary name assigned by tmp_name instead of the actual name. To get around this problem, I had to modify both the public and private functions that handle attachments.

First, here is the PHP code that extracts the file information from posted form where the name attachment is the name of the form object of type file:

Code: [Select]
$mail->from($senderemail);
$mail->subject("This is a testt");
$attach = $_FILES[attachment][tmp_name];
$name = $_FILES[attachment][name];
$mail->attach($attach,$name);

The public function now looks like this (changes highlighted):

function attach ($filename, $name, $disposition = attachment, $mimetype=null, $cid=null) {
            if($mimetype == null) $mimetype = vlibCommon::getMimeType($filename);
            $this->attachments] = $filename;
            $this->name] = $name;      
            $this->mimetypes] = $mimetype;
            $this->dispositions] = $disposition;
            if (!$cid) {
                srand((double)microtime()*96545624);
                $cid = md5(uniqid(rand()))[email protected];
            }
            $this->contentIDs] = $cid;
            return $cid;
        }

Here is the small change in the private function (changes highlighted):

 function _build_attachments () {
            $sep = chr(13).chr(10);
            $ata = array();
            $k=0;

            for ($i=0; $i < count( $this->attachments); $i++) {
                $filename = $this->attachments[$i];
            //changed basename to point to name instead of attachments
                $basename = basename($this->name[$i]); 
              $mimetype = $this->mimetypes[$i];
                $disposition = $this->dispositions[$i];
                $contentID = $this->contentIDs[$i];
                //more code....

I am only posting this just in case someone else experiences similar problems using the attachment function. Otherwise the class works really well. :)
Last Edit: Sun, 11. February 2007 - 12:44:56 by ClausVB

Offline ClausVB

  • Administrator
  • Hero Member
  • *****
  • Posts: 566
    • Homepage: clausvb.de
Re: Problem with uploading and sending attachments
Reply #1 on: Sun, 11. February 2007 - 12:43:50
Thanks!

Another option: Just move the files (with $_FILES) to an upload folder and attach them from there.

Regards,
Claus