Sending email attachment in Mule - Why am I getting payload attachment - mule

I am sending an email with a csv attachment in Mule using the SMTP connector.
The email generated from Mule has two attachments on it.
The first is a file named Payload.
The second is the file I explicitly added myFile<date>.csv
How can I get rid of the Payload attachment? From my understanding what is in Payload file should be appearing as the content of the email. But that is not quite what is happening and instead it is appearing as an attachment.
Here is what my sub-flow that sends the email looks like. The payload coming in is a csv.
<sub-flow name="paymentReconSendMailSubFlow">
<set-attachment attachmentName="myFile-#[server.dateTime.format("yyyyMMddHHmmss") + '.csv']" value="#[payload]" contentType="application/csv" doc:name="Attachment"/>
<set-payload doc:name="Set Payload" value="#[null]"/>
<smtps:outbound-endpoint host="${smtp.host}" port="${smtp.port}" user="${smtp.username}" password="${smtp.password}" connector-ref="SMTP" to="${smtp.toAddress}" from="${smtp.fromAddress}" subject="BLAH" replyTo="${smtp.replyToAddress}" responseTimeout="10000" mimeType="text/html" doc:name="SMTP"/>
</sub-flow>
And here is what I get in my inbox.

It looks like Mule still has the content type of the message payload set to application/csv, so thinks your null payload is a CSV. Try adding mimeType="text/plain" to the set-payload element

Could you try to serialize the csv file to a byte array before attaching?
for example:
<file:inbound-endpoint path="${output.folder}" doc:name="File"/>
<file:file-to-byte-array-transformer doc:name="File to String"/>
<set-attachment value="#[message.payload]" contentType="application/csv"
attachmentName="#['myfile.csv']"
doc:name="Attachment" />
Let us know if is working and the issue is the serialisation of the file before sending

Related

How to convert org.glassfish.grizzly.utils.BufferInputStream to JSON in Mule?

On my first steps with Mule I'm writing a basic Http Proxy.
Currently I forward the request to the api server and what I'd like to do is reading the payload that I receive from it before responding to the client.
When I try to log it with #[payload] it prints
org.glassfish.grizzly.utils.BufferInputStream#2306df30
How can I print it properly in JSON format?
The full code:
<flow name="proxy">
<http:listener config-ref="http-lc-0.0.0.0-8081" path="![p['proxy.path']]" parseRequest="false"/>
<http:request config-ref="http-request-config" method="#[message.inboundProperties['http.method']]"
path="#[message.inboundProperties['http.request.path'].substring(message.inboundProperties['http.listener.path'].length()-2)]" parseResponse="false">
<http:request-builder>
<http:query-params expression="#[message.inboundProperties.'http.query.params']"/>
</http:request-builder>
<http:success-status-code-validator values="0..599" />
</http:request>
<logger doc:name="Logger" level="INFO" message="Payload #[payload]"/>
The payload after HTTP request is generally in stream format, ref:- https://docs.mulesoft.com/mule-user-guide/v/3.7/http-request-connector
There are two ways you can get the payload after http:request
1) <object-to-string-transformer doc:name="Object to String"/> after http:request
or
2) using a logger and use MEL expression <logger message="#[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
Try #[message.payloadAs(java.lang.String)] which will log the expected output.
Hope this helps.
The http component will send as InputStream,So use byte array to string transformer after http component.If you just want to print you can use #[message.payloadAs(java.lang.String)] but you wanna do any operation just drag and drop a byte array to string transformer
Simplest way is to just use <object-to-string-transformer doc:name="Object to String"/> after http request component and then place a logger with #[payload].
One way to make sure that you get proper JSON response is to set payload as application/json mimetype.
<set-payload value="#[payload]" mimeType="application/json" doc:name="Set Payload"/>
Add this line after the HTTP Request. Just setting the payload value as #[payload] will convert it to json While converting it to string will just print it as string and not make it Json type.

Return individual values from linkedHashMap in Mule

I have an output from a webservice in Mule that returns a linkedHashMap and I need to get the individual values to be dynamically inserted into a template. The template is used to send email through the SMTP connector. I can get all values using MEL #[payload], but I can't get them one by one. I've tried #[payload.get(0)], #[payload[0]] but they all return null.
The Mule XML looks like this:
<flow name="MW_Flow">
<file:inbound-endpoint path="C:\....\1" connector- ref="File" responseTimeout="10000" doc:name="File" pollingFrequency="60000"/>
<ws:consumer config-ref="File_Read_WS" operation="all3" doc:name="FileRead DBWriter WS"/>
<dw:transform-message metadata:id="6ee92ba8-9f67-40d6-bfa3-3e237da20822" doc:name="Transform Message">
<foreach doc:name="For Each">
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
<parse-template location="C:\.....\Templates\Mail.txt" metadata:id="b7d894eb-465b-47f7-a542-b49fc4fb53d9" doc:name="Parse Template"/>
<logger message="2: #[message.exception] #[message.dataType] #[payload]" level="INFO" doc:name="Logger"/>
</foreach>
</flow>
The template (plain text file) looks a bit like this:
Hello [name].
This is email from [name2]. The following event [event].....
All I get are null values except when using #[payload] which returns the whole row (4 values).
Any help greatly appreciated!
/Johan
If your payload is a Map then payload.get(0) or payload[0] will behave as if you are trying to get a value from map with 0 as key, which I guess doesn't exist in your map.
Try accessing it with name - #[payload.name] or #[payload.name2] or #[payload[name]]

Mule foreach : Splitter returned no results

