Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 2
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 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
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • 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
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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.