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

Data type mismatch
Hi,
I am migrating data from algol to c.I mapped real datatype in algol to double datatype in

Form always sends to error page...
Hello,
Any help will be greatly appreciated. I am having trouble getting multiple fields to be re

Curl & sessions PLS HELP
Hello,

I have a problem with curl and sessions and i will try to explain the best i could.

Checking if a process is running (problems including psapi.dll i think...)
Ok, so all I'm trying to do is check if a program is running...

I've found out that I nee

Uploading/Downloading files stored in MySQL database
Hey all,

This problem just came up in my website and I'm having a hard time figuring out what

How a counter of users ? such as ---> (231 Viewing)
I want to count how many users are actually viewing the page, How is that possible?
Thank you guy

update sql when refresh - php
hi
I have made a table (attachement)

the users can update the sql database using + or x bu

Big Problem!! Please help
Hi Guys,

Im making a website for a friend have encountered a really annoying problem. When ev

Help Ordering Arrays
Hi, I have 3 arrays as shown below.

Code: $users = $this->get_latest_users();
$flir

PHP and XML image gallery variable issue
I'm trying to display the large image on the same page as the thumbnails, to do that I'm passing a v

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