+ Start a Discussion
benwrigleybenwrigley 

Attaching file to email

Hi There,

 

I would like to attach and attachment to an email. Looking through the documentation I can see the methods for setDocumentAttachments and setFileAttachments  but neither of these appear to allow attaching an 'Attachment' object.

 

I have an opportunity with a file attached in 'Notes & Attachments' but not sure how to add this to an email.

 

TIA

 

 

Pradeep_NavatarPradeep_Navatar

Try out the sample code to get a file attachment attached with email :

 

            account = [select Name, (SELECT Contact.Name, Contact.Email FROM Account.Contacts)

            from Account where id = :ApexPages.currentPage().getParameters().get('id')];

            // Reference the attachment page, pass in the account ID

                                                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

                                                PageReference pdf = Page.attachmentPDF;

                                                pdf.getParameters().put('id',(String)account.id);

                                                pdf.setRedirect(true);

 

                                                // Take the PDF content

                                                Blob b = pdf.getContent();

 

                                                // Create the email attachment

                                                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

                                                efa.setFileName('attachment.pdf');

                                                efa.setBody(b);

                                                email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

 

Hope this helps.

benwrigleybenwrigley

Hi Pradeep,

 

I don't think this will quite achieve what I'm after unless I have misunderstood. This looks like attaching a file from an upload, is that right?

 

What I need to be able to do is attach a file that is already in SFDC, but it hasn't been uploaded as a 'Document' it has been uploaded as an 'Attachment' to an Opportunity. 

 

Is this possible to do?

 

TIA

 

 

SMasterSMaster

Hi,

 

I am try to create a class  for send emails with attachment....

 

But i am getting the error: Compile Error: Variable does not exist: Name.. why so....

 

 

public class testout{
 
 public opportunity_proposals__c q { get;set; }
 
    
 public Id oppr     {    get;set;    }      
 public testout(ApexPages.StandardController ctrl)   
 {      
 oppr = ctrl.getRecord().Id;
 }
 
 public Pagereference emailAtt()
 {
 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'SMaster@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setTargetObjectId('005Q0000000FR7f');
        mail.setTemplateId('00XQ0000000QULj');
        mail.saveAsActivity = false;
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Id, Name, Body, BodyLength from Attachment where ParentId = :oppr])
        {
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
        efa.setFileName(a.Name);
        efa.setBody(a.Body);
        fileAttachments.add(efa);
        }
        mail.setFileAttachments(fileAttachments);

        return null;
}
}