function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AyushKhandelwal9AyushKhandelwal9 

Dynamically Attach the file with VF email Template

I want to send the email to a user of my org whenever a case record is marked as closed. I want to add the recent file as attachment with the case. I have a VF email template which I want to use to send the emails.

We have an option to do this via apex(SingleEmailMessage) but I do not want to add user email as toAddress(). It might be counted as the daily email limit. 


I have created a VF component to add the attachment but the attachment is not coming in readable format. VF is not rendering EncodingUtil.base64Encode(cv.VersionData) in readable format in attachment.
public List < String > getFile() {
    List < ContentDocumentLink > cdl;
    List < String > fileData = new List < String > ();
    ContentVersion cv;
    String fileName = fileNameLabel + '%';
    if (caseRecordId != null) {
        cdl = [Select Id, ContentDocumentId, LinkedentityId, ContentDocument.Title FROM contentDocumentLink where LinkedEntityId =: caseRecordId and ContentDocument.Title like: fileName limit 1];
    }
    if (cdl != null && !cdl.isEmpty() && cdl[0].ContentDocumentId != null) {
        cv = [Select Id, Title, VersionData, FileType from ContentVersion where ContentDocumentId =: cdl[0].ContentDocumentId];
    }
    fileData.add(cv.Title);
    fileData.add(cv.FileType);
    fileData.add(EncodingUtil.base64Encode(cv.VersionData));
    return fileData;
}


 
<apex:component controller="AttachFileController" access="global">
  <apex:attribute name="caseRecId" type="Id" description="Id of the case" assignTo="{!caseRecordId}" />
  <apex:attribute name="fileName" type="String" description="File Name" assignTo="{!fileNameLabel}" />
  <messaging:attachment rendered="{!File.size > 0}" renderAs="{!If(File.size > 0, File[1], 'pdf')}" filename="{!If(File.size > 0, File[0], 'Attachment')}"> {!File[2]} </messaging:attachment>
</apex:component>


I have tried to use toString() method as well but it only works if the file contains text else it will throw an error (blob is not a valid utf-8 string).

Please help me with this issue
SubratSubrat (Salesforce Developers) 
Hello ,

Please try with below :
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTargetObjectId(caseOwnerId); // Set the recipient's user ID or another valid ID
email.setSaveAsActivity(false); // Prevent the email from being saved as an activity

email.setTemplateId(emailTemplateId); // Set the ID of your VF email template

Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setBody(fileContent); // Set the binary content of the file
attachment.setFileName(fileName); // Set the desired name of the attachment
email.setFileAttachments(new List<Messaging.EmailFileAttachment>{ attachment });

Messaging.sendEmail(new List<Messaging.Email> { email });
Make sure to replace caseOwnerId with the appropriate user ID, emailTemplateId with the ID of your VF email template, fileContent with the binary content of the file, and fileName with the desired name of the attachment.

By using this approach, you can send an email with an attachment without directly specifying the recipient's email address in toAddress().


Hope this helps !
Thank you.