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
wendy chan 3wendy chan 3 

Upload attachments and send with Email

I`m have build below visualforce page which allows our portal users to send emails. Page works fine, however now I want to add a functionality to allow users to upload the attachment and send with email. I have went through lot of resources online but cant find the solution. Can someone please help?

 
public class sendEmailStdCon {

    private final Case c;

    //Case fields
     public String Caseno { get; set; }
     public String CaseRecType { get; set; }
     public String CaseStatus { get; set; }
     public String CaseSubject { get; set; }
    public string threadID { get; set; }
    public string refNumber   { get; set; }

    public String subject { get; set; }
    public String body { get; set; }
    public String addresses { get; set; }
    public String BodyofEmail { get; set; }
    public String BccAddress { get; set; }
    public String ccAddress { get; set; }
    public String ToAddress { get; set; }


    public   String orgEmails {get;set;}



    datetime currenttime =  System.now();

    public sendEmailStdCon(ApexPages.StandardController controller) {

        Case c = [SELECT id, CaseNumber,Status ,Subject, Email_Case_Ref__c , RecordType.Name from Case where id = :ApexPages.currentPage().getParameters().get('id')];

            Caseno = c.CaseNumber;
         CaseRecType = c.RecordType.Name;
            CaseStatus = c.Status;
            CaseSubject = C.Subject;
           refNumber = c.Email_Case_Ref__c;     

        EmailMessage e = [ SELECT id, FromName,CcAddress, BccAddress,ToAddress , Subject, TextBody, FromAddress from EmailMessage WHERE 
                          ParentId = :ApexPages.currentPage().getParameters().get('id')  ORDER BY MessageDate DESC Limit 1  ];



        BodyofEmail = e.TextBody;
        Subject = e.Subject;
        BccAddress =  Userinfo.getUserEmail();
        ccAddress = e.CcAddress;
        addresses = e.FromAddress; 
        ToAddress = e.ToAddress;
    }







    public Case getCase() {
        return c;
    }

    public PageReference send() {

                  system.debug('orgEmails: ' + orgEmails);


      OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = :orgEmails];


        // Define the email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 



        String[] toAddresses = ToAddress.split(';', 0);

        // Sets the paramaters of the email
        email.setSubject( subject + ' ' + refNumber);
        email.setToAddresses( toAddresses );

        if (ccAddress != null && ccAddress.trim() != '' ) {
        String[] cccAddresses =ccAddress.split(';', 0);
        email.setCcAddresses( cccAddresses );
            }

        email.setPlainTextBody( BodyofEmail );



                if (BccAddress != null && BccAddress.trim() != '' ) {
        String[] BccAddresses =BccAddress.split(';', 0);
        email.setCcAddresses( BccAddresses );
            }


        if ( owea.size() > 0 ) {
        email.setOrgWideEmailAddressId(owea.get(0).Id);
        }



        // Sends the email
        Messaging.SendEmailResult [] r = 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});   


             //   EmailMessage E = New EmailMessage () ;
            //  e.


        return null;
    }
}

VF:
<apex:page standardcontroller="Case" extensions="sendEmailStdCon">


    <apex:messages />
    <apex:pageBlock title="Send an Email to Your Representatives">
        <p>Fill out information below and click on "Send" button send an email.</p>
        <br />
        <apex:pageBlockSection columns="2"> 
            <apex:outputText label="Case Number" value="{!Caseno}"> </apex:outputText>
            <apex:outputText label="Case Record Type" value="{!CaseRecType}"> </apex:outputText>
            <apex:outputText label="Status" value="{!CaseStatus}"> </apex:outputText>
            <apex:outputText label="Subject" value="{!CaseSubject}"> </apex:outputText>

        </apex:pageBlockSection>

        <apex:form >


            <br /><br />

            <apex:outputLabel value="To" for="To"/>:<br />     
            <apex:inputText value="{!ToAddress}" id="to" maxlength="500" style="width: 500px;" />
            <br /><br />

            <apex:outputLabel value="CC" for="CC"/>:<br />     
            <apex:inputText value="{!ccAddress}" id="cc" maxlength="500" style="width: 500px;" />
            <br /><br />

            <apex:outputLabel value="Bcc" for="Bcc"/>:<br />     
            <apex:inputText value="{!Bccaddress}" id="Bcc" maxlength="500" style="width: 500px;"  />
            <br /><br />


            <apex:outputLabel value="Subject" for="Subject"/>:<br />     
            <apex:inputText value="{!subject}" id="Subject" maxlength="500" style="width: 500px;" />
            <br /><br />


            <apex:outputLabel value="Body" for="Body"/>:<br />     
            <apex:inputTextarea value="{!BodyofEmail}" id="Body"  rows="10" cols="80" style="width:550px;height:500px"   />           
            <br /><br /><br />
            <apex:commandButton value="Send Email" action="{!send}" /> 
        </apex:form>
    </apex:pageBlock>
</apex:page>

NagendraNagendra (Salesforce Developers) 
Hi,

Here you need to add tag to accept file

I had used HTML tag for this

in javascript, you can get the file and send to server side

at server side, you will get the file in blob format, the add this file to email attachment like this.
public void send(blob attachment){

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('attachment');
    efa.setBody(attachment);

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); 
    email.setSubject( subject );
    email.setToAddresses( toAddresses );
    email.setPlainTextBody( body );

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

    // Sends the email
    Messaging.SendEmailResult [] r = 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email})

}
Hope this helps.

Regards,
Nagendra