/**
  * Create new Email record (and link to given record) including attachments
  * @global Users $current_user
  * @global PearDataBase $adb
  * @param  MailManager_Model_Message $mailrecord
  * @param String $module
  * @param CRMEntity $linkfocus
  * @return Integer
  */
 function __CreateNewEmail($mailrecord, $module, $linkfocus)
 {
     global $current_user, $adb;
     if (!$current_user) {
         $current_user = Users::getActiveAdminUser();
     }
     $handler = vtws_getModuleHandlerFromName('Emails', $current_user);
     $meta = $handler->getMeta();
     if ($meta->hasWriteAccess() != true) {
         return false;
     }
     $focus = new Emails();
     $focus->column_fields['activitytype'] = 'Emails';
     $focus->column_fields['subject'] = $mailrecord->_subject;
     if (!empty($module)) {
         $focus->column_fields['parent_type'] = $module;
     }
     if (!empty($linkfocus->id)) {
         $focus->column_fields['parent_id'] = "{$linkfocus->id}@-1|";
     }
     $focus->column_fields['description'] = $mailrecord->getBodyHTML();
     $focus->column_fields['assigned_user_id'] = $linkfocus->column_fields['assigned_user_id'];
     $focus->column_fields["date_start"] = date('Y-m-d', $mailrecord->_date);
     $focus->column_fields["email_flag"] = 'MailManager';
     $from = $mailrecord->_from[0];
     $to = $mailrecord->_to[0];
     $cc = !empty($mailrecord->_cc) ? implode(',', $mailrecord->_cc) : '';
     $bcc = !empty($mailrecord->_bcc) ? implode(',', $mailrecord->_bcc) : '';
     //emails field were restructured and to,bcc and cc field are JSON arrays
     $focus->column_fields['from_email'] = $from;
     $focus->column_fields['saved_toid'] = $to;
     $focus->column_fields['ccmail'] = $cc;
     $focus->column_fields['bccmail'] = $bcc;
     $focus->save('Emails');
     $emailid = $focus->id;
     // TODO: Handle attachments of the mail (inline/file)
     $this->__SaveAttachements($mailrecord, 'Emails', $focus);
     return $emailid;
 }
Esempio n. 2
0
 /**
  * Searches the Mail Box with the query
  * @param String $query - imap search format
  * @param MailManager_Model_Folder $folder - folder instance
  * @param Integer $start - Page number
  * @param Integer $maxLimit - Number of mails
  */
 function searchMails($query, $folder, $start, $maxLimit)
 {
     // JFV - enable UTF imap search
     //		$nos = imap_search($this->mBox, $query);
     $nos = imap_search($this->mBox, $query, SE_FREE, 'UTF-8');
     // JFV END
     if (!empty($nos)) {
         $nmsgs = count($nos);
         $reverse_start = $nmsgs - $start * $maxLimit;
         $reverse_end = $reverse_start - $maxLimit;
         if ($reverse_start < 1) {
             $reverse_start = 1;
         }
         if ($reverse_end < 1) {
             $reverse_end = 0;
         }
         if ($nmsgs > 1) {
             $nos = array_slice($nos, $reverse_end, $reverse_start - $reverse_end);
         }
         // Reverse order the messages
         rsort($nos, SORT_NUMERIC);
         $mails = array();
         $records = imap_fetch_overview($this->mBox, implode(',', $nos));
         foreach ($records as $result) {
             array_unshift($mails, MailManager_Model_Message::parseOverview($result));
         }
         $folder->setMails($mails);
         $folder->setPaging($reverse_end, $reverse_start, $maxLimit, $nmsgs, $start);
         //-1 as it starts from 0
     }
 }
 /**
  * Function used to set the record fields with the information from mail.
  * @param Array $qcreate_array
  * @param MailManager_Model_Message $mail
  * @return Array
  */
 function processFormData($qcreate_array, $mail)
 {
     $subject = $mail->subject();
     $from = $mail->from();
     if (!empty($from)) {
         $mail_fromAddress = implode(',', $from);
     }
     if (!empty($mail_fromAddress)) {
         $name = explode('@', $mail_fromAddress);
     }
     if (!empty($name[1])) {
         $companyName = explode('.', $name[1]);
     }
     $defaultFieldValueMap = array('lastname' => $name[0], 'email' => $mail_fromAddress, 'email1' => $mail_fromAddress, 'accountname' => $companyName[0], 'company' => $companyName[0], 'ticket_title' => $subject, 'subject' => $subject, 'potentialname' => $subject, 'projectname' => $subject, 'projecttaskname' => $subject);
     $defaultFieldValueMapKeys = array_keys($defaultFieldValueMap);
     foreach ($qcreate_array as $qc_array) {
         $new_qc_array = array();
         foreach ($qc_array as $q_array) {
             if (in_array($q_array[2][0], $defaultFieldValueMapKeys)) {
                 if ($q_array[2][0] == "lastname") {
                     $q_array[3][1] = $defaultFieldValueMap[$q_array[2][0]];
                 } else {
                     $q_array[3][0] = $defaultFieldValueMap[$q_array[2][0]];
                 }
             }
             $new_qc_array[] = $q_array;
         }
         $new_qcreate_array[] = $new_qc_array;
     }
     return $new_qcreate_array;
 }
