Function to extract email attachments using PHP IMAP


Posted on 16th Feb 2014 07:03 pm by admin

function extract_attachments($connection, $message_number) {

$attachments = array();
$structure = imap_fetchstructure($connection, $message_number);
Did you know?Explore Trending and Topic pages for more stories like this.

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;

}

can anyone please explain this code
No comments posted yet

Your Answer:

Login to answer
147 Like 16 Dislike
Previous forums Next forums
Other forums

How to use php and sql to check if values match the ones in a table (for logins)
How would I code it that the script takes two variables that are passed to it (UserID and PIN), and

Bit manipulation program with binary operations
My homework is to create a program to scan in and add binary numbers and print them like so

Shuffle between users ??
I have multiple $users in table. I need to send them $message.

I need to send the next messag

login page does not execute a else statement
I've created a login page using sessions.
When an incorrect user name or password is entered then

very easy question about SQL info
Hello,
I think that I have a very easy question.
I know how to create a form using php SQL and

Big O & time complexity ???
can you help me how to calculate the Big O & time complexity for any algorithm and c++ program pleas

Insert data in Mysql and move to another page
I'm stuck with this simple problem. I need to insert data in Mysql and then hit submit and move to a

System copy error in RUN ABAP REPORT PHASE
Hi,
I am performing system copy on my test server. I have previously taken export of JAVA + A

Streaming local file from PHP while it’s been written to by a CURL process
I am creating a simple Proxy server for my website. Why I am not using mod_proxy and mod_cache is a

session_destroy();
new to php
I have a simple login and am trying to write a logout.
I set a $_SESSION var to 1 i

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash