+ Start a Discussion

email attachment

hi ,


I am working in a solution where i run a batch class which gives me the all the ids of an object based on some criteria. In this i need to collect all these ids and put it into a csv file and send an email with the csv file attached to the email.


Can anybody suggest me the code to accomplish this.

Rasmus MenckeRasmus Mencke

Use visualforce email templates to do that.


Take a look at this package as an example:



Here is another example:



hi rasmus thanks for the reply but in my case i want it the file to be a CSV file and send as an attahcment with the email.

Rasmus MenckeRasmus Mencke

You can do that with Visualforce email templates.


Add a component like this into the template, this example is a PDF, but easy to change into a csv



<messaging:attachment renderas="pdf" filename="Opportunity_Products.pdf">

<img src="http://www.salesforce.com/common/assets/css/images/hdr_apexdn.gif" border="0"></img>

        <p>Dear {!recipient.name},</p>
        <p>Below is a list of products related to the Opportunity:<b> {!relatedTo.name}</b>.</p>
        <br/>Account: <i> {!relatedTo.Account.name}  </i>  
        <br/>Opportunity Owner: {!relatedTo.owner.name}
        <br/>Opportunity Amount: {!ROUND(relatedTo.Amount,0)} 
        <br/>Opportunity Close Date: {!relatedTo.CloseDate}  

    <apex:datatable border="2" cellspacing="5" var="opp2" value="{!relatedTo.OpportunityLineItems}">
        <apex:facet name="header">Products associated with Opportunity {!relatedTo.name}</apex:facet>
          <apex:column value="{!opp2.PriceBookEntry.name}" headerValue="Product Name"/>
        <apex:column value="{!opp2.Quantity}" headerValue="Quantity" />
        <apex:column value="{!opp2.UnitPrice}" headerValue="Units" />
        <apex:column value="{!opp2.TotalPrice}" headerValue="Total Price" />






jaya sai prasadjaya sai prasad

If you want to send an Email with Attachment a native salesforce app MassMailer DOCS is perfect for you.

MassMailer DOCS let’s you send mass email attachments to your leads or contacts while securely storing your files with Rackspace Cloud Files.

You can try this app by installing from appexchange  -Massmailer Docs

You can learn more details about the product on this website - docs.massmailer.io