+1 for absolute path. I've got in the habit of always using absolute paths,
makes it so much easier in the long run.

Thanks,
Nathan.

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of Chris Hope
Sent: Friday, 12 November 2010 6:14 p.m.
To: [email protected]
Subject: Re: [phpug] tricky one...imap email and attachments

Or else use an absolute path for the filename. One or the other may
fix your issue.



On 12 November 2010 18:12, Chris Hope <[email protected]> wrote:
> Try adding chdir(dirname(__FILE__)) at the top of the script.
>
>
>
> On 12 November 2010 17:36, Brendan Brink <[email protected]>
wrote:
>> hi there,
>>
>> still having difficulties with this one:
>>
>> What I want to accomplish is:
>>
>> 1. Create a CRON job that checks an email address for messages every
minute
>> 2. Take each message, extract the attachments, then send a new email to a
>> different address and add the attachments to it.
>>
>> Have been able to successfully do this with a PHP script, however when I
try
>> and run through a CRON job have issues with the file_put_content line
>>
>> Code is below...
>>
>> function extract_attachments($connection, $message_number) {
>>
>>     $attachments = array();
>>     $structure = imap_fetchstructure($connection, $message_number);
>>
>>     if(isset($structure->parts) && count($structure->parts)) {
>>
>>         for($i = 0; $i < count($structure->parts); $i++) {
>>
>>             $attachments[$i] = array(
>>                 'is_attachment' => false,
>>                 'filename' => '',
>>                 'name' => '',
>>                 'attachment' => ''
>>             );
>>
>>             if($structure->parts[$i]->ifdparameters) {
>>                 foreach($structure->parts[$i]->dparameters as $object) {
>>                     if(strtolower($object->attribute) == 'filename') {
>>                         $attachments[$i]['is_attachment'] = true;
>>                         $attachments[$i]['filename'] = $object->value;
>>                     }
>>                 }
>>             }
>>
>>             if($structure->parts[$i]->ifparameters) {
>>                 foreach($structure->parts[$i]->parameters as $object) {
>>                     if(strtolower($object->attribute) == 'name') {
>>                         $attachments[$i]['is_attachment'] = true;
>>                         $attachments[$i]['name'] = $object->value;
>>                     }
>>                 }
>>             }
>>
>>             if($attachments[$i]['is_attachment']) {
>>                 $attachments[$i]['attachment'] =
imap_fetchbody($connection,
>> $message_number, $i+1);
>>                 if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
>>                     $attachments[$i]['attachment'] =
>> base64_decode($attachments[$i]['attachment']);
>>                 }
>>                 elseif($structure->parts[$i]->encoding == 4) { // 4 =
>> QUOTED-PRINTABLE
>>                     $attachments[$i]['attachment'] =
>> quoted_printable_decode($attachments[$i]['attachment']);
>>                 }
>>             }
>>
>>         }
>>
>>     }
>>
>>     return $attachments;
>>
>> }
>>
>>
>> $server = "pop3.####.co.nz";
>> $login = "#...@#####.co.nz";
>> $password = "#####";
>>
>>
>> $connection = imap_open('{pop3.webhost.co.nz:110/pop3}', $login,
$password);
>> $count = imap_num_msg($connection);
>> echo "MESSAGES TO PROCESS: ".$count;
>> for($a = 1; $a <= $count; $a++) {
>>
>>
>>     $structure = imap_fetchstructure($connection, $a);
>>     //print_r($structure);
>>     $header = imap_headerinfo($connection, $a);
>>     //print_r($header);
>>     //$raw_body = imap_body($connection, $i);
>>     //print_r($raw_body);
>>
>>     $attachments = extract_attachments($connection, $a);
>>
>>
>>     $thesubject = $header->subject;
>>     $blanksubject = $header->Subject;
>>
>>     $numberofattachments = 0;
>>
>>
>>     foreach ($attachments as $attachment) {
>>
>>         if ($attachment['is_attachment']) {
>>
>>             $numberofattachments++;
>>
>>             echo $attachment['filename'];
>>             str_replace(" ","_",$attachment['filename']);
>>
>>             $testfilename =
"file_uploads/INVOICE_".$attachment['filename'];
>>             $bytes = file_put_contents($testfilename,
>> $attachment['attachment']);
>>
>> //WHEN GETS TO THIS NO BYTES ARE RETURNED:
>>
>> and get this message from the CRON JOB:
>> Warning: file_put_contents(file_uploads/INVOICE_Order 41344.pdf): failed
to
>> open stream: No such file or directory in
>> /import/home/folder1/folder2/folder3/folder4/folder5/process_inbox.php on
>> line 97
>>
>> I am thinking maybe a path issue? but has anyone any advice /
>> recommendations?
>>
>> The file_uploads is within the folder5 directory.
>>
>> Works fine when running the script without CRON job.
>>
>>
>>
>> --
>> NZ PHP Users Group: http://groups.google.com/group/nzphpug
>> To post, send email to [email protected]
>> To unsubscribe, send email to
>> [email protected]
>
>
>
> --
> Chris Hope
> The Electric Toolbox Ltd
>
> Email: [email protected]
> Web: www.electrictoolbox.com
> Phone: +64 9 522 9531
> Mobile: +64 21 866 529
>



-- 
Chris Hope
The Electric Toolbox Ltd

Email: [email protected]
Web: www.electrictoolbox.com
Phone: +64 9 522 9531
Mobile: +64 21 866 529

-- 
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]

-- 
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]

Reply via email to