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
Rohan SureshRohan Suresh 

using attachment table in visualforce email templates

I have created a visualforce email template. The body of this template contains some text and few merge fields linked to custom object. I am able read the data using merge fields successfully.

But this custom object also allows users to attach files. So I need to send those attached files (if any) with the same email.

I have tried different options but none of them worked for me. Now for time being what i did is that i added a custom component in my mail body and that custom component has a apex:repeat control that gives the link and content type of attachments in a tabular format. Below is my custom component:

<table cellpadding="1" cellspacing="0" width="100%" border="1">

    <tr><td colspan="2"><b>Attachments</b></td></tr>

    <tr><td><b>File Name</b></td>         <td><b>Content Type</b></td></tr>

    <apex:repeat value="{!attachments}" var="attachment">


         <td><apex:outputLink value="{!attachment.Id}">          {!}</apex:outputLink></td>





But the above solution requires the user to be connected to the internet and login to salesforce to access the attachments since the files are not physically attached to mail.

Do we have any way through which we can read attachments from attachment table and send as attachments using visualforce email templates. Also there may be 1 or more than one attachment.




You can use messaging:attachment tag..
Rohan SureshRohan Suresh

Hi, Thanks for your response.. I know we can use messaging:attachment tag but I am reading the attachments dynamically from Attachment table so I dont know what would be the content type and other details.


There could be one or more than one attachment so cant predict how many messaging:attachment tag to add.


Also my VisualForce Email template is dependent on some other custom object. So I cannot put 'Attachment' in 'Related to' attribute of email template.

You can not attach the files from attachment object using VF template...only possibility is to use Messaging class- sendEmail and EmailFileAttachment to attach your files from Attachment object physically and use trigger to invoke it