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.
Basic knowledge of how to customize suiteCRM
First, inside our project folder let’s create a file named
custom_utils.php in this path
This file will contain
jobQueue functions that we can use globally within our application. The gist below contains the code snippet for
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 the
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
task: The task to perform. In our case the value we’ll be passing is
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.
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.
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
The file is a copy of
AOS_PDF_Templates which is located in this path
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.
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";
In this tutorial, we have been able to implement a custom way fo sending dynamic PDF to accounts via email.