+ Start a Discussion
Chelsea LukowskiChelsea Lukowski 

How to send an email for Opportunities with attachment form notes and attachments?

I have created a Visualforce page and Apex Class to be able to send an email from an Opportunity with Attachments that are in the Notes & Attachments section. The page allows them to pick an attachment to send with the email. The email will send but does not include the attachment. I know I am missing something, any help would be appriciated.

Apex Class
public class email_class{

    public email_class(ApexPages.StandardController controller) {

    }
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
       
    Public PageReference send(){
    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
            mail.setToAddresses(new String[] {emailTo}); 
            mail.setCcAddresses(new String[] {emailCC});   //set the cc address
            mail.setSubject(subject);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(email_body);
            mail.setWhatId(opportunityId);// Set email file attachments 
            
       List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

            for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
                efa.setFileName(a.Name); 
                efa.setBody(a.Body); 
                fileAttachments.add(efa);}
                mail.setFileAttachments(fileAttachments);// Send email
                
                
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
        }
}



VF page
<apex:page standardController="Opportunity" extensions="email_class">
    <apex:form >
           
        <apex:pageBlock title="Email Details">
            <b>To: </b> <apex:inputText value="{!emailTo}"/><p/>
            <b>CC: </b> <apex:inputText value="{!emailCC}" /><p/>  
            <b>Enter Subject: </b> <apex:inputText value="{!subject}" maxlength="200"/><p/>            
            <b>Enter Body: </b> <apex:inputTextArea value="{!email_body}" rows="10" cols="100"/><p/>
          
        <apex:pageBlock title="Attachments">
        <apex:pageBlockTable value="{!opportunity.Attachments}" var="Att">
            <apex:column headerValue="Select">
            <apex:inputCheckbox value="{!opportunity.Send_Email_with_Quote__c}"/>
            </apex:column>
       <apex:column value="{!Att.createddate}"/>
       <apex:column value="{!Att.name}"/>
       <apex:column value="{!Att.description}"/>
       </apex:pageBlockTable><p/>
       </apex:pageblock>
                
       <apex:commandButton value="Send Email" action="{!send}"/>
       <apex:commandButton value="Canel" action="{!cancel}"/>
            
       </apex:pageBlock>
                           
    </apex:form>    
</apex:page>

 
Best Answer chosen by Chelsea Lukowski
GauravTrivediGauravTrivedi
Hi Chelsea,

Try this code it will work for you.
 
public class email_class{
	
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
	
	public email_class(ApexPages.StandardController controller) {
		opportunityId = ApexPages.currentPage().getParameters().get('id');
    }
	
    Public PageReference send(){

		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
		mail.setToAddresses(new String[] {emailTo}); 
		mail.setCcAddresses(new String[] {emailCC});   //set the cc address
		mail.setSubject(subject);
		mail.setBccSender(false);
		mail.setUseSignature(false);
		mail.setPlainTextBody(email_body);
		mail.setWhatId(opportunityId);// Set email file attachments 

		List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

		for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
			Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
			efa.setFileName(a.Name); 
			efa.setBody(a.Body); 
			fileAttachments.add(efa);
		}
		mail.setFileAttachments(fileAttachments);// Send email
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		return null;
	}
}

If your problem is resolved please mark this as best answer. Thanks! :)

All Answers

GauravTrivediGauravTrivedi
Hi Chelsea,

Try this code it will work for you.
 
public class email_class{
	
    Public string ToAddresses {get;set;}
    Public string CCAddresses {get;set;}
    Public string opportunityId {get;set;}
    Public string subject {get;set;}
    public string email_body {get;set;}
    public string emailTo {get;set;}
    public string emailCC {get;set;}
	
	public email_class(ApexPages.StandardController controller) {
		opportunityId = ApexPages.currentPage().getParameters().get('id');
    }
	
    Public PageReference send(){

		Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // set the to address
		mail.setToAddresses(new String[] {emailTo}); 
		mail.setCcAddresses(new String[] {emailCC});   //set the cc address
		mail.setSubject(subject);
		mail.setBccSender(false);
		mail.setUseSignature(false);
		mail.setPlainTextBody(email_body);
		mail.setWhatId(opportunityId);// Set email file attachments 

		List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();

		for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :opportunityId]){  // Add to attachment file list  
			Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();  
			efa.setFileName(a.Name); 
			efa.setBody(a.Body); 
			fileAttachments.add(efa);
		}
		mail.setFileAttachments(fileAttachments);// Send email
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
		return null;
	}
}

If your problem is resolved please mark this as best answer. Thanks! :)
This was selected as the best answer
Chelsea LukowskiChelsea Lukowski
That worked! Thank you!
Mike Young 12Mike Young 12
Hello Chelsea,
Needed the same functionality as subject of your question and post. So, I borrowed some of your and Gaurav's code and almost have this working on a custom object.  Thank you.  However, the email sends all of the attachments instead of just those checked (choosen) on the VF page.  Can you offer some thoughts as why that would be?  I may be missing the relationship between your Opportunity.send_email_with_quote__c field and how that check filters the list of attachments to only include those that are checked.  Or, maybe I am missing something added later to your code or this would only work on a standard object (althought I do not see why that would be).  Any help would be much appreciated.  Thanks.   
Ashley UnderwoodAshley Underwood
Hello,

I'm looking for a little help on a variation of the above Class and VF.

I'm using the same code but have substituted cases instead of opps.

my issues is that I've linked the case to a custom object (similar to accounts) and on this record there are a few email fields that will auto populate corresponding email fields on the case record with a process.

I'd like to have those emails from the case record prepopulate the To and CC fields.
Ashley UnderwoodAshley Underwood
I was able to resolve my issue by assigning parameters to each email (emailTo & emailCC) within the command button.

<apex:commandButton value="Send Email" action="{!send}" oncomplete="CloseAndRefresh();">
           <apex:param assignTo="{!emailTo}" value="{!case.MyEmailValueForTo__c}"/>
           <apex:param assignTo="{!emailCC}" value="{!case.MyEmailValueForCC__c}"/>
           </apex:commandButton>
       <apex:commandButton value="Cancel" action="{!cancel}"/>
Amarjeet HytechAmarjeet Hytech
what is Send_Email_with_Quote__c