You need to sign in to do that
Don't have an account?

Need help sending email via apex using a regular email template? Kudos of help!
The following code works as far as attaching a visualforce pdf and emailing it to who I want.
What I need help with is that I would like to build a regular HTML email template and call it from my code.
Anyone that contributes will get kudos.
Here is the code:
public PageReference emailPDF() { List<Contract> ContractToUpdate = new List<Contract>(); for(Contract c : [Select Id, Invoice_Sent__c from Contract where Id =:ParentId]) { c.Invoice_Sent__c = True; ContractToUpdate.add(c); } update ContractToUpdate; ToAddresses = new List <String>(); ToAddresses.add(cc.Finance_Contact_Email__c); Subject = 'Hope this works'; Body = 'This is a test'; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); PageReference pdf = Page.ContractPDFCreation; pdf.getParameters().put('id',parentId); pdf.setRedirect(true); Blob b; b = pdf.getContent(); Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName('Invoice.pdf'); efa.setBody(b); email.setSubject( subject ); email.setToAddresses( ToAddresses ); email.setPlainTextBody( body ); email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); return new PageReference('/'+parentId); }
Thank you,
Steve Laycock
Try adding
setTargetObjectID( CONTACT ID);
//Set it to any contact if you are not using merge fields from contact
then add the
setWhatID(ID of the Contract object);
then use
setTemplateID(IDOFYOURTEMPLATE);
In theory it will work just fine.
The only other way is to use a VF template.