email-attachmentsmirthmirth-connect

Mirth Connect: Processing Attachment


Mirth Connect Server 3.5.1, Java version: 1.8.0_171

I've successfully sent form-data from POSTMAN (see postman.xml) to MIRTH (see below JavaScript Writer). My JavaScript Writer can grab all info except I got stuck on what to do with the base64 file content.

This code run without error but the email I received got no attachment. Everything else is good.

postman.xml:

<HttpRequest>
   <Content boundary="--------------------------287734394493965736503744" multipart="yes">
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="channelFilter"</Content-Disposition>
         </Headers>
         <Content multipart="no">attachment</Content>
      </Part>
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="to"</Content-Disposition>
         </Headers>
         <Content multipart="no">coisox@gmail.com</Content>
      </Part>
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="cc"</Content-Disposition>
         </Headers>
         <Content multipart="no">coisox3@gmail.com</Content>
      </Part>
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="subject"</Content-Disposition>
         </Headers>
         <Content multipart="no">From Postman With Attachment</Content>
      </Part>
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="body"</Content-Disposition>
         </Headers>
         <Content multipart="no">Lalala</Content>
      </Part>
      <Part>
         <Headers>
            <Content-Disposition>form-data; name="file"; filename="xls.png"</Content-Disposition>
            <Content-Type>image/png</Content-Type>
         </Headers>
         <Content encoding="Base64" multipart="no">iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAA3NCSVQICAjb4U/gAAAACXBIWXMA AAG7AAABuwE67OPiAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAWJQTFRF ////4uLrhLtZ4uXmwcbLhL1a4ubn4uXnhL1ahL1bhb5bhr5dh79eh79fiL9fiL9gicBgi8FjjMFl jcJmjsJoj8Noj8NpkMNqkcRrksVtk8VvlMZvlcZxlsdyl8d0mMV4mch2msl4nMp6nst9oMyAoc2B o8iMo86Epc+Gps+Ip9CJqtGMq9KOr9SUsLe9sNWVsdWWs9aZtLvAtbzBtdebttidt9ifuNmfutqi u9qjvNumwN2qwN2rwcfMxuCyyOG1yeK3ytHYytTUzNzGzeS8zuW9zuW+z9Xb0ObA0ebB0ebC0+fD 1tvf1unH1unI2OrL2evN2+zP3ODj3eDj3u3S3u3T4O7V4uXn4u/Y5PDa5PHb5fHc5fHd5vLe5/Lf 6PPh6fPi7vbo7/bp7/bq8Pfq8ffs8vju8/nv9Pnw9Pnx9frx9fry9/v1+Pv1+fz3+vz5+/35/P36 /P37/f78///+////UwWFaAAAAAh0Uk5TABo8psDJ5upYXiAEAAAB00lEQVRYw93X90/CQBQHcLBa nrhn3RsHiooK7j1Q3LPiXrhBEd//b4GqRW3vtedPfJP2JZfcJ3m53F3OZtPGLoiyQbJtjNgdsmF6 WYIgMwCWIDIBhiCzAWOBAhgKJMBIoAEGAhHQF6iArkAG9AQ6oCOwgEGWwAImexkCC9gdYAgyU5g0 7kImZG32O5YArWAN0AgWgW/BKvAlWAY+BeuAKnAAKYEHSApcQELgAxSBE5DXeAH5/4CsHDCRxvlf gKn5ivALAJPJbKA4VfLUT41rdMxTQAL8cW+ieF580BAdVgcrQqjkwU0BPPjWBtDyil7owaA6OIMn QxMHsWlSC0G8qyq7wXXQAFGsU/6FThJQcIRH+3herAXCOJ5HXwXpFvGpBrSA7x2fVzvJy7iCuAlp ADRvvSNeuGlAH8ZiOJIOAFR6Q/haSwGaX9DvxXjHDwAg9wz7CUDpFW4ALOJjtQKsS5JU6QTnwlgT QHkEuwnAEl6WKEtxjNsKkMx1UXkcMbwXwXAJAZgKtyRK9eky1NwngdN86DpU6ttyHcdmqm91SZl+ Hpg8VNs5j/X2Od2LZZaWACcQ2Pn/qy2DANHcfNH00/dHBNOP7/Q47H883wVyF6KQmv8Bq6jVN1IO SxsAAAAASUVORK5CYII=</Content>
      </Part>
   </Content>
</HttpRequest>

JavaScript Writer:

var email = new Packages.org.apache.commons.mail.SimpleEmail();

email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new Packages.org.apache.commons.mail.DefaultAuthenticator("myusername", "mypassword"));
email.setSSLOnConnect(true);

var part = connectorMessage.getRawData().split('<Part>');
for(i=1; i<part.length; i++) {
    var disposition = part[i].split('<Content-Disposition>')[1].split('</Content-Disposition>')[0];
    var name = disposition.split('; name="')[1].split('"')[0];

    var value = '';
    if(disposition.indexOf('; filename="')>-1) {
        var filename = disposition.split('; filename="')[1].split('"')[0];
        var type = part[i].split('<Content-Type>')[1].split('</Content-Type>')[0];
        value = part[i].split('<Content encoding="Base64" multipart="no">')[1].split('</Content>')[0];
        //var file = addAttachment(value.encodedData, type);
        var file = addAttachment(value, type);
    }
    else {
        value = part[i].split('<Content multipart="no">')[1].split('</Content>')[0];
    }

    email.setFrom("test@gmail.com");
    if(name=='to') email.addTo(value);
    else if(name=='cc') email.addCc(value);
    else if(name=='subject') email.setSubject(value);
    else if(name=='body') email.setMsg(value);
}

email.send();

Solution

  • Your problem is, that you are using addAttachment method. If you just call this method like you do, then it has nothing to do with the email object that you are putting together, but it instead adds an attachment to the Mirth message. You should see an attachments tab with your attachments in the channel for the messages you have processed.

    Read the documentation how to add attachments in the email library you are using, and act accordingly. Maybe it is as simple as calling email.addAttachment(), but I don't know.

    PS. Instead of splitting the XML string by hand, why not use Mirth's XML handling capabilities to access the different parts of XML?

    PPS. Why not use Mirth's SMTP sender instead of doing it all by hand?