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
amit wagaskaramit wagaskar 

Not able to send an email with attachment

Hi all,
I'm trying to send an email which contains some merge field as well as attachment.but the thing is that when i'm sending an email by using apex trigger it will only sending the email with merge field and not the attachment.also when im doing the same by giving static value for the Setname and setbody method at that time i'm able to send email with attachment.wants to know what i'm missing here.

here is the code which i'm using to achieve functionality  mention above. 
//my trigger
trigger LeadCreate on Lead (after insert) {
    
    if(trigger.isafter)
    {
        if(trigger.isinsert)
        {
            sendMailOnLead.sendmailtoHR(trigger.new);
        }
        
    }

}
//helper class for the trigger
public class SendMailOnLead {
    
    
    public static list<lead> sendmailtoHR(list<lead>leads)
    {
        EmailTemplate et=[Select id from EmailTemplate where name=:'LeadDetails'];    
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        
        for(Lead l:leads)
        {
             Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
             String[] sendingTo = new String[]{'wagaskar.amit@gmail.com'};
             
             singleMail.setTargetObjectId(l.Id);
             singleMail.setTemplateId(et.Id);
             singleMail.setToAddresses(sendingTo);
            
            list<attachment>getattachment=new list<Attachment>();
             getattachment=[select id,name,body from Attachment where parentid=:l.id];
            for (Attachment a: getattachment)
            {
              Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename(a.Name);
              efa.setbody(a.Body);
              fileAttachments.add(efa);
            }
            //by using below code i'm able to send an email with attachment
            /*
             Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
              efa.setFilename( 'myAttachmentName.txt');
              efa.setbody(Blob.valueOf('Unit Test Attachment Body'));
              fileAttachments.add(efa);

            */
            singleMail.setFileAttachments(fileAttachments);
            system.debug('Attachment Added successfully');
            emails.add(singleMail);
            
            
        }
        Messaging.sendEmail(emails);
        system.debug('class run successfully');
        return leads;
    }

}
thanks,
Amit
 
@Amit Kumar Giri@Amit Kumar Giri
Are u getting any record for "getattachment" ? Check if u are getting anything for this .
getattachment=[select id,name,body from Attachment where parentid=:l.id];
amit wagaskaramit wagaskar
Hi Amit,
thank you for your quick response.
now i'm able to send an email with attachment but facing another problem after this.which is it is sending an email to 'wagaskar.amit@gmail.com' which is ok for me but it will also sending  email on email id which is on lead record.so I dont want to send an email  on lead email id.I only want email to be sent on  'wagaskar.amit@gmail.com' email id.
please let me know that  what i need to do here to achieve above mention functionality.