Email PDF using logic hook

Hello,

Suite does not provide the possibility to email a PDF using workflow. Only text can be sent.

Is it possible to email a PDF using a logic hook? Just like when clicking on the “Email PDF” link for quotes and invoices.

In our company we need to add PDF’s to reminder emails and I’m doing some research on how to achieve this.

Hi,

You should be able to create an Email Template and add Attachments to it.
(Via the attachments section near the bottom of the page when editing an Email Template)

Then, you can use this template in the Workflow

This will send an Email with the Attachment. (as the Email template contains the attachment)

Hi John,

Thanks for your quick response.

Using an email template it’s possible to add an attachment to emails generated from within the Workflow, but these attachments will be fixed. Our requirement is to send out reminder emails for invoices and each email should contain the PDF of the particular invoice. As far as I know this is not possible without some code customisation (please correct me if I’m wrong).

My idea is to change the Workflow action from sending out an email to creating a new record in some new module. In this new module I will create a logic hook using a before_save or after_save event. This logic hook will take care of generating the PDF of the invoice, attach it to an email, and send it out to the client.

Do you think this is the best solution and is it possible to generate the PDF of the invoice and send it out using custom code?

Hi,

That sounds like it will be the best way to proceed with this.

I’ve never done this myself but I imagine it is certainly possible to do using Logic Hooks.

There was previously a page on the SugarCRM Dev blog that may have assisted: http://developer.sugarcrm.com/2013/07/10/download-a-file-in-a-logic-hook/

Although it appears the code parts of this page have vanished, but you can see that he has indeed used before_save and after_save logic hooks, so I imagine that will be a good idea when programming this.

This may also help with the code for sending emails: http://developer.sugarcrm.com/2011/03/01/howto-send-an-email-inside-sugar-thru-code/

There is also this, for sending emails with attachments via the code: https://johndopenotes.wordpress.com/2013/03/26/sugarcrm-send-email-with-attachment-using-sugarphpmailer-class/

Although, I’m not sure what version of SugarCRM that is for, so it may or may not be viable.
(Anything that would work in SugarCRM CE 6.5.x, should work in SuiteCRM)

Hopefully these links give you a good place to start!

1 Like

Thanks John! They definitely give me a good place to start. :slight_smile:

Hi John,

I gave it a try this weekend and got it to work. I can now send reminder emails with the actual invoice PDF attached to them which is great.

In order to do so I had to change generatePdf.php (in modules/AOS_PDF_Templates) so the solution is not upgrade safe.

I tried to put the new version of generatePdf.php in custom/modules/AOS_PDF_Templates but then it isn’t picked up. The old version is used instead.

Do you know if it’s somehow possible to make the system use my updated file in an upgrade safe way?

Hi,

Have you added it as a custom entrypoint?
You may have to do so for it to use your custom generatePDF file.

Navigate to : /custom/include/MVC/Controller/
If this directory doesn’t exist, feel free to create it.

In this location, create the file: “entry_point_registry.php”

In this file, you will have to add:

<?php

$entry_point_registry['generatePdf'] = array('file' => 'custom/modules/AOS_PDF_Templates/generatePdf.php' , 'auth' => '1');

(If your custom generatePDF file is named something different, or is in another location, edit the file location in the code section above)

Then run a Quick Repair and Rebuild in your CRM and try to generate a PDF again, it should hopefully pick up your custom generatePDF file.

1 Like

That did the trick.Thanks again!

1 Like

Hello Luolun,

can you post some more Details for for newbies in coding? :slight_smile:

Will be awesome to get this work within the regular invoice module on the first safe action.

Thank you.

Regards.

Instead of using a logic to hook to do the actual heavy lifting I would have used a logic hook to put that activity in the job queue. Logic hooks slow down the time it takes to save your record. This can be bad for user experience. If you move it to the job queue then you get a faster save and if the activity/script fails you can track it back to the job.

Hi Luolun, can you upload the files you create/modify?or post the codes?
I think that you had done a great job, it will be very useful for some of us.

Thanks again!

Hi Jhon,

We are also looking to implement similar kind of functionality. Can you please share the changes you made in the file in order to achieve this functionality.

Thanks in advance!

It would be nice to know how to do that :slight_smile:

Hi Luolun,

