CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Help With Calling A Function (http://www.codingforums.com/showthread.php?t=285058)

Jinxy 12-31-2012 07:03 AM

Help With Calling A Function
 
Hi guys! I found this script doing a google search for extracting attachments from emails using php and imap. It seems to be working just fine as far as logging in and I can even extract an attachment from a single email but I need to be able to extract several attachments from more than one email and then save them to my site. Something like this added to the script:

PHP Code:

foreach ($content as $data)
file_put_contents($filename$data); } 

I know that's not the correct syntax but hopefully you understabd what I mean. Here's the script below:

PHP Code:

<?php
$server 
"{imap.gmail.com:993/imap/ssl}INBOX";
$username "user@gmail.com";
$password "qwerty";
$mbox imap_open($server$username$password);
// Getting all emails 
if ($headers imap_headers($mbox)) {
        
$i 0;
        foreach (
$headers as $val) {
                
$i ++;
                
$info imap_headerinfo($mbox$i);

// Gets the current email structure (including parts) 
// Use var_dump($structure) to check it out
$structure imap_fetchstructure($mbox$info->Msgno);

$attachments get_attachments($structure);
foreach (
$attachments as $k => $at) {
$content imap_fetchbody($mbox$info->Msgno$at['part']);

switch (
$at['encoding']) {
case 
'3':
$content base64_decode($content);
                                break;
                                case 
'4':
        
$content quoted_printable_decode($content);
                                break;
                        }          
                }
        }
}
// Shutting down 
imap_close($mbox);
/** 
* Gets all attachments 
* Including inline images or such 
* @author: Axel de Vignon 
* @param $content: the email structure 
* @param $part: not to be set, used for recursivity 
* @return array(type, encoding, part, filename) 

*/ 
function get_attachments($content$part null) {
        static 
$results;

        
// First round, emptying results 
        
if (is_null($part)) {
                
$results = array();
        }

        
// Removing first dot (.) 
        
if (substr($part01) == '.') {
                
$part substr($part1);
        }
        
// Checking ifdparameters 
        
if (isset($content->ifdparameters) && $content->ifdparameters == && isset($content->dparameters) && is_array($content->dparameters)) {
foreach (
$content->dparameters as $object) {
                        if (isset(
$object->attribute) && strtolower($object->attribute) == 'filename') {
$results[] = array( 
       
'type'        => (isset($content->subtype)) ? $content->subtype ''
       
'encoding'    => $content->encoding
       
'part'        => (is_null($part)) ? $part
       
'filename'    => $object->value 
                                
);
                        }
                }
        }
        
// Checking ifparameters 
        
else if (isset($content->ifparameters) && $content->ifparameters == && isset($content->parameters) && is_array($content->parameters)) {
foreach (
$content->parameters as $object) {
                        if (isset(
$object->attribute) && strtolower($object->attribute) == 'name') {
$results[] = array(
       
'type'        => (isset($content->subtype)) ? $content->subtype ''
       
'encoding'    => $content->encoding
       
'part'        => (is_null($part)) ? $part
       
'filename'    => $object->value 
                                
);
                        }
                }
        }
        
// Recursivity 
        
if (isset($content->parts) && count($content->parts) > 0) {
// Other parts into content
foreach ($content->parts as $key => $parts) {
     
get_attachments($parts, ($part.'.'.($key 1)));
                }
        }
        return 
$results
}
?>

Thanks!

Jinxy 01-11-2013 11:48 PM

Ok I finally got this script so that it saves the attachments to my host by adding this:

PHP Code:

$filename $at['filename'];
file_put_contents($filename$content); 

So now the code looks like this and works fine:

PHP Code:

<?php
$server 
"{imap.gmail.com:993/imap/ssl}INBOX";
$username "user@gmail.com";
$password "qwerty";
$mbox imap_open($server$username$password);
// Getting all emails 
if ($headers imap_headers($mbox)) {
        
$i 0;
        foreach (
$headers as $val) {
                
$i ++;
                
$info imap_headerinfo($mbox$i);

// Gets the current email structure (including parts) 
// Use var_dump($structure) to check it out
$structure imap_fetchstructure($mbox$info->Msgno);

$attachments get_attachments($structure);
foreach (
$attachments as $k => $at) {
$content imap_fetchbody($mbox$info->Msgno$at['part']);

switch (
$at['encoding']) {
case 
'3':
$content base64_decode($content);

$filename $at['filename'];
file_put_contents($filename$content);

                                break;
                                case 
'4':
        
$content quoted_printable_decode($content);
                                break;
                        }          
                }
        }
}
// Shutting down 
imap_close($mbox);
/** 
* Gets all attachments 
* Including inline images or such 
* @author: Axel de Vignon 
* @param $content: the email structure 
* @param $part: not to be set, used for recursivity 
* @return array(type, encoding, part, filename) 

*/ 
function get_attachments($content$part null) {
        static 
$results;

        
// First round, emptying results 
        
if (is_null($part)) {
                
$results = array();
        }

        
// Removing first dot (.) 
        
if (substr($part01) == '.') {
                
$part substr($part1);
        }
        
// Checking ifdparameters 
        
if (isset($content->ifdparameters) && $content->ifdparameters == && isset($content->dparameters) && is_array($content->dparameters)) {
foreach (
$content->dparameters as $object) {
                        if (isset(
$object->attribute) && strtolower($object->attribute) == 'filename') {
$results[] = array( 
       
'type'        => (isset($content->subtype)) ? $content->subtype ''
       
'encoding'    => $content->encoding
       
'part'        => (is_null($part)) ? $part
       
'filename'    => $object->value 
                                
);
                        }
                }
        }
        
// Checking ifparameters 
        
else if (isset($content->ifparameters) && $content->ifparameters == && isset($content->parameters) && is_array($content->parameters)) {
foreach (
$content->parameters as $object) {
                        if (isset(
$object->attribute) && strtolower($object->attribute) == 'name') {
$results[] = array(
       
'type'        => (isset($content->subtype)) ? $content->subtype ''
       
'encoding'    => $content->encoding
       
'part'        => (is_null($part)) ? $part
       
'filename'    => $object->value 
                                
);
                        }
                }
        }
        
// Recursivity 
        
if (isset($content->parts) && count($content->parts) > 0) {
// Other parts into content
foreach ($content->parts as $key => $parts) {
     
get_attachments($parts, ($part.'.'.($key 1)));
                }
        }
        return 
$results
}
?>

My only problem now is that I want to add it to a form but I get this error message when I do:

Fatal error: Call to undefined function get_attachments()

Why would I get that error after putting it inside a simple form like this?

PHP Code:

<?php
if (isset($_POST[extract]))
{
# script above
}
?>
<form method="post">
<input type="submit" name="extract">
</form>


Jinxy 01-13-2013 02:09 AM

I finally figured out why I'm getting that error after putting that code inside an if statemant, but I still don't know how to fix it so it works.

Quote:

If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.
Can somebody show me how to fix that script so that it can be put inside an if statement so I can use it with a form. I would greatly appreciate it.

Thanks


All times are GMT +1. The time now is 03:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.