I get a list of files on amazon S3 and iterate over the list of files and process one file at a time. The corresponding flow is as follows --
<flow name="process-from-s3" doc:name="process-from-s3"
processingStrategy="synchronous">
<poll doc:name="Poll" frequency="${s3-poll-interval}">
<s3:list-objects config-ref="Amazon_S3" doc:name="Get List of files"
accessKey="${s3-access-key}" secretKey="${s3-secret-key}"
bucketName="${s3-read-bucket}" />
</poll>
<choice doc:name="Choice">
<foreach doc:name="For Each">
<set-session-variable variableName="s3_file_name" value="#[payload.getKey()]" doc:name="Session Variable"/>
<logger message="From bucket ( ${s3-read-bucket} ), received the file #[s3_file_name]" level="INFO" doc:name="Logger"/>
<flow-ref name="process_s3_file" doc:name="Flow Reference"/>
</foreach>
</choice>
</flow>
The flow works well, however it keeps on spitting the following log statements if there are no files found.
[03-06 21:52:05] WARN Foreach$CollectionMapSplitter
[[myapp].connector.polling.mule.default.receiver.01]: Splitter returned no results.
If this is not expected, please check your split expression
How can I avoid this annoying log message. Should I wrap the foreach within a choice router that processes the foreach if there is atleast one element in the list. Any suggestions are welcome.
I would rather set the log level for org.mule.routing.Foreach$CollectionMapSplitter to ERROR than configure any additional logic for this warning. See Mule docs for configuring logger/log4j if you need to.

Sending a file attachement with Mule

I have seen this answer but it does not show how you use the MEL to send the file in the value field. If you enter some value in there that is the content of the file. I assume you have to move the payload from the file endpoint connector to the attachment value property using MEL.
Also how can you set the content type dynamically
Mule SMTP - send email with attachment
Thanks
Jaco.
You can use the file-to-string-transformer to transform your file to string. You can also use Mule variables, properties, etc for defining the content type or other params. Example:
<file:inbound-endpoint path="/tmp/attachments" responseTimeout="10000"/>
<file:file-to-byte-array-transformer/>
<set-variable variableName="ct" value="test/plain" />
<set-attachment attachmentName="#[message.outboundProperties.filename]" value="#[payload]" contentType="#[flowVars['ct']]"/>
<set-payload value="this is my message"/>
<smtp:outbound-endpoint...

How to parse inbound e-mail when using Mule's IMAP transport?

I am just starting to learn mule so please forgive me if this is a very basic question.
I have successfully configured mule to monitor my mail box so whenever a mail comes in I write the mail to text file (just for testing).
Now I need to parse this mail and get the message / From / To from the mail.
<flow name="testFlow" doc:name="testFlow">
<imap:inbound-endpoint host="ip"
port="143" user="username" password="pwd" doc:name="IMAP"
responseTimeout="10000" transformer-refs="Message_Properties">
</imap:inbound-endpoint>
<file:outbound-endpoint path="C:\"
outputPattern="#[function:datestamp].dat" doc:name="File">
</file:outbound-endpoint>
</flow>
I thought I would be able to get the header information using the below expression (using expression transformer)
#[inboundProperties['email-header-name']]
but this doesn't seem to work. I also tried the below expressions but didn't work,
#[map-payload:HOST]
#[map-payload:MESSAGE]
#[map-payload:TIMESTAMP]
Can someone help? Also does anyone know if there is a document with the list of available expressions?
EDIT:
I included the logger to view the properties and it displayed all the properties but I couldnt find the body.. Also, I tried getting the fromAddress using
#[map-payload:fromAddress] and #[inboundProperties['fromAddress']]
but didn't work, can someone let me know where I am wrong?
The properties are something like below,
inbound.fromAddress=Service <service#xx.com>
inbound.subject=Test Final inbound.toAddresses=Service
Edit 2:
This is what I have tried but doesnt work :(
I am trying to retrieve the email subject and write to file or just show it using logger based on email subject but I am getting the below error.
ERROR 2013-02-07 19:22:45,275 [[test].connector.file.mule.default.dispatcher.01] org.mule.exception.DefaultMessagingExceptionStrategy:
********************************************************************************
Message : Could not find a transformer to transform "SimpleDataType{type=javax.mail.internet.MimeMessage, mimeType='*/*'}" to "SimpleDataType{type=java.io.InputStream, mimeType='*/*'}".
Code : MULE_ERROR-236
Can you let me know where I am making mistake?
<flow name="testFlow1" doc:name="testFlow1">
<imap:inbound-endpoint host="ip"
port="143" user="uname" password="pwd" doc:name="IMAP"
responseTimeout="10000" disableTransportTransformer="true">
</imap:inbound-endpoint>
<logger message="#[message.inboundProperties['inbound.fromAddress']]" level="INFO" doc:name="Logger"/>
<choice doc:name="Choice">
<when expression="message.inboundProperties['inbound.subject']=='plain test'">
<processor-chain>
<logger message="#[message.inboundProperties['inbound.fromAddress']]" level="INFO" doc:name="Logger"/>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<file:outbound-endpoint path="C:\mule" outputPattern="#[function:datestamp].dat" responseTimeout="10000" disableTransportTransformer="true" doc:name="File">
</file:outbound-endpoint>
</processor-chain>
</otherwise>
</choice>
</flow>
Unfortunately the IMAP connector documentation doesn't list the message properties created when a new message is received. You can find all the created properties by adding:
<logger level="WARN" />
after the imap:inbound-endpoint. This will log all the message meta information, including properties, at WARN level.
You can also find the property names in the MailProperties JavaDoc. For example inbound.fromAddress is the inbound property that contains the sender's email address.
The message body is the text email content, unless it's a multi-part email. In that case, if the first part is text/plain, it will be set as the message payload, otherwise all parts will be available as inbound message attachments.