after much research, it doesn't look this can be done. The only real option is to put all the attachments together as one attachment.
it looks like the only way you can add multiple attachments is when you know the number of attachments (or potential #) ahead of time. if you do you then you can create those elements in the email template
<messaging:attachment rendered="(some criteria)" >
<messaging:attachment rendered="(some criteria)" >
I know it's way too late but i thought I would post our solution incase anyone else has the same issue.
Coincidentally we were having the exact same problem with datamail.co.nz/vretrieve haha.
Basically the problem is that you can't use response.getBody() as it will convert the whole response to a string and corrupt the binary attachment, and you can't just use response.getBodyAsBlob() because the text component of the response will also corrupt the file.
There should be a header in the response called 'Content-Type', which also should have a parameter called 'boundary', this is used to separate the different parts of the response.
What we did was pull out the boundary value and convert it to hex, then convert the whole response to hex and use the boundary to split out the attachment, then convert the separated file back to a blob.
For us, there was still some header information in the section containing the file however that didn't seem to affect the file when it was converted back to a pdf so we left it there, others may need to split that out too.
here's our code,
//read the response header to get the boundary string
String header = res.getHeader('Content-Type');
String boundary = header.split('boundary="')[1].split('"')[0];
//convert the boundary string to Hex so we can split the attachment from the rest of the response
String boundaryHex = EncodingUtil.convertToHex(Blob.valueOf('--'+boundary));
//convert the entire response to Hex
String responseHex = EncodingUtil.convertToHex(res.getBodyAsBlob());
//use the boundaryHex to split out the attachment then convert the file back to a blob
Blob pdf = EncodingUtil.convertFromHex( responseHex.split(boundaryHex)[2] );
//return the blob
return pdf;
Best Answer
In Visualforce email template you can use below tag to render content into a PDF attachment in final email.
But if you are trying to render an already existing PDF document from say "Documents" in salesforce, you can try below steps
1) Get the content of the document by querying document. 2) Assign this document body to a property(String type) in the controller of visualforce email template. Then use the property inside above mentioned tags.
Generally if you want more control over this kind of scenario, we go for an apex trigger and from trigger we send apex generated email. In this case you can query documents or any other object or even multiple objects and control attachment contents more precisely.
Please try below code to encode the Document body as string suitable for email attachment.It worked for me while reading text Document into apex and rendering as PDF in email,
It looks like, if we are reading a PDF document into string, it may not render property because of encryption. But there are some work arounds, please check below, https://developer.salesforce.com/forums/ForumsMain?id=906F00000009DKQIA2