How to apply <base64-encoder-transformer/> on message.OutboundAttachment in Mule

shrinathM picture shrinathM · Aug 14, 2014 · Viewed 7.3k times · Source

I have a base64Binary encoded file(eg:pdf,img,doc). In Mule I'm setting this encoded data as an outBoundAttachment to eventually send it as attachments using SMTP. How to decode or apply decoding transformer only to the outbound attachment and send it in smtp.

<set-attachment attachmentName="xml.pdf" value="#[flowVars['pdf']]" contentType="application/pdf" doc:name="Attachment"/>
<base64-decoder-transformer />
<string-to-byte-array-transformer />
<smtp:outbound-endpoint host="smtp.gmail.com" user="${username}" password="${pwd}"  connector-ref="smtpConnector" to="${toAddress}"  from="${fromAddress}" mimeType="text/html" subject="This is sample from mule"   responseTimeout="10000" doc:name="SMTP">
 <email:object-to-mime-transformer useOutboundAttachments="true"/>
 </smtp:outbound-endpoint> 

Currently I'm getting:

'Failed to transform from "base64" to "org.mule.transformer.types.SimpleDataType"'
'Bad Base64 Input character at 0:60(decimal)'

Answer

Ryan Carter picture Ryan Carter · Aug 14, 2014

Most transformers only work on the payload of the message. To apply it to a property or a variable you can use an enricher and make the variable the payload first before adding it back as a property. This way the original payload is not overwritten. Example:

   <enricher target="#[flowVars.myvar]">
      <processor-chain>
         <set-payload value="#[flowVars.myvar]" />
         <base64-decoder-transformer />
      </processor-chain>
   </enricher>

More on enrichment here: http://www.mulesoft.org/documentation/display/current/Message+Enricher