Send email using formula and attach document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    Send email using formula and attach document

    Hi Folks,
    I have a simple workflow for contact, when a document is created for the contact i want to send an email to the contact using formula and want to attach the contact related document file into the email as attachment, any help will be much appreciated. below code that didn't work.
    Code:
    // Find document related to the contact
    $documentId = record\findRelatedOne('Contact', id, 'documents', 'createdAt', 'desc');
    // get document name
    $documentName = record\attribute('Document', $documentId, 'name');
    // Get the attachment for the given document
    $attachmentId = record\findOne('Attachment', 'relatedId', $documentId);
    // Prepare email body content
    $emailBody = string\concatenate( 'Please find attached required document', $documentName, '\n', 'Cheers', '\n' );
    // Create email record
    $emailId = record\create('Email', 'to', entity\attribute('emailAddress'), 'status', 'Sending', 'subject', 'Required Document', 'body', $emailBody, 'isHtml', true, 'attachmentsIds', list($attachmentId) );
    // Send email
    ext\email\send($emailId);
    Many thanks
    Rabii
    Web Dev
  • Vadym
    Super Moderator
    • Jun 2021
    • 345

    #2
    Hi rabii,

    Code:
    $documentId = record\findRelatedOne('Contact', id, 'documents', 'createdAt', 'desc');
    
    $documentName = record\attribute('Document', $documentId, 'name');
    
    $fileId = record\attribute('Document', $documentId, 'fileId');
    
    $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')
    );
    
    $emailBody = string\concatenate(
    'Please find attached required document: ', $documentName, '<br> Cheers');
    
    $emailId = record\create(
    'Email',
    'to', entity\attribute('emailAddress'),
    'subject', 'Required Document',
    'body', $emailBody,
    'isHtml', true,
    'status', 'Sending',
    'attachmentsIds', list($attachmentId),
    'parentId', id,
    'parentType', 'Contact'
    );
    
    // ext\email\applyTemplate($emailId, 'some-email-template-id');
    // (Note, when using a template, the following parameters described above do not need to be defined: 'subject', 'body', 'is Html')
    
    ext\email\send($emailId);
    Last edited by Vadym; 12-01-2021, 08:30 AM.

    Comment

    • muulox
      Member
      • Jan 2022
      • 31

      #3
      Hello,

      I integrated this code in my espocrm but the system is sending 2 emails separately 1. The attachemnt / 2. My template.
      My question: in this code ""ext\email\applyTemplate($emailId, 'some-email-template-id'); i need to modify the 'some-email-template-id' ?
      What i'm doing wrong?
      Thank you!​
      Attached Files

      Comment


      • rabii
        rabii commented
        Editing a comment
        hey i have noticed that you use both code below

        Code:
        ext\email\applyTemplate($emailId, 'some-email-template-id');
        // (Note, when using a template, the following parameters described above do not need to be defined: 'subject', 'body', 'is Html')
        ext\email\send($emailId);
        both lines of code above will send email that is why the email was sent twice, you should just use one of them. the first line of code
        Code:
        ext\email\applyTemplate($emailId, 'some-email-template-id');
        allows you to send an email using a template and you need to replace some-email-template-id with an Id of an email template in your system. otherwise if you want to inline the body the you should just use
        Code:
         ext\email\send($emailId);
        read this to understand the different between both functions. https://docs.espocrm.com/administrat.../#extemailsend

        hope this helps
    • muulox
      Member
      • Jan 2022
      • 31

      #4
      Thank you! Another question where i find the email template id?
      I got it, it is the url /#EmailTemplate/view/63e1386e46138191b
      Last edited by muulox; 02-07-2023, 12:27 PM.

      Comment


      • rabii
        rabii commented
        Editing a comment
        that is correct
    Working...