Search code examples
http-postemail-attachmentsmulesoftmule4apikit

Mulesoft - I want to send an e-mail with a file attached that was posted to my API endpoint


Using Mule 4.x, I would like to have my API set up with an HTTP POST method endpoint, that allows a user to post form data containing a file.

Then I would like to take that file, and e-mail it with the posted file as an attachment on the e-mail.

This should be file-type agnostic if possible, as it could be any filetype that is posted to the API.

I can't seem to transform the form part's "content" into something that is suitable for an e-mail attachment.

I've gone through many different iterations. What I've got at this very moment.. When I log the payload, I get:

 ----------------------------344843990838395406613242 
Content-Disposition: form-data; name="fileContents"; filename="upload.txt" 
Content-Type: file

[{"message": "Hello World!"}]
----------------------------344843990838395406613242--

Which looks good. I can access the filename and log it by using: #[payload.parts[0].headers.'Content-Disposition'.filename]

But whenever I try to access: #[payload.parts[0].content]

Whether it is to attempt to 'log' it, or in the Attachment field of the Send Email component, I get the following Error message:

""Unable to find a sub type in `file, while writing Weave.
Trace: at main (Unknown)" evaluating expression: "payload.parts[0].content"."

Separately, I was able to attach a local file to an e-mail, by using a 'Read' component, and then a 'Set Variable' to assign it to a variable, with the MIME type of "application/binhex". Then I appeared to be able to attach any file type to the e-mail just by changing the local file that was being pointed to.

But I'm not sure how to make the connection from the incoming form-part file, to attaching it.

I feel like there must be a step I'm missing. Any tips/tricks/advice/examples would be greatly appreciated. Do I need to 'read' the content stream and transform it into something?

Note: Also, there is no specific reason that I am trying to use a POST with the form-data body containing the file. That just seemed to make the most sense to me, but if there is a better way to send it that would work better, by all means, let me know.

Edit/Additional Info: In the video here https://www.youtube.com/watch?v=5_YKXE8E7Sw I noticed that his payload outputs the correct Content-Type, not just "file" like mine does. The only difference that I noticed is that I am using APIKit as the entry point into my API. He was not.

I created a test project just using a standard http listener, and the output does indeed contain the actual Content-Type of the file.

Maybe this is a bug in APIKit? Or do I need to configure something specifically to allow the actual file input type to come through correctly?


Solution

  • I've found a solution with the help of someone from the Mulesoft forums.

    Rather than posting my file as a form part data, I am Base64 encoding the contents of my file and sending it as a JSON formatted body of the http post. Then using this json payload to transform into an e-mail attachment to add to the E-mail Connector.

    This seems to be working perfectly. And seems to be file-type agnostic.