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
Matt Prorok 7Matt Prorok 7 

Can VF email templates include Salesforce files as attachments?

I've looked through the forums and documentation and am not clear on what the answer is.
I am using Salesforce Files and we have a use case where we would like to email out the Salesforce File as an attachment to an email by using a VF email template. The email recipients would not have access to Salesforce so we would need to provide the physical file and not a link. The basic idea is that the VF email template body would contain information from the Salesforce record itself (in our case Work Order) and then the Salesforce File would be added as an attachment.

Does anyone know if this is possible or has done it before? Thanks so much for your help!

Matt
David Zhu 🔥David Zhu 🔥
You can attach a standard file to VF email template, it is at the bottom of the email template settings.
Matt Prorok 7Matt Prorok 7
@David sorry I should have clarified. The file would be different per each email. So the idea is that this is not a standard file, but a file which has been manually uploaded to be associated with a record, for example.
David Zhu 🔥David Zhu 🔥
Then you have to use Apex code to handle this.
Dushyant SonwarDushyant Sonwar
You need to create your email template using apex.
 
Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(myContact.Email);
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('sirdavid@bankofnigeria.com');
      mail.setSenderDisplayName('Official Bank of Nigeria');
    
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('business@bankofnigeria.com');
      mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('URGENT BUSINESS PROPOSAL');
      String body = 'Dear ' + myContact.FirstName + ', ';
      body += 'I confess this will come as a surprise to you.';
      body += 'I am John Alliston CEO of the Bank of Nigeria.';
      body += 'I write to request your cooperation in this ';
      body += 'urgent matter as I need a foreign partner ';
      body += 'in the assistance of transferring $47,110,000 ';
      body += 'to a US bank account. Please respond with ';
      body += 'your bank account # so I may deposit these funds.';
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
      Messaging.sendEmail(mails);

If you want to add file then check below link how to get the Salesforce binary blob data.
https://developer.salesforce.com/forums/?id=9060G0000005VtDQAU

attach the blob data and shoot the emails
WOCFBody = ;//blob query the File content and store
Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
			  efa.setFileName('Test.PDF');
			  efa.setBody(WOCFbody); 
			  fileAttachments.add(efa);
mails.setFileAttachments(fileAttachments);
mails.add(mail); 
Messaging.sendEmail(mails);

 

Hope this helps!
Dushyant SonwarDushyant Sonwar
example of sending email using apex
https://www.sfdc99.com/2014/03/01/sending-emails-using-apex/