I’d also be interested to know how you got this to work! I understand the general concept of custom workflows, but haven’t gone beyond calculating/changing field values and relationships. how do you interact with the email functionality via code?

I’m sure it would be a great help if you would provide us with some code examples!

Hi Luolun,

I am trying to acheve the same, do you mind to share the modification you have done? Appreciate a lot.

Nobody ever does share their solutions and despite this question having been asked in various iterations over the years, it’s interesting how it’s never built into the functionality despite there being a paid module with a Suite CRM email address to direct questions to if you need the functionality. Just one more example of a module that, without a paid upgrade, does not achieve anything but is a waste of time. I’ve developed 37 different email templates over the weekend just to find that there is no way to attach them automatically to a record through workflow and there is no way of attaching them to an Email either. The question is, exactly what use is a manual intervention PDF when you could attach the same via Outlook or Thunderbird etc?

It’s 2019 and the new year bells have rung many times (as we admittedly get near to even more going into 2020 in a little over 17 weeks) since many posts were created on this topic and, still, Suite CRM has no ability to Email a dynamically generated PDF. Many people have asked (Going back over 5 years) and, it would seem every frustrated user that had hoped for a response from the Developers and others with a solution will have been destined for disappointment. here’s what I found on the topic having gone round and round in circles:

https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/1149-attaching-pdf-template-to-e-mail

https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discusion-general/21407-how-to-attach-pdf-template-to-email-template

https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/12735-email-pdf-template-as-attachment-through-workflow

https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/8386-email-pdf-using-logic-hook

https://suitecrm.com/suitecrm/forum/advanced-opensales-workflow-portal-support/12659-email-pdf-template-as-attachment-through-email-workflow

https://suitecrm.com/suitecrm/forum/developer-help/1080-generate-pdf-with-worklflow-module-and-send-it-as-mail-attachment

Aside from noticing that there is a third party extension module in the Suite CRM store, I can’t see how, with repeated need demonstrated over the years, that Suite CRM would not have noticed the requests for dynamically generated PDF to Email functionality and I can only imagine the dismay at the functionality being unexpectedly discovered as not present.

From a usability perspective, I am extremely sympathetic of anyone’s “plug and play” starting approach to Suite CRM - to see what options are available and what populated menus items are available from the get-go as hints in terms of Suite CRM built in functionality before further exploration.

So, in terms of this approach, a new user sees “PDF Templates”. they see “Workflow”, “Cases”, they see “Email” and “Email Templates” and they see that PDF Templates, Emails and cases are available within the workflow module too as options for Workflow rules. Within the PDF Templates option, a new user sees the ability to add in variables for dynamic generation.

So, you’d be forgiven for entering in your data and expecting to be able to get Suite CRM, within its’ GUI, without additional code, to work in terms of dynamically generating PDF’s and attaching them to Emails and to add updates to case records as the result of workflow automation (particularly in terms of development testing to know that each automation stage worked correctly) - after all, if the feature is not intended to work out of the box then what’s the point in even bothering to add it as a menu item in the GUI when custom functionality is routinely added from the drop down editor and through the creation of ad-hoc relationships and the addition of fields through the Suite CRM built in studio and wider admin functionality?

I have had a separate conversation on the forums last week about this approach and, it would seem from Sales Agility (the company behind the maintenance of Suite CRM)'s perspective that people who attempt to use the software out of the box are misguided by simply expecting it to work and that they should expect to have to do custom coding as standard. I will emphasise, it was quite obvious that custom configuration (within the GUI) and custom coding were clearly very different in that thread.

Personally i’m extremely cynical about such core functionality being left out of Suite CRM. The Developers maintain that they have a very small team and that they can’t get around to things straight away. Emailing of dynamic PDF’s as attachments has been a request around for over 5 years and I have seen many other massive and smaller projects completed by the Development team in that time through review on Github. Meanwhile, we see a paid for extension (purported to be a third party extension) for $70 on the Suite CRM store providing functionality for something ignored over 5 years.

As I have said before, there is clearly no point in having a PDF template mechanism if it has to be manually activated on a one time basis, given the amount of manual interaction, any PDFs might as well be virtually printed from any software and attached in your favourite Email client (Outlook, Thunderbird to name a few).

