Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Send Email Template As PDF Attachment using Salesforce Apex

    Send Email Template As PDF Attachment using Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniJuly 4, 2021Updated:June 11, 20236 Comments2 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Send Email Template As PDF Attachment using Salesforce Apex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

    apex email template lightning pdf generation
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleTop 5 Session Security for LWC
    Next Article Generic Apex class for Calling External System
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    View 6 Comments

    6 Comments

    1. Pingback: Schedule Email alerts based on Business Hours in Salesforce

    2. Kerrianne Hebinger on 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 on October 30, 2022 9:06 am

        Hello Kerrianne,

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

        Thank You,
        Dhanik

        Reply
    3. Satabdi Acharya on 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 on 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
    4. Satabdi Acharya on March 23, 2023 1:26 pm

      Hi Dhanik,

      Let me know how we can connect

      Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.