Save e-mail attachment as a Case document

Hello,

We would like to have a functionnality allowing to save an e-mail attachment to a Case without leaving sugarcrm interface. Right now the only way to do this is manually by downloading the file and then upload it as a document.

What is the best way to implement this ? Using the API ? (https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.9/Integration/Web_Services/v10/Exampl… )

Attachment in mail :

Goal (link attachment as a document to the case):

Thank you for your help

  • We had the same requirement some years ago (2012). 

    The requirement was to have every attachment to inbound or outbound emails on Case queues to be in a "Case Attachments" module on the Case.

    From a technical standpoint, I regret satisfying that request because each file has to be duplicated on the file system, so there is a copy for the attachment on the email and a copy for the attachment on the Case. 

    Having said that, here is how we did this:

    We wanted to keep these attachments separate from the "Documents", so I created a module called Case Attachments (attch_CaseAttachments) using Module Builder as a "documents" type module and added a relationship between that module and the Cases module.

     

    In the Emails module I added an after_relationship_add logic hook that checks if the related module is a Case and does a number of things, among others, it copies the email attachment into the case attachment module.

     

     

     

    <?php
    class EmailCaseHandling
    {
       // Email RELATED to a case
       function update_on_relationship ($bean,$event,$arguments){
            //bean = Email
            //case retrieved from related_id
            require_once('custom/entry_points/Cases/saveCaseAttachments.php');
            if ($arguments['related_module'] =='Cases'){
                    $case = BeanFactory::retrieveBean('Cases', $arguments['related_id'], array('disable_row_level_security' => true));
                    $GLOBALS['log']->debug("EmailCaseHandling::update_on_relationship; Case: " . $case->id . " Email: " . $bean->id);
                    //save any case attachments and link them to the case
                    if(!empty($bean->id) && !empty($case->id)) saveCaseAttachments($case, $bean);
       // ...do other stuff.

     

    I keep the custom function in the entry_points directory though it's not strictly an entry point. A legacy choice I plan to improve in the near future.

     

    <?php
    //given a case bean and an email bean add any attachments found on the email to the Case Attachments module and link the Case Attachments record to the case.
    function saveCaseAttachments($case,$email){
      $GLOBALS['log']->debug("SaveCaseAttachments: caseId = " . $case->id . " emailId = ". $email->id);
      if(!empty($email->id) && ! empty($case->id)){
        $GLOBALS['log']->debug("SaveCaseAttachments: Case and Email Beans Loaded");
        if($email->load_relationship('attachments')){
          foreach ($email->attachments->getBeans() as $n){
            $GLOBALS['log']->debug("SaveCaseAttachments: Copy note id ".$n->id);
            $a  = BeanFactory::newBean('attch_CaseAttachments');
            if(isset($n->name) && $n->name != ''){
              $a->document_name = $n->name;
            }else{
              $a->document_name = $n->filename;
            }
            $a->filename = $n->filename;
            $a->file_mime_type = $n->file_mime_type;
            //emails from addresses in our domain are outgoing, others are considered incoming
            if(strstr("mydomain.com",strstr("@", $email->from_addr))){
              $a->category_id = 'outgoing';
            }else{
              $a->category_id = 'incoming';
            }
            $a->save();
            $attach_id = $a->id;
            if (!empty($attach_id)){
              // copy the email attachment to the upload directory with the id of the attachment record
              // the file and id of the record have to match for the attachment to be downloadable properly
              // dir must be hardcoded, not sure why TODO: get dir from config
              if(!file_exists("upload/{$attach_id}")){
                copy("upload/{$n->id}","upload/{$attach_id}");
              }
              // link the case to the attachment
              $GLOBALS['log']->debug("SaveCaseAttachments: Link Attachment {$attach_id} to Case");
              if($case->load_relationship('attch_caseattachments_cases') && !empty($attach_id)){
                $case->attch_caseattachments_cases->add($attach_id);
              }
            }
          }
        }
      }
      return;
    }

    There are probably better ways to do this now that Emails are sidecar. As I said this is a vintage piece of code from my early days of Sugar (2012) that I just have not gotten around to revising. 

    Francesca