After a long series of trials and errors I managed to get it to work. Thanks to @pgr for the ideas - I used them partially in my solution.
I ended up doing the following:
- Set up a bounce-handling email account with auto-import option turned on (so that email hooks get triggered for each incoming message)
- Define a custom logic hook for
Emails
module (as a file under/custom/Extension/modules/Emails/Ext/LogicHooks/
) which gets triggered onafter_save
event; - The hook extracts “From” email address of the incoming email and looks up the prospect(s) with this email and marks them not to be disturbed again by adding them to the corresponding Suppression lists of the email campaigns they have previously been a part of.
Although not a perfect solution, it does exactly what I wanted it to
I’ll leave some useful code fragments here.
How to retrieve a list of all records (beans) with given email address?
$from_email = '<your_email_address_here>';
$sea = new SugarEmailAddress();
$beans = $sea->getBeansByEmailAddress( $from_email );
You can further filter the $beans
array by the object type you need (e.g. $bean instanceof Prospect
).
How to create a new ProspectList for a given Campaign?
// map $campaign_id to the actual Campaign object
$campaign = BeanFactory::getBean( 'Campaigns', $campaign_id );
// ensure prospect lists data is loaded for this campaign
$campaign->load_relationship( 'prospectlists' );
// Create a new prospect list
$prospect_list = new ProspectList();
$prospect_list->name = '<some name here, e.g. $campaign->name . "some_suffix"';
$prospect_list->assigned_user_id = $assigned_user_id;
$prospect_list->list_type = "default"; // either 'default', 'test' or 'exempt'
$prospect_list->save();
// Add this list to the campaign
$campaign->prospectlists->add($prospect_list->id);
I hope this saves some time for someone who comes across this post.
It helped me to read not only SuiteCRM’s documentation but also the docs of its commercial version (SugarCRM):
https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.0/Data_Framework/Models/SugarBean/