function _engagements_imap_get_attachments
Search API
7.x engagements.module | _engagements_imap_get_attachments($inbox, $email_number, $from_email_machine_readable, $cid = 0) |
6.x engagements.module | _engagements_imap_get_attachments($inbox, $email_number, $from_email_machine_readable, $cid = 0) |
$inbox is the imap link. email_number is msg_no.
1 call to _engagements_imap_get_attachments()
- engagements_imap_get_all_received_messages in modules/
engagements/ engagements.module - Connect to our imap server, download all received messages from students (or others). We will then delete them, so they don't get read twice.
File
- modules/
engagements/ engagements.module, line 2222 - This is the primary module file for the engagements module.
Code
function _engagements_imap_get_attachments($inbox, $email_number, $from_email_machine_readable, $cid = 0)
{
/* get information specific to this email */
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = imap_fetchbody($inbox, $email_number, 2);
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$attachments [$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'subtype' => '',
'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;
$attachments [$i]['subtype'] = $structure->parts [$i]->subtype;
}
}
}
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;
$attachments [$i]['subtype'] = $structure->parts [$i]->subtype;
}
}
}
if ($attachments [$i]['is_attachment']) {
$attachments [$i]['attachment'] = imap_fetchbody($inbox, $email_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']);
}
}
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
$rtn = array();
if (count($attachments) != 0) {
foreach ($attachments as $c => $at) {
if ($at ['is_attachment'] == TRUE) {
// Create a new random filename to save it as.
$filename = 'email__' . $from_email_machine_readable . '_' . time() . '_' . mt_rand(1, 9999); // We will figure out the ext (from mimetype) later.
$tmp_name = sha1($filename . time() . mt_rand(1, 99999) . mt_rand(1, 99999));
// Save to our temporary location on the server.
$test = file_put_contents(sys_get_temp_dir() . '/' . $tmp_name, $at ['attachment']);
if (!$test) {
watchdog('engagements_email', 'Unable to write media file at temp location: ' . sys_get_temp_dir(), array(), 'error');
fpm('Unable to write media file at temp location:' . sys_get_temp_dir() . '. Permissions issue?');
continue;
}
// Next, we want to rename it to have the proper file extension, as best we can tell.
$ext = strtolower($at ['subtype']); // if we can't figure it out, use this.
// Attempt to figure out the mimetype....
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, sys_get_temp_dir() . '/' . $tmp_name);
$temp = explode(";", $mimetype);
$mimetype = strtolower(trim($temp [0]));
$new_ext = engagements_get_file_extension_from_mime_type($mimetype);
if ($new_ext) {
$ext = $new_ext;
}
$filename .= ".$ext"; // the filename doesn't have its extension already for whatever reason.
$file = array(
'name' => $filename,
'type' => $mimetype,
'tmp_name' => $tmp_name,
);
$fid = content_add_new_uploaded_file($file, $cid, FALSE);
// TODO: Possibly rename filename based on if it is encrypted or not?
$rtn [] = array(
'fid' => $fid,
'filename' => $filename,
'mimetime' => $mimetype,
'ext' => $ext,
);
} // if is_attachment == TRUE
} // foreach
}
return $rtn;
}