Esempio n. 4
0
 /**
  * Function which processes request for Mail Operations
  * @global Integer $list_max_entries_per_page - Number of entries per page
  * @global PearDataBase Instance $adb
  * @global Users Instance $current_user
  * @global String $root_directory
  * @param MailManager_Request $request
  * @return MailManager_Response
  */
 function process(MailManager_Request $request)
 {
     global $list_max_entries_per_page, $adb, $current_user;
     $response = new MailManager_Response();
     if ('open' == $request->getOperationArg()) {
         $foldername = $request->get('_folder');
         $connector = $this->getConnector($foldername);
         $folder = $connector->folderInstance($foldername);
         $connector->markMailRead($request->get('_msgno'));
         $mail = $connector->openMail($request->get('_msgno'));
         // Get updated count after opening the email
         $connector->updateFolder($folder, SA_MESSAGES | SA_UNSEEN);
         $viewer = $this->getViewer();
         $viewer->assign('FOLDER', $folder);
         $viewer->assign('MAIL', $mail);
         $uicontent = $viewer->fetch($this->getModuleTpl('Mail.Open.tpl'));
         $metainfo = array('from' => $mail->from(), 'subject' => $mail->subject(), 'msgno' => $mail->msgNo(), 'msguid' => $mail->uniqueid(), 'folder' => $foldername);
         $response->isJson(true);
         $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount(), 'ui' => $uicontent, 'meta' => $metainfo));
     } else {
         if ('mark' == $request->getOperationArg()) {
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             $folder = $connector->folderInstance($foldername);
             $connector->updateFolder($folder, SA_UNSEEN);
             if ('unread' == $request->get('_markas')) {
                 $connector->markMailUnread($request->get('_msgno'));
             }
             $response->isJson(true);
             $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount() + 1, 'status' => true, 'msgno' => $request->get('_msgno')));
         } else {
             if ('delete' == $request->getOperationArg()) {
                 $msg_no = $request->get('_msgno');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $connector->deleteMail($msg_no);
                 $response->isJson(true);
                 $response->setResult(array('folder' => $foldername, 'status' => true));
             } else {
                 if ('move' == $request->getOperationArg()) {
                     $msg_no = $request->get('_msgno');
                     $foldername = $request->get('_folder');
                     $moveToFolder = $request->get('_moveFolder');
                     $connector = $this->getConnector($foldername);
                     $connector->moveMail($msg_no, $moveToFolder);
                     $response->isJson(true);
                     $response->setResult(array('folder' => $foldername, 'status' => true));
                 } else {
                     if ('send' == $request->getOperationArg()) {
                         require_once 'modules/MailManager/config.inc.php';
                         // This is to handle larger uploads
                         $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $to_string = rtrim($request->get('to'), ',');
                         $connector = $this->getConnector('__vt_drafts');
                         if (!empty($to_string)) {
                             $toArray = explode(',', $to_string);
                             foreach ($toArray as $to) {
                                 $relatedtos = MailManager::lookupMailInVtiger($to, $current_user);
                                 $referenceArray = array('Contacts', 'Accounts', 'Leads');
                                 for ($j = 0; $j < count($referenceArray); $j++) {
                                     $val = $referenceArray[$j];
                                     if (!empty($relatedtos) && is_array($relatedtos)) {
                                         for ($i = 0; $i < count($relatedtos); $i++) {
                                             if ($i == count($relatedtos) - 1) {
                                                 $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                                 $parentIds .= $relateto[1] . "@1";
                                             } elseif ($relatedtos[$i]['module'] == $val) {
                                                 $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                                 $parentIds = $relateto[1] . "@1";
                                                 break;
                                             }
                                         }
                                     }
                                     if (isset($parentIds)) {
                                         break;
                                     }
                                 }
                                 if ($parentIds == '') {
                                     if (count($relatedtos) > 0) {
                                         $relateto = vtws_getIdComponents($relatedtos[0]['record']);
                                         $parentIds = $relateto[1] . "@1";
                                         break;
                                     }
                                 }
                                 $cc_string = rtrim($request->get('cc'), ',');
                                 $bcc_string = rtrim($request->get('bcc'), ',');
                                 $subject = $request->get('subject');
                                 $body = $request->get('body');
                                 $description = $body;
                                 if (!empty($relateto) and !empty($relateto[1])) {
                                     $entityId = $relateto[1];
                                     $parent_module = getSalesEntityType($entityId);
                                     if (!empty($parent_module)) {
                                         $description = getMergedDescription($body, $entityId, $parent_module);
                                         $subject = getMergedDescription($subject, $entityId, $parent_module);
                                     } else {
                                         $n = MailManager_RelationControllerAction::ws_modulename($relateto[0]);
                                         if ($n == 'Users') {
                                             $description = getMergedDescription($body, $entityId, 'Users');
                                             $subject = getMergedDescription($subject, $entityId, 'Users');
                                         }
                                     }
                                 }
                                 $pos = strpos($description, '$logo$');
                                 if ($pos !== false) {
                                     $description = str_replace('$logo$', '<img src="cid:logo" />', $description);
                                     $logo = 1;
                                 }
                                 $fromEmail = $connector->getFromEmailAddress();
                                 $userFullName = getFullNameFromArray('Users', $current_user->column_fields);
                                 $userId = $current_user->id;
                                 $mailer = new Vtiger_Mailer();
                                 $mailer->IsHTML(true);
                                 $mailer->ConfigSenderInfo($fromEmail, $userFullName, $current_user->email1);
                                 $mailer->Subject = $subject;
                                 $mailer->Body = $description;
                                 $mailer->addSignature($userId);
                                 if ($mailer->Signature != '') {
                                     $mailer->Body .= $mailer->Signature;
                                 }
                                 $ccs = empty($cc_string) ? array() : explode(',', $cc_string);
                                 $bccs = empty($bcc_string) ? array() : explode(',', $bcc_string);
                                 $emailId = $request->get('emailid');
                                 $attachments = $connector->getAttachmentDetails($emailId);
                                 if ($logo) {
                                     $logo_attach = array('name' => 'logo', 'path' => 'themes/images/', 'attachment' => 'logo_mail.jpg');
                                     $mailer->AddEmbeddedImage($logo_attach['path'] . $logo_attach['attachment'], $logo_attach['name'], $logo_attach['name'] . 'jpg', 'base64', 'image/jpg');
                                 }
                                 $mailer->AddAddress($to);
                                 foreach ($ccs as $cc) {
                                     $mailer->AddCC($cc);
                                 }
                                 foreach ($bccs as $bcc) {
                                     $mailer->AddBCC($bcc);
                                 }
                                 global $root_directory;
                                 if (is_array($attachments)) {
                                     foreach ($attachments as $attachment) {
                                         $fileNameWithPath = $root_directory . $attachment['path'] . $attachment['fileid'] . "_" . $attachment['attachment'];
                                         if (is_file($fileNameWithPath)) {
                                             $mailer->AddAttachment($fileNameWithPath, $attachment['attachment']);
                                         }
                                     }
                                 }
                                 $status = $mailer->Send(true);
                             }
                         }
                         if ($status === true) {
                             $email = CRMEntity::getInstance('Emails');
                             $email->column_fields['assigned_user_id'] = $current_user->id;
                             $email->column_fields['date_start'] = date('Y-m-d');
                             $email->column_fields['time_start'] = date('H:i');
                             $email->column_fields['parent_id'] = $parentIds;
                             $email->column_fields['subject'] = $mailer->Subject;
                             $email->column_fields['description'] = $mailer->Body;
                             $email->column_fields['activitytype'] = 'Emails';
                             $email->column_fields['from_email'] = $mailer->From;
                             $email->column_fields['saved_toid'] = $to_string;
                             $email->column_fields['ccmail'] = $cc_string;
                             $email->column_fields['bccmail'] = $bcc_string;
                             $email->column_fields['email_flag'] = 'SENT';
                             if (empty($emailId)) {
                                 $email->save('Emails');
                             } else {
                                 $email->id = $emailId;
                                 $email->mode = 'edit';
                                 $email->save('Emails');
                             }
                             $response->isJson(true);
                             $response->setResult(array('sent' => true));
                         } else {
                             $response->isJson(true);
                             $response->setError(112, 'please verify outgoing server.');
                         }
                     } else {
                         if ('attachment_dld' == $request->getOperationArg()) {
                             $attachmentName = $request->get('_atname');
                             $attachmentName = str_replace(' ', '_', $attachmentName);
                             if (MailManager_Utils::allowedFileExtension($attachmentName)) {
                                 // This is to handle larger uploads
                                 $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                                 ini_set('memory_limit', $memory_limit);
                                 $mail = new MailManager_Model_Message(false, false);
                                 $mail->readFromDB($request->get('_muid'));
                                 $attachment = $mail->attachments(true, $attachmentName);
                                 if ($attachment[$attachmentName]) {
                                     // Send as downloadable
                                     header("Content-type: application/octet-stream");
                                     header("Pragma: public");
                                     header("Cache-Control: private");
                                     header("Content-Disposition: attachment; filename={$attachmentName}");
                                     echo $attachment[$attachmentName];
                                 } else {
                                     header("Content-Disposition: attachment; filename=INVALIDFILE");
                                     echo "";
                                 }
                             } else {
                                 header("Content-Disposition: attachment; filename=INVALIDFILE");
                                 echo "";
                             }
                             flush();
                             exit;
                         } elseif ('getdraftmail' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftMail = $connector->getDraftMail($request);
                             $response->isJson(true);
                             $response->setResult(array($draftMail));
                         } elseif ('save' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftId = $connector->saveDraft($request);
                             $response->isJson(true);
                             if (!empty($draftId)) {
                                 $response->setResult(array('success' => true, 'emailid' => $draftId));
                             } else {
                                 $response->setResult(array('success' => false, 'error' => "Draft was not saved"));
                             }
                         } elseif ('deleteAttachment' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $deleteResponse = $connector->deleteAttachment($request);
                             $response->isJson(true);
                             $response->setResult(array('success' => $deleteResponse));
                         } elseif ('forward' == $request->getOperationArg()) {
                             $messageId = $request->get('messageid');
                             $folderName = $request->get('folder');
                             $connector = $this->getConnector($folderName);
                             $mail = $connector->openMail($messageId);
                             $attachments = $mail->attachments(true);
                             $draftConnector = $this->getConnector('__vt_drafts');
                             $draftId = $draftConnector->saveDraft($request);
                             if (!empty($attachments)) {
                                 foreach ($attachments as $aName => $aValue) {
                                     $attachInfo = $mail->__SaveAttachmentFile($aName, $aValue);
                                     if (is_array($attachInfo) && !empty($attachInfo) && $attachInfo['size'] > 0) {
                                         if (!MailManager::checkModuleWriteAccessForCurrentUser('Documents')) {
                                             return;
                                         }
                                         $document = CRMEntity::getInstance('Documents');
                                         $document->column_fields['notes_title'] = $attachInfo['name'];
                                         $document->column_fields['filename'] = $attachInfo['name'];
                                         $document->column_fields['filestatus'] = 1;
                                         $document->column_fields['filelocationtype'] = 'I';
                                         $document->column_fields['folderid'] = 1;
                                         // Default Folder
                                         $document->column_fields['filesize'] = $attachInfo['size'];
                                         $document->column_fields['assigned_user_id'] = $current_user->id;
                                         $document->save('Documents');
                                         //save doc-attachment relation
                                         $draftConnector->saveAttachmentRel($document->id, $attachInfo['attachid']);
                                         //save email-doc relation
                                         $draftConnector->saveEmailDocumentRel($draftId, $document->id);
                                         //save email-attachment relation
                                         $draftConnector->saveAttachmentRel($draftId, $attachInfo['attachid']);
                                         $attachmentInfo[] = array('name' => $attachInfo['name'], 'size' => $attachInfo['size'], 'emailid' => $draftId, 'docid' => $document->id);
                                     }
                                     unset($aValue);
                                 }
                             }
                             $response->isJson(true);
                             $response->setResult(array('attachments' => $attachmentInfo, 'emailid' => $draftId));
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }
 /**
  * Function which processes request for Mail Operations
  * @global Integer $list_max_entries_per_page - Number of entries per page
  * @global PearDataBase Instance $adb
  * @global Users Instance $current_user
  * @global String $root_directory 
  * @param MailManager_Request $request
  * @return MailManager_Response
  */
 function process(MailManager_Request $request)
 {
     global $list_max_entries_per_page, $adb, $current_user;
     $response = new MailManager_Response();
     if ('open' == $request->getOperationArg()) {
         $foldername = $request->get('_folder');
         $connector = $this->getConnector($foldername);
         $folder = $connector->folderInstance($foldername);
         $connector->markMailRead($request->get('_msgno'));
         $mail = $connector->openMail($request->get('_msgno'));
         // Get updated count after opening the email
         $connector->updateFolder($folder, SA_MESSAGES | SA_UNSEEN);
         $viewer = $this->getViewer();
         $viewer->assign('FOLDER', $folder);
         $viewer->assign('MAIL', $mail);
         $uicontent = $viewer->fetch($this->getModuleTpl('Mail.Open.tpl'));
         $metainfo = array('from' => $mail->from(), 'subject' => $mail->subject(), 'msgno' => $mail->msgNo(), 'msguid' => $mail->uniqueid(), 'folder' => $foldername);
         $response->isJson(true);
         $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount(), 'ui' => $uicontent, 'meta' => $metainfo));
     } else {
         if ('mark' == $request->getOperationArg()) {
             $foldername = $request->get('_folder');
             $connector = $this->getConnector($foldername);
             $folder = $connector->folderInstance($foldername);
             $connector->updateFolder($folder, SA_UNSEEN);
             if ('unread' == $request->get('_markas')) {
                 $connector->markMailUnread($request->get('_msgno'));
             }
             $response->isJson(true);
             $response->setResult(array('folder' => $foldername, 'unread' => $folder->unreadCount() + 1, 'status' => true, 'msgno' => $request->get('_msgno')));
         } else {
             if ('delete' == $request->getOperationArg()) {
                 $msg_no = $request->get('_msgno');
                 $foldername = $request->get('_folder');
                 $connector = $this->getConnector($foldername);
                 $connector->deleteMail($msg_no);
                 $response->isJson(true);
                 $response->setResult(array('folder' => $foldername, 'status' => true));
             } else {
                 if ('move' == $request->getOperationArg()) {
                     $msg_no = $request->get('_msgno');
                     $foldername = $request->get('_folder');
                     $moveToFolder = $request->get('_moveFolder');
                     $connector = $this->getConnector($foldername);
                     $connector->moveMail($msg_no, $moveToFolder);
                     $response->isJson(true);
                     $response->setResult(array('folder' => $foldername, 'status' => true));
                 } else {
                     if ('send' == $request->getOperationArg()) {
                         require_once 'modules/MailManager/config.inc.php';
                         // This is to handle larger uploads
                         $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                         ini_set('memory_limit', $memory_limit);
                         $to_string = rtrim($request->get('to'), ',');
                         $connector = $this->getConnector('__vt_drafts');
                         if (!empty($to_string)) {
                             $toArray = explode(',', $to_string);
                             foreach ($toArray as $to) {
                                 $relatedtos = MailManager::lookupMailInVtiger($to, $current_user);
                                 if (!empty($relatedtos) && is_array($relatedtos)) {
                                     for ($i = 0; $i < count($relatedtos); $i++) {
                                         if ($i == count($relatedtos) - 1) {
                                             $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                             $parentIds .= $relateto[1] . "@1";
                                         } else {
                                             $relateto = vtws_getIdComponents($relatedtos[$i]['record']);
                                             $parentIds .= $relateto[1] . "@1|";
                                         }
                                     }
                                 }
                             }
                         }
                         $cc_string = rtrim($request->get('cc'), ',');
                         $bcc_string = rtrim($request->get('bcc'), ',');
                         $subject = $request->get('subject');
                         $body = $request->get('body');
                         $fromEmail = $connector->getFromEmailAddress();
                         $userFullName = $current_user->first_name . ' ' . $current_user->last_name;
                         $mailer = new Vtiger_Mailer();
                         $mailer->IsHTML(true);
                         $mailer->ConfigSenderInfo($fromEmail, $userFullName, $current_user->email1);
                         $mailer->Subject = $subject;
                         $mailer->Body = $body;
                         $tos = explode(',', $to_string);
                         $ccs = empty($cc_string) ? array() : explode(',', $cc_string);
                         $bccs = empty($bcc_string) ? array() : explode(',', $bcc_string);
                         $emailId = $request->get('emailid');
                         $attachments = $connector->getAttachmentDetails($emailId);
                         foreach ($tos as $to) {
                             $mailer->AddAddress($to);
                         }
                         foreach ($ccs as $cc) {
                             $mailer->AddCC($cc);
                         }
                         foreach ($bccs as $bcc) {
                             $mailer->AddBCC($bcc);
                         }
                         global $root_directory;
                         if (is_array($attachments)) {
                             foreach ($attachments as $attachment) {
                                 $fileNameWithPath = $root_directory . $attachment['path'] . $attachment['fileid'] . "_" . $attachment['attachment'];
                                 if (is_file($fileNameWithPath)) {
                                     $mailer->AddAttachment($fileNameWithPath, $attachment['attachment']);
                                 }
                             }
                         }
                         $status = $mailer->Send(true);
                         if ($status === true) {
                             $email = CRMEntity::getInstance('Emails');
                             $email->column_fields['assigned_user_id'] = $current_user->id;
                             $email->column_fields['date_start'] = date('Y-m-d');
                             $email->column_fields['time_start'] = date('H:i');
                             $email->column_fields['parent_id'] = $parentIds;
                             $email->column_fields['subject'] = $mailer->Subject;
                             $email->column_fields['description'] = $mailer->Body;
                             $email->column_fields['activitytype'] = 'Emails';
                             $email->column_fields['from_email'] = $mailer->From;
                             $email->column_fields['saved_toid'] = $to_string;
                             $email->column_fields['ccmail'] = $cc_string;
                             $email->column_fields['bccmail'] = $bcc_string;
                             $email->column_fields['email_flag'] = 'SENT';
                             if (empty($emailId)) {
                                 $email->save('Emails');
                             } else {
                                 $email->id = $emailId;
                                 $email->mode = 'edit';
                                 $email->save('Emails');
                             }
                             $response->isJson(true);
                             $response->setResult(array('sent' => true));
                         } else {
                             $response->isJson(true);
                             $response->setError(112, 'please verify outgoing server.');
                         }
                     } else {
                         if ('attachment_dld' == $request->getOperationArg()) {
                             $attachmentName = $request->get('_atname');
                             $attachmentName = str_replace(' ', '_', $attachmentName);
                             if (MailManager_Utils::allowedFileExtension($attachmentName)) {
                                 // This is to handle larger uploads
                                 $memory_limit = ConfigPrefs::get('MEMORY_LIMIT');
                                 ini_set('memory_limit', $memory_limit);
                                 $mail = new MailManager_Model_Message(false, false);
                                 $mail->readFromDB($request->get('_muid'));
                                 $attachment = $mail->attachments(true, $attachmentName);
                                 if ($attachment[$attachmentName]) {
                                     // Send as downloadable
                                     header("Content-type: application/octet-stream");
                                     header("Pragma: public");
                                     header("Cache-Control: private");
                                     // JFV - convert attachment file name encoding
                                     //                    header("Content-Disposition: attachment; filename=$attachmentName");
                                     // refer - http://linux.ohwada.jp/modules/smartsection/item.php?itemid=516
                                     $name = $attachmentName;
                                     // ブラウザを判定する
                                     $ua = $_SERVER['HTTP_USER_AGENT'];
                                     $browser = 'unknown';
                                     if (strstr($ua, 'MSIE') && !strstr($ua, 'Opera')) {
                                         $browser = 'msie';
                                     } elseif (strstr($ua, 'Opera')) {
                                         $browser = 'opera';
                                     } elseif (strstr($ua, 'Firefox')) {
                                         $browser = 'firefox';
                                     } elseif (strstr($ua, "Chrome")) {
                                         $browser = 'chrome';
                                     } elseif (strstr($ua, "Safari")) {
                                         $browser = 'safari';
                                     }
                                     // 英数字だけかを判定する
                                     $ascii = mb_convert_encoding($name, "US-ASCII", "UTF-8");
                                     if ($ascii == $name) {
                                         $browser = 'ascii';
                                     }
                                     // ブラウザに応じた処理
                                     switch ($browser) {
                                         // urlencode する
                                         case 'ascii':
                                             $name = str_replace(' ', '_', $name);
                                             $name = rawurlencode($name);
                                             break;
                                             // SJIS に変換する
                                         // SJIS に変換する
                                         case 'msie':
                                             $name = mb_convert_encoding($name, "SJIS", "UTF-8");
                                             break;
                                             // RFC2231形式を使用する
                                         // RFC2231形式を使用する
                                         case 'firefox':
                                         case 'chrome':
                                         case 'opera':
                                             $name = "utf-8'ja'" . rawurlencode($name);
                                             $is_rfc2231 = true;
                                             break;
                                             // UTF-8 のまま
                                         // UTF-8 のまま
                                         case 'safari':
                                             break;
                                             // 諦めて代替えを使う
                                         // 諦めて代替えを使う
                                         default:
                                             //                    		$name = $name_alt;
                                             $name = str_replace(' ', '_', $name);
                                             $name = rawurlencode($name);
                                             break;
                                     }
                                     if ($is_rfc2231) {
                                         $dis = 'Content-Disposition: attachment; filename*=';
                                     } else {
                                         $dis = 'Content-Disposition: attachment; filename=';
                                     }
                                     header($dis . $name);
                                     // JFV END
                                     echo $attachment[$attachmentName];
                                 } else {
                                     header("Content-Disposition: attachment; filename=INVALIDFILE");
                                     echo "";
                                 }
                             } else {
                                 header("Content-Disposition: attachment; filename=INVALIDFILE");
                                 echo "";
                             }
                             flush();
                             exit;
                         } elseif ('getdraftmail' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftMail = $connector->getDraftMail($request);
                             $response->isJson(true);
                             $response->setResult(array($draftMail));
                         } elseif ('save' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $draftId = $connector->saveDraft($request);
                             $response->isJson(true);
                             if (!empty($draftId)) {
                                 $response->setResult(array('success' => true, 'emailid' => $draftId));
                             } else {
                                 $response->setResult(array('success' => false, 'error' => "Draft was not saved"));
                             }
                         } elseif ('deleteAttachment' == $request->getOperationArg()) {
                             $connector = $this->getConnector('__vt_drafts');
                             $deleteResponse = $connector->deleteAttachment($request);
                             $response->isJson(true);
                             $response->setResult(array('success' => $deleteResponse));
                         } elseif ('forward' == $request->getOperationArg()) {
                             $messageId = $request->get('messageid');
                             $folderName = $request->get('folder');
                             $connector = $this->getConnector($folderName);
                             $mail = $connector->openMail($messageId);
                             $attachments = $mail->attachments(true);
                             $draftConnector = $this->getConnector('__vt_drafts');
                             $draftId = $draftConnector->saveDraft($request);
                             if (!empty($attachments)) {
                                 foreach ($attachments as $aName => $aValue) {
                                     $attachInfo = $mail->__SaveAttachmentFile($aName, $aValue);
                                     if (is_array($attachInfo) && !empty($attachInfo) && $attachInfo['size'] > 0) {
                                         if (!MailManager::checkModuleWriteAccessForCurrentUser('Documents')) {
                                             return;
                                         }
                                         $document = CRMEntity::getInstance('Documents');
                                         $document->column_fields['notes_title'] = $attachInfo['name'];
                                         $document->column_fields['filename'] = $attachInfo['name'];
                                         $document->column_fields['filestatus'] = 1;
                                         $document->column_fields['filelocationtype'] = 'I';
                                         $document->column_fields['folderid'] = 1;
                                         // Default Folder
                                         $document->column_fields['filesize'] = $attachInfo['size'];
                                         $document->column_fields['assigned_user_id'] = $current_user->id;
                                         $document->save('Documents');
                                         //save doc-attachment relation
                                         $draftConnector->saveAttachmentRel($document->id, $attachInfo['attachid']);
                                         //save email-doc relation
                                         $draftConnector->saveEmailDocumentRel($draftId, $document->id);
                                         //save email-attachment relation
                                         $draftConnector->saveAttachmentRel($draftId, $attachInfo['attachid']);
                                         $attachmentInfo[] = array('name' => $attachInfo['name'], 'size' => $attachInfo['size'], 'emailid' => $draftId, 'docid' => $document->id);
                                     }
                                     unset($aValue);
                                 }
                             }
                             $response->isJson(true);
                             $response->setResult(array('attachments' => $attachmentInfo, 'emailid' => $draftId));
                         }
                     }
                 }
             }
         }
     }
     return $response;
 }