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);

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

need help in creating captcha
hi i have a problem creating captcha system. i create some basic script for image displaying but it

try catch error
I have been trying try catch but, I can't get it to work. The code opens a non existing file called

add to sql result
ok see rite now my sites hit counter is counted from the mysql visitor log i need to clear it aft

ALV grid with 2 headers
Hi Gurus,

I need to display ALV GRID as below.

Header1

IMAGE - SERVER SIDE scripting help...
How to receive pixel data of an image and buid a bitmap file on the server using the HTTP POST metho

Dynamic memory problem
Hey,

At the beginning of my code, I have this line:

Shape* gShape = 0;

Blocking video streaming
Hello everyone,

I post a message here because i didn't find any solution yet.
I just finis

[newb] Image hosting help.
Hey there, I'm new to php, I know some basics and i can code PWN, I'm only 14 but I'm interested in

type check while uploading
Hi Everyone,

How can I check the exact type of a file while uploading on my site?

Here

What am I missing here? Help!
Hello all!. I can't seem to get this working right. Well - it renders right, but something is goin

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