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
SMasterSMaster 

How to send an Email with Attachment??

Hi All,

 

I want to send an email with an Attachment from Notes and Attachment related list.

 

i.e. i have few attachments inside Notes and Attachment related list of a custom Object.. and on a button click i need to send an email with attachment

 

please let me kow how can i do that...

 

 

Thanks

WesNolte__cWesNolte__c

Hey,

 

Have you looked at this document: http://www.salesforce.com/us/developer/docs/pages/Content/pages_email_sending_attachments.htm

 

The process will be similar for you, the trick is to fetch your attachment into a Blob as they do with the PDF.

 

Cheers,

Wes

SMasterSMaster

Hi Wes,

 

Thanks for this.

 

I tried to write a code to achive the same using the Onclick Javascript event.. but i am getting the syntax error: Please help

 

{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}

var temp= new sforce.SObject("opportunity_proposals__c");
temp.Id ="{!opportunity_proposals__c.Id}";
temp.SMEs_Email__c = "{!opportunity_proposals__c.SMEs_Email__c}";
var emailAddresses = temp.SMEs_Email__c;

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emailAddresses);
//mail.setCcAddresses(ccAddresses);
mail.setReplyTo('SMaster@gmail.com');
mail.setSenderDisplayName('Support');
mail.setSubject('Mail has been sent');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Your Email has been sent');
//mail.setTargetObjectId(temp.Id);
mail.setWhatId(temp.Id);

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

 

 

WesNolte__cWesNolte__c

Ah I see.. you'll need to use Apex to do this. You can make your button pass information to "dispatcher" visualforce page and that will send the email for you. This is a bit more complex, see this example of creating a dispatcher: http://jeffdonthemic.wordpress.com/2008/11/14/redirecting-users-to-different-visualforce-pages/

 

The controller for the dispatcher page would include code similar to that which I listed previously. These are tricky concepts on their own but gluing them together isn't too difficult once you understand the concepts. Give it a go and then let me know if you get stuck.

 

Wes

NaishadhNaishadh

Is there any specific reason for using Ajax for sending email with attachment? You can do it easily in trigger using same code. any way what error your are getting?

SMasterSMaster

Hello Sir,

 

You are right..I have sucessfully done that using the following code. But i wanted to know that can i get my following trigger fired on Detail Page Button click??

 

I want the trigger should get fire only when i click the button....

 

trigger sendemailwithattachment on opportunity_proposals__c (after insert,after update)
{


for(opportunity_proposals__c oppr : Trigger.new)
   {
   if(oppr.SMEs_Email__c != null)
     {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{oppr.SMEs_Email__c};
       // String[] toAddresses = 'oppr.SMEs_Email__c';

        mail.setToAddresses(toAddresses);
        mail.setReplyTo('SMaster@gmail.com');
        mail.setSenderDisplayName('CRM Support');
        //mail.setSubject('Mail with Attachment');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        //mail.setPlainTextBody('Your Email has been sent'); 
        mail.setTargetObjectId(oppr.OwnerId);
        mail.setTemplateId('00XQ0000000QULj');
        mail.saveAsActivity = false;


      //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       
       
       
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :oppr.Id])
        {
        // 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 });
       
}       
}
}

NaishadhNaishadh

yes that is also possible.

Move your email notification code in to apex class and call that method from your page button click using action function. 

SMasterSMaster

Hello Sir,

 

I tried even doing that.. i tried creating an apex class using the code.. but was unable to do so...

 

please suggest how can i write an apex class for this code....

NaishadhNaishadh

please send me your class

WesNolte__cWesNolte__c

He's trying to send from a standard page so he won't be able to add an action function to the button component. Some type of dispatcher will be required.

 

Wes

SMasterSMaster

Hi,

 

I have got some workaround of achieving the same.

 

What i have done..

 

on the button click i have updated a checkbox.. and on that updation i have fired my trigger... this way i am able to fire my trigger directly from page layout using detail page button...

 

But, now i wanted to send only one selected files from Notes and Attachment related list.. please let me know how can do that??

TresureTresure

I was able to use it successfully.

 

Thanks for helping.

KevinBrKevinBr
Instead of having a dependency on an exising attachment, you can just use a dummy file to pass into the email message

remove the for-loop, and change the lines:

 efa.setFileName(a.Name);
 efa.setBody(a.Body);

to something like:

efa.setFilename( 'myAttachmentName.txt');
efa.setbody(Blob.valueOf('Unit Test Attachment Body'));
 
guna malliguna malli
Hi
Iam using this code,but that code show Error like this  " Error: Compile Error: line 4:0 no viable alternative at character ' ' at line 4 column 0"

please replay me

trigger sendemailwithattachment on opportunity_proposals__c (after insert,after update)
{
for(opportunity_proposals__c oppr : Trigger.new)
   {
   if(oppr.SMEs_Email__c != null)
     {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{oppr.SMEs_Email__c};
       // String[] toAddresses = 'oppr.SMEs_Email__c';
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('SMaster@gmail.com');
        mail.setSenderDisplayName('CRM Support');
        //mail.setSubject('Mail with Attachment');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        //mail.setPlainTextBody('Your Email has been sent'); 
        mail.setTargetObjectId(oppr.OwnerId);
        mail.setTemplateId('00XQ0000000QULj');
        mail.saveAsActivity = false;
      //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       
       
       
      //Set email file attachments
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :oppr.Id])
        {
        // 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 });
       
}       
}