Similarly within Workflow, to demonstrate the scale of the problem, there is not even a way to automate the dynamic generation of a PDF Template for adding to a client record such that an Email could be generated notifying the client of the newly generated PDFs existence (in the absence of emailing the dynamically generated PDF as an Email attachment in the first place). There is also no way of updating a client’s notes through workflow automation either (but there is an ability to create “internal” (private) notes not published to the Joomla plugin portal) - I think this is a bug.

I’m sure you can, by now, see how my project has been a struggle from start to (no)finish. Purported functionality should not be populated in the menu items, it should be grayed out until premium activation, or should work from the get-go. Functionality that is exceptionally limited should be highlighted as so by these means. Not everybody (certainly not I) is a coder but some of us look to Open Source software to do completely free community work (I mean not a penny changes hands and no back door benefits either, not even a chocolate bar!).

I host on a home computer for example. Not all of us are in a position to pay coders exuberant amounts of money for functionality and nor should we be held to ransom, not by misguiding ourselves (as nauseatingly previously insinuated) as to Suite CRM (or SA)'s abilities but by being misled in, what is at best, features rolled out before they are complete, at worst, deliberate mis-advertisement of capabilities by virtue of their population within the GUI so that users get hooked by their time investment and later discover that they are completely stuck. Both fall squarely under the responsibility of Suite SRM Development and not the user.

It’s only fair if I suggest a much more innocent return on behalf of Suite CRM - that they have created populated menu items (such as PDF Templates, Workflows etc) to advertise the capabilities of Suite CRM presuming custom coding development but even this to me is extremely misguided as the types of people to rely on a GUI are not going to be those capable of custom coding and a blank screen in your favourite code editor is also capable of being turned into your wildest dreams with the right coding skills.

I hope this post saves people a lot of searching and, along with responses, combines the various posts into one topic, illustrates the need for it and, with a bit of optimism, actually leads to the matter being solved once and for all as its been going on for 5 years too long and I suspect most people here (that are not on the Development Team or associated with them) would agree.

The Code for Doing It Yourself

I notice many who have asked have subsequently not shared their solutions for doing it themselves.

With gratitude and full credit to @Jason-Dang on the Suite CRM Development - he has said the following and given me permission to share it:

…This requires custom code if you want to attach a parsed PDF. You can add attachments to email templates including PDFs but these would be static and not parsed with user data…

To get you started what you’ll need to do is (in a logic hook or whatever):

Load your email and PDF template beans.
Build an array of beans you want to parse the templates with:

 '{ModuleName}' => '{recordId}'.

For email templates you can call parse_template on the template object and pass in the subject, body and bean array like below:

 $subject = $emailTemplate->parse_template($emailTemplate->{'subject'}, $beanArray);

      $body = $emailTemplate->parse_template($emailTemplate->{'body'}, $beanArray);

      $bodyHtml = $emailTemplate->parse_template($emailTemplate->{'body_html'}, $beanArray);

For the PDF, use the templateParser class instead as the placeholder variables they parse differ (plural vs singular).
You’ll want to essentially replicate most of what’s in modules/AOS_PDF_Templates/generatePdf.php, replacing html/js and template variables, then use mPDF to generate the file, output it to the server (temp or permanent depending on use cases).

Lastly with your parsed templates, you use SugarPHPMailer to send the email with the parsed email template and the parsed PDF attached (You can find examples of this within the core of Suite).

2 Likes

I’ve took this up here more widely and i’m trying to forward everyone to the same thread - https://suitecrm.com/suitecrm/forum/suitecrm-7-0-discussion/8386-email-pdf-using-logic-hook?start=15#88923 for comment and contribution…

Raised officially https://github.com/salesagility/SuiteCRM-Portal-Joomla/issues/36 here and
https://github.com/salesagility/SuiteCRM/issues/7788 here.

Hi all, I’m not a coder and I’m looking for a solution to automatically send (via workflow) an email with a dynamic pdf attachment; in a custom module, that is related to a contact, every month I need to upload a pdf file to be sent to a contact. Nowaday I upload the file in suitecrm and send it with a separate mail app.
I’ve looked everywhere but I cant’t find a solution, free or in the store.
About the instructions in this thread, they aren’t enough complete for my very limited experience.
Are there any development in this way?