This is a public Forum  publicRSS

Topic

    Saravanan S
    Create attachment or Send attachments via email of...Answered
    Topic posted June 12, 2017 by Saravanan SSpecialist, last edited June 12, 2017 
    89 Views, 13 Comments
    Title:
    Create attachment or Send attachments via email of Incident,Contact and Organization
    Content:

    Hi All,

     

    My Requirement is to attach or send email Contact's attachments and Organization's attachment while changing the incident status.

    I have stored the contact and org attachments in an array and tried to send in mail but it does not work. I have attached the snapshot here.

    But when I tried to send with the array location (like "$attach_array[0]") attachment is sending successfully.

    Please help me to achieve the process.

     

    Thanks,

    S.Saravanan

    Code Block:

    Best Answer

    Bastiaan van der Kooij

    Just checked and the FileAttachment array is an object and not an actual array, so it does not come with the normal php array functions. You should try to understand the objects that are required, so you can transform them in case it is needed. Then it makes sense what Barilitio says, that a loop is required to create a new File Attachment array;

    $Att = new RNCPHP\FileAttachmentArray();
    foreach ($Contact->FileAttachments as $item)
        $Att[count($Att)] = $item;
    foreach ($Organization->FileAttachments as $item)
        $Att[count($Att)] = $item;
    
    $mm->FileAttachments = $Att;
    

    Answer

     

    • Barrilito

      Hi S.Saravanan,

      I am not sure what your exact issue is here. You say that some part works, and some other part does not. But what is it precisely you want to do?

      I guess you want the two attachments to be sent, but you only see one attachment being sent, correct?

      If correct, then I would guess, looking at your code, that you need to address the attachments to the FileAttachment array correctly. I have not done so much with attachments, so I am just looking at what I see from the documentation and from your code.

      First of all, something like this does not make sense to me:

      $MailMessage->FileAttachments[] = $all_attach[0].$all_attach[1]; --> Not Working

      You try to concatenate two objects / arrays as it are attachment objects, it are not strings, so that won't work indeed...

      I would guess you need to do something like this:

      $MailMessage->FileAttachments[0] = $all_attach[0];
      $MailMessage->FileAttachments[1] = $all_attach[1];

      Regards

    • Saravanan S

      Hi Barrilito,

      Thanks for your response.

      Your understanding is correct, I want to send two object's attachments in a same mail (Contact and Organization)

      $MailMessage->FileAttachments[0] = $all_attach[0];
      $MailMessage->FileAttachments[1] = $all_attach[1];

      I tried the above method  too, but its also not working.

       

      Thanks,

      S.Saravanan

    • Barrilito

      Hi,

      Ok.

      Well, in general you should set up the message like this:

        $mm = new RNCPHP\MailMessage();
        $mm->FileAttachments = $obj->FileAttachments;  

      (where $obj is the object desired, say contact, incident, whatever). You set that up correct I think.

      But in that part you want to combine attachments from different objects. I think the combination won't work as both objects will return different arrays. For example a 'FileAttachments' field of Incident returns a FileAttachmentIncidentArray , while the 'FileAttachments' field of MailMessage expects a FileAttachmentArray . So try to look at what kind of array you get back from the contact and organization and do something with those arrays to get them in one format.

      Regards

    • Bastiaan van der Kooij

      That would make sense, the $MailMessage->FileAttachments expects a FileAttachments array and not an array of FileAttachments arrays. So try something like;

      $MailMessage->FileAttachments = array_merge((array)$Contact->FileAttachments, (array)$Organization->FileAttachments);
      

      Bastiaan

    • Saravanan S

      Hi barrilito,

      Yes from different object is the problem here, ill look what kind of array it returns and try.

      Regards,

      S.Saravanan

    • Saravanan S

      Hi Bastiaan,

      Thanks for your response, I tried with your suggestion but this showing a below error,

      array_merge():Argument #1 is not an array

      Is there anything I missed?

      Regards,
      S.Saravanan

    • Saravanan S

      HI Barrilito,

      But when I tried to send contact attachment alone or Org attachment alone its working fine.

      Issue is sending both obj attachments (array) in mail.

      Thanks,

      S.Saravanan

    • Barrilito

      Hi,

      I am sure this should be possible. And I agree that they work seperately, so the data is ok.

      I have not that much time to really dive into and test something myself. Please try to look in detail what kind of arrays are returned.

      Perhaps even try it without the explicit casting to an array like this:

      $MailMessage->FileAttachments = array_merge($Contact->FileAttachments, $Organization->FileAttachments);
      

      Otherwise I would suggest to print_r the output of the arrays and to build some new array.

      Regards

    • Barrilito

      ps. just to be sure: File attachments should be saved to an object before they can be used in the mail message!

    • Barrilito

      ps. My last remark for today (as my day ends), what version do you work with? I just read elsewhere on the community that in previous older versions a simple thing like $mm->FileAttachments = $obj->FileAttachments; did not work. They fixed that using a loop construction:

      j=0;

      if(!empty($obj->FileAttachments)) {
          $mm->FileAttachments = new RNCPHP\FileAttachmentArray();
          foreach ($obj->FileAttachments as $key => $file) {
              $mm->FileAttachments[$j] = $file; 
              $j++;
          }

      Hope you get it to work.

      Regards

    • Bastiaan van der Kooij

      Just checked and the FileAttachment array is an object and not an actual array, so it does not come with the normal php array functions. You should try to understand the objects that are required, so you can transform them in case it is needed. Then it makes sense what Barilitio says, that a loop is required to create a new File Attachment array;

      $Att = new RNCPHP\FileAttachmentArray();
      foreach ($Contact->FileAttachments as $item)
          $Att[count($Att)] = $item;
      foreach ($Organization->FileAttachments as $item)
          $Att[count($Att)] = $item;
      
      $mm->FileAttachments = $Att;
      
    • Barrilito

      Thanks for testing Bastiaan yes

      @S.Saravanan: makes sense? / Will this work for you?

      Regards

    • Saravanan S

      Hi Bastiaan,

      Thanks for your input. 

      Process achieved with help of the provided content.

      Hi Barrilito,

      Yes its working.

      Thanks for your help.

       

      Rergards,

      S.Saravanan