Attaching PDF template to email in SuiteCRM

Photo by Arnold Francisca on Unsplash

I needed to send an invoice to customers based on the product they want to buy when an endpoint is being called.
I searched online for similar implementation but could not find any. I came across this thread on the suitecrm community website which has a couple of people who want to programmatically send pdf template via email but could not because suiteCRM doesn’t support it by default, it requires custom code.
So I will be writing on how I was able to achieve this task for future self and anyone else trying to achieve the same functionality.

Prerequisites

Basic knowledge of how to customize suiteCRM

First, inside our project folder let’s create a file named custom_utils.php in this path custom/include/custom_utils.php
This file will contain sendEmail and jobQueue functions that we can use globally within our application. The gist below contains the code snippet for custom/include/custom_utils.php

Utility functions to send an email and add a job to a queue in suiteCRM

SendEMailas the name implies, it sends emails anywhere in our application by calling and passing the required arguments an example usage will be shown later in this post.

jobQueue this function is used to schedule tasks to be executed at a later time when the systems cron job runs. We need to generate a pdf file in the background because the task can be time-consuming and we don’t want to delay the user’s request response.

Scheduling Task In SuiteCRM

In other to execute scheduled tasks we need to define the function to be executed when our queued task runs. We will specify the function name as the target in the argument to be passed when we call theJobQueue function.
To learn more about job queues in SuiteCRM check out this documentation on Scheduled Tasks.
Note: that both scheduled tasks and using the job queue require that you have the schedulers set up.

The scheduled task target function

We need to create a file name SendInvoice.php in this path custom/Extension/modules/Schedulers/Ext/ScheduledTasks and included the code snippet below.

In this function, we make a call to an entry-point which we will define soon. This entry point requires some query parameters. e.g index.php?entryPoint=generatePdfSendMail&task=emailpdf&templateID=’.$templateId.’&module=AOS_Invoices&uid=’.$invoice_id

task: The task to perform. In our case the value we’ll be passing is emailPdf
templateID: The PDF template id to be used which in my case is the invoice pdf template id.
module: The module which we’ll be passing to the selected pdf template. Which in my case is the AOS_Invoices.
uid: The record id to be fetched from the passed module. The invoice id.

Entrypoint

Entry points are simply a page that provides access to SuiteCRM. To register an entry point we need to create a file named entry_point_registry.php in this path custom/include/MVC/Controller and add the code snippet below. To learn more about entry point in SuiteCRM check out its documentation.

entry_point_registry.php

Now let’s create the generatePDFSendMail.php file which is where all the operation of generating a pdf and sending it to a customer email happens. The file should be saved in this path custom/modules/AOS_PDF_Templates.

The file is a copy of generatePdf.php from AOS_PDF_Templates which is located in this path modules/AOS_PDF_Templates/generatePdf.php

We will be modifying line 145 to 176 with the code snippet below

After the modification of your generatePDFSendMail.php the content should look something like this gist https://gist.github.com/03balogun/6f274b637f3361ab4c55482b11adf810

Testing our implementation

Before testing, we need to make sure to run Quick Repair and Rebuild from the admin section.
There are several ways to test this implementation:

Visiting the entry point URL: If we visit the entry point URL we defined earlier via our browser passing all required query parameters we should get the invoice delivered to our email.
http://localhost/your-crm-project/index.php?entryPoint=generatePdfSendMail&task=emailPdf&module=AOS_Invoices&uid=6c5826b9-2307-8392-302a-5e1c391322f1

Email PDF using logic hook

From any of your logic hook event life circle, you can call the jobQueuefunction we declared earlier. Passing the required parameters. e.g.

$invoice_id = "6c5826b9-2307-8392-302a-5e1c391322f1";
jobQueue($invoice_id, “function::SendInvoiceEmail”);

Conclusion

In this tutorial, we have been able to implement a custom way fo sending dynamic PDF to accounts via email.

--

--

--

A human who writes code and likes to makes amazing things happen.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Defining Moment as a Software Engineer

Task — 19

Control Systems Tutorial

Thoughts on Refactoring

Using Python’s pickling to explain Insecure Deserialization

Flutter PDF Creation With Arabic Context

Who is a Quality Assurance Engineer and what should he know?

Bash Scripting — Intro — Linode Linux Server — Simple Script

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Balogun Wahab

Balogun Wahab

A human who writes code and likes to makes amazing things happen.

More from Medium

Proxy Server Errors: the Main Types and Ways to Fix Them

Cross border data transfers and the case against Google Analytics.

Sharp Notes to SQL Users Migrating to DynamoDB

3 Features To Look For In Your Schema Performance Testing Tool

SeeQR ER Diagram