Home SalesforceApex Send Email Template As PDF Attachment using Salesforce Apex

Send Email Template As PDF Attachment using Salesforce Apex

by Dhanik Lal Sahni
Send Email Template As PDF Attachment using Salesforce Apex

Sending Email Template as PDF attachement is one of the basic requirement in Salesforce application. This blog will give step by step information how to send Email Template as PDF attachement and as a email body both.

Steps for sending Email Template as attachment

  1. Create Email Template for Attachement
  2. Create Email Template for Email Body
  3. Create Apex Utility Class for sending email
  4. Test Functionality

1. Create Email Template for attachment

Create a email template using html. You can add styles using html and css in email template.

For this blog, I have tried to create below email. You can try based on your requirement.

Email Template Name : Profile

Send Email Template As PDF Attachment using Salesforce Apex

2. Create Email Template for cover letter or email body

Create a email template for email body of email which need to be sent to customer/user.

We can use any place holder to make dynamic population of values at runtime based on record.

I have added only one place holder {name} which is replaced in body of email before sending to user.

Email Template Name : Mail Cover Letter

Send Email Template As PDF Attachment using Salesforce Apex

3. Create Apex Utility Class for sending email

Let us create a apex class which will send email to customer/user. For this we can use below classes.

Messaging.EmailFileAttachment :

This will be used to create a PDF using email template’s html content. Below code is used to convert html into PDF.

attach.setContentType('application/pdf');
attach.Body = Blob.toPDF(body);

Messaging.SingleEmailMessage:

This class will be used to create email message. I have used HTML value of email template in code, but you can also directly use email template id in code using setTemplateId method.

Complete Apex Code

4. Test Functionality

Test above functionality by executing below code.

PDFGenerationService.generatePDF();

You can call this code from lightning component or batch apex.

Demo Page

Send Email Template As PDF

References:

Blob Class

Messaging Class

Other Related Post

Send Email Using Email Template and Apex

You may also like

6 comments

Schedule Email alerts based on Business Hours in Salesforce September 2, 2021 - 12:13 pm

[…] Send Email Template As PDF Attachment using Salesforce Apex […]

Reply
Kerrianne Hebinger October 25, 2022 - 5:56 am

Hi Dhanik – as a non coder, I have been able to implement this code and it works perfectly.
I have run into a difficulty in that the email template(s) being used for both cover and pdf have merge fields in them. Research I’ve done indicates I need to add settargetobjectID and setwhatid but I just don’t seem to be able to place them correctly in relation to the code you’ve provided. Would you be able to assist please?

Reply
Dhanik Lal Sahni October 30, 2022 - 9:06 am

Hello Kerrianne,

Let us connect on Linkedin. I will help you with your issue.

Thank You,
Dhanik

Reply
Satabdi Acharya March 17, 2023 - 4:18 pm

Hi Dhanik, I got this article as very helpful for me while debug a issue. I also have requirement of merge fields in the pdf. Can you pls help me on this?

Reply
Dhanik Lal Sahni March 19, 2023 - 8:59 pm

Hello Satabadi,

Please explain your requirement with an example. We can also connect and discuss on this.

Thank You,
Dhanik

Reply
Satabdi Acharya March 23, 2023 - 1:26 pm

Hi Dhanik,

Let me know how we can connect

Reply

Leave a Comment