First Impressions in your eCommerce store Matter! Make your online store alluring and likable from the first sight with our Magento Development Services
This blog provides the complete code to send a custom email from any event, configuring the email templates from the backend and attaching pdf files in email.First, create a module. Refer the blog – How to create a module in Magento 2?We have created a module with the Dckap as Vendor name and CustomModule as Module name.Here we have assumed a B2B scenario in which the customer applies for credit through an application form on the website, saves and uploads the file. On successful upload, an email with a PDF attachment is fired to the accounting team informing about the application for credit from a customer.
Contents
8 Steps To Attach a PDF File To Emails In Magento 2
Put simply, if you are planning to send an email along with a PDF file you have to follow the below steps.
Step 1:
To begin with, create an email_templates.xml file in app/code/Dckap/CustomModule/etc folder with the following content.template id is the email template identifier. The template identifier will be provided based on the section_id_group_id_field_id which is declared in the system.xml.email_report have is a file name in HTML formatsection_id_group_id_field_id will be explained in the following sections.
Step 2:
Create a template file in the path app/code/Dckap/CustomModule/view/frontend/email/email_report.html and the content for email can be added by declaring the template variables from the action file. For instance here the Customer details have been set as a content for email.The name and email values have been set on templateParams array which has been configured in the action file. We can translate the string “Name:” and “Email:” embedded in a
tag just by opening the two curly braces {{ followed by trans, the string that should be translated in double or single quotes and two closing curly brackets }}.
Step 3:
The created email template can be configured from the admin by creating the system.xml file. Add a file system.xml with the following code in the path app/code/Dckap/CustomModule/etc/adminhtml/. This will hold a default value for email template which will be defined in config.xmlIn this blog, we are going to use the ‘Sales’ section in the back end where the email template for the credit email can be configured. The section id is going to be ‘sales_email’, under which a separate group with group id as ‘dckap_accounting’ will be created and labeled it as ‘Dckap Accounting’.It’s time to create a field under the group with the field id as ‘credit_email_template’. Also, there is one more field from which the receiver email (Accounting email ) could be configured with the field id as copy_to_accounting. The email id can be validated while saving the configuration, provided that required-entry validate-email is given.The field created will be given with the source model ‘MagentoConfigModelConfigSourceEmailTemplate’ with the all available email templates. The source model is a model of data that we can use for configuration.
Step 4:
Create a config.xml file in the path app/code/Dckap/CustomModule/etc/ for setting the default template value (i.e., email_report.html as default) in the admin custom field that has been created using system.xml.The default value for the field created through the system.xml can be given by config.xml where the structure follows as above.
Step 5:
Once the code for the email template and the configuration file has been added it’s time to formulate the email send function in the helper class as follows in path app/code/Dckap/CustomModule/Helper/Emailreport.php.
The following code will be able to send the pdf attachments in the email if the path of the PDF file path is defined.The function sendEmailReport is responsible for sending the email.
This function can be used from any of the actions for sending the email with the pdf file or without the pdf file.
Step 6:
If your email has the requisite for attachment of pdf file then the following file has to be built in…app/code/Dckap/CustomModule/Model/Mail/TransportBuilder.php.
Magento uses MagentoFrameworkMailTemplateTransportBuilder to send email in which adding attachment has not been supported yet. Hence we are overriding the class in our custom module.Step 7:Add di.xml in the file path app/code/Dckap/CustomModule/etc/di.xml to set the preference to our custom TransportBuilder class for the core TransportBuilder.Step 8:It’s all set now! We will be able to send an email to accounting team with the pdf file attached by the customer.The following snippet can be added in any action from which the email has to be sent. Declare the template path in a variable as follows
First Impressions Matter! Make your online store alluring and likable from the first sight with our Magento Design Support