Attach A Pre-Existing Attachment To Email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dodom2
    Senior Member
    • Jan 2020
    • 198

    Attach A Pre-Existing Attachment To Email

    Im looking for guidance on how we can attach a already pre-existing file to a outgoing email thats generated via formula. In my use case Im generating a log file from a external script then creating a record via api. Once this record is created I would like to trigger a BPM to send this log file to a certain email. I cant seem to make it work. Does anyone have a solution for this without custom work?
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    This is how you can do it using Script Formula, code example below:
    PHP Code:
    $attachmentId = record\create('Attachment',
        'role', 'Attachment',
        'type', record\attribute('Attachment', fileId, 'type'),
        'size', record\attribute('Attachment', fileId, 'size'),
        'global', record\attribute('Attachment', fileId, 'global'),
        'name', record\attribute('Attachment', fileId, 'name'),
        'sourceId', fileId,
        'storage', record\attribute('Attachment', fileId, 'storage')
    );
    
    $emailId = record\create('Email',
        'to', $to,
        'status', 'Sending',
        'attachmentsIds', list($attachmentId),
        'parentId', id,
        'parentType', 'Contact'
    );
    
    //applying email template Id
    ext\email\applyTemplate($emailId, '60f96b0fb996194ff');
    ext\email\send($emailId);      
    read this also https://forum.espocrm.com/forum/gene...7202#post87202

    Hope this helps
    Rabii
    Web Dev

    Comment


    • dodom2
      dodom2 commented
      Editing a comment
      Thank you Rabii! This works! Life saver my friend!

      $name=client.name;
      $attachmentId = record\create('Attachment',
      'role', 'Attachment',
      'type', record\attribute('Attachment', batchFileId, 'type'),
      'size', record\attribute('Attachment', batchFileId, 'size'),
      'global', record\attribute('Attachment', batchFileId, 'global'),
      'name', record\attribute('Attachment', batchFileId, 'name'),
      'sourceId', batchFileId,
      'storage', record\attribute('Attachment', batchFileId, 'storage')
      );
      $emailId = record\create(
      'Email',
      'to', 'WHATEVER EMAIL ADDRESS',
      'subject', 'LOG FILE DELIVERY NOTICE',
      'isHtml', true,
      'status', 'Sending',
      'attachmentsIds', list($attachmentId),
      'parentId', id,
      'parentType', 'ENTITY NAME'
      );

      ext\email\applyTemplate($emailId, '630a4c0765a160ef0');

      ext\email\send($emailId);

      // applying email template
      ext\email\applyTemplate($emailId, '6406695f4152e6fb3');
      ext\email\send($emailId);

    • rabii
      rabii commented
      Editing a comment
      Glad it worked you are welcome
Working...