You need to sign in to do that
Don't have an account?
Apex EmailFileAttachment 3MB limit - Any way to provide links to attachments in email?
Hello.
I have just come across the somewhat ridiculous convert-3mb-attachments-to-HTML limitation in Salesforce, which has effectively crippled a new app that I just developed.
The app is an expense app that permits users to enter expense details into a custom object, attach an image of a receipt (either via the desktop or mobile app), and then submit an expense report electronically that bundles all of the expense details into an email message with all of the receipts attached to the email. With this limitation, the attachments are all being sent as HTML files with links to the attachments, which appear to be hosted on some Salesforce instance that doesn't require a valid login to view.
I don't love the linking aspect, but I could live with it if there was a way to have the body of the email message contain links to the attachments, instead of each attachment being "attached" as an HTML file with an embedded link.
Here's a code snippet, just containing the code to add the attachments to an EmailFileAttachment. Everything above and below this code is functional and irrelevant to this issue.
Is there some way to include a list of the attachment links in the body of the message instead of actually attaching them as HTML files?
Thanks in advance.
-Greg
I have just come across the somewhat ridiculous convert-3mb-attachments-to-HTML limitation in Salesforce, which has effectively crippled a new app that I just developed.
The app is an expense app that permits users to enter expense details into a custom object, attach an image of a receipt (either via the desktop or mobile app), and then submit an expense report electronically that bundles all of the expense details into an email message with all of the receipts attached to the email. With this limitation, the attachments are all being sent as HTML files with links to the attachments, which appear to be hosted on some Salesforce instance that doesn't require a valid login to view.
I don't love the linking aspect, but I could live with it if there was a way to have the body of the email message contain links to the attachments, instead of each attachment being "attached" as an HTML file with an embedded link.
Here's a code snippet, just containing the code to add the attachments to an EmailFileAttachment. Everything above and below this code is functional and irrelevant to this issue.
List<ContentVersion> attachmentData = new List<ContentVersion>(); attachmentData = [SELECT ContentDocumentId,VersionData,FileExtension,FileType,Id,Title,PathOnClient FROM ContentVersion where Id IN :ContentVersionDetailsMap.keySet()]; for (ContentVersion a :attachmentData) { Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setContentType(extContentTypeMap.get(a.FileExtension)); // Content type of attachment efa.setFileName(a.Title + '.' + a.FileExtension); //Title of the PDF efa.setFileName(a.PathOnClient); //Title of the PDF efa.setBody(a.VersionData); //Body of the PDF,need to do transfer into blob fileAttachments.add(efa); } mail.setFileAttachments(fileAttachments);
Is there some way to include a list of the attachment links in the body of the message instead of actually attaching them as HTML files?
Thanks in advance.
-Greg
As a possible workaround to the attachment size issue, I considered sending each attachment in its own email message. It's not ideal, but at least it would save the recipients some clicks. I did some testing and rewrote the email sending portion of the code, and that worked... except I've hit upon another governor limit in that only 10 email invocations is permitted.
Still open to suggestions.
-Greg
This kind of governor limit (10 mails max.) is for an unique transaction (synchronous) but you can use asynchronous treatments (batches and queues) to exceed easily this limit.
1) an anonymous code for a test with 15 mails sent with a unique caller but using 15 enqueued jobs:
The idea is to pass all the needed parameters for each job in the constructor of my_async : AsyncSendMails(recipient,subject,map1,map2);
Then you just trigger the mailings with the System.enqueueJob(my_async)
There is also a governor limit for the number of jobs in a queue but this limit is sufficient and you can also use FlexQueue (100 jobs).
2) The class AsyncSendMails must implement the interface Queueable and the method execute(QueueableContext context)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm
That is not exactly the code that you will use but that is the principle.
The only problem with the asynchronous treatments is the monitoring of the results (job well executed finally or not?).
That is also possible but that is not writen in the code above.
But you will not use the code anyway without other changes.
You can use salesforce file here. What would be the ideal solution you can twick you logic to take the attachment and save it in salesforce file. And you can create a external sharing link and include that in your HTML template. So the receipent receives the template with the link. When he clicks on the link the file gets downloaded. Preety much work like When we put any file in Google drive and send the link to others to download.
The attachment still resides in salesforce and allows users to download the file using an external link. I believe you wouldn't also face Email attachment governer limit.
Let me know if this works.
Regards,
Avishek Nanda
Alain, I like the approach in your second suggestion, where you add attachments up to the max of 3MB, then send the message and create a new message for additional attachments (at least I think that's what you are suggesting). I think the only thing missing would be a test to determine if a single attachment is over 3MB, in which case it would need to send the attachment as an HTML link. Make sense?
-Greg
The batches and queues are quite heavy solutions and the sending of just the links of the files (already stored in files perhaps in your org) as Avishek Nanda suggests could be a more suitable way and much more lighter but you need the code example. I will try to code an example out of curiosity or Avishek Nanda has perhaps already an example.
I agree. But due to the temporary nature of the attachments, creating files and providing links to documents that only need to exist temporarily seems equally heavy. The password-free temporary CDN that Salesforce seems to employ when automatically converting the attachments to HTML links would be a perfect fit... and that goes back to my original question. Is there some way to obtain a list of those temporary links instead of them being attached as individual HTML files?
-Greg
You have already solved half of the problem and for the extra mails exceeding ten just try a solution with queues.
Alain