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

Visual force email template and attachment file.

Hello, I am looking to have attachments (i.e. actual physical file and not the link) in vf email template. How do i accomplish this? Appreciate if you have any sample code. Thanks


I am not clear with the requirement. Do you want attachment contents should be shown in email itself?


Prasanna__d wrote:

I am not clear with the requirement. Do you want attachment contents should be shown in email itself?


Hi ,

I am having the same requirement in my project. we need to attach the file present in Notes&Attachment related list in the visualforce email template as an (real)attachment(not as a link to the attachment).So that, users can directly download/view the file from email attachment , they don;t need to login to salesforce to view/download it. Thanks in advance.

Any help on this is much appreciated. 


I am also looking for a solution to create a VF E-Mail template which would have the Link to the Attachment which is associated with the Opportunity Record.


Can some one help in please


Hi All,

I have got this to work myself and used in my project. Here is the sample code.


************* VF Component *******************(Component Name say : testemail)


<apex:component controller="emailattachment" access="global">

<apex:attribute name="emailatt" description="Attachment URL" access="global" type="String" assignTo="{!Oppid}"/>

<apex:repeat value="{!attach}" var="att">
<a href="{!att}">{!attach[att]} </a> <br/>





******************** Component Controller Class *********************************


public class emailattachment {


public string Oppid;
public string attid;
public Map<String,String> urlMap=new Map<String,String>();
public List<Attachment> attach;

public string getOppid() {
return null;

public void setOppid(String s) {

public Map<String,String> getattach() {
attach=[Select id,name From Attachment Where Parentid=:Oppid];

for(Attachment att1:attach) {
String urls=URL.getSalesforceBaseURL().toExternalForm()+'/servlet/servlet.FileDownload?file=' ;
return urlMap;

public void setattach(List<Attachment> ata) {





****************VF Email Template*********************************

<messaging:emailTemplate subject="Email Subject" recipientType="User" relatedToType="Opportunity">


<messaging:htmlEmailBody >

// Body of your email

// Below is the link to the attachments associated with the opportunity record


<c:testemail emailatt="{!}" rendered="true">









First create the VF component and custom controller for it. and then include the component in the vf email to get the link to the associated attachments. Note that, with this approach users will be required to login to salesforce to access the files.


Please mark it as a Solution if it answers your question.




Your cod work properly but required login to salesforce for show the attachment.
Any suggest for the way not required to login to salesforce ?
Is it possibile show a link of attachment to anyone in the world without access of salesforce ?
The below code will render attachment information without any customization. You will be required to login though.

<apex:repeat var="cx" value="{!relatedTo.Attachments}">
          <td style="padding: 5px;">Attachment:</td>
          <td style="padding: 5px;"><a href="{!}">{!cx.Name}</a></td>