You need to sign in to do that
Don't have an account?
amit 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
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
getattachment=[select id,name,body from Attachment where parentid=:l.id];
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.