Imported emails appear under Leads, but Leads don't appear under these emails

I just wanted to confirm if this is by design or not, so let me give you an example:

  1. I create a Lead, e.g. “John Smith” with an email “john.smith@example.com
  2. I send an email to John Smith (doesn’t matter how, let’s say externally)
  3. SuiteCRM automatically picks up the email from my email box, imports it into its database and matches that email to the correct Lead. I can see this email listed on John Smith’s page (Leads module, History panel)

However, if I find that specific email in my SuiteCRM, I don’t see John Smith listed on the Leads panel of that email.

In other words, I see that both records (Lead and Email) are present in the system, and Lead > Email relationship is added, however Email > Lead relationship is missing.

Is that how it should be, by design?

I would expect the relationship is added both ways, so that I can see the Lead (John Smith) on that email record’s page.

Any tips or hints are greatly appreciated.

What you’re describing is the difference between a one-to-many and a many-to-many relationship.

I recommend checking which kind the relationship actually is (in Admin / Studio). It’s possible that it is already many-to-many and in that case all that would be missing is a subpanel to display the information.

1 Like

Hi @pgr and thanks for the quick reply!

I’ve checked in Studio > Leads > Relationships and confirmed that email_leads_rel is a many-to-many relationship.

However, I don’t think the panel is missing. The panel I’m talking about is the one that displays related Leads of an Email. I can see these panels on the pages of individual emails in SuiteCRM.

The panel shows the related leads when I manually add the leads to the email, however it doesn’t automatically relate a Lead that’s already in the system to the new inbound emails.

In other words, what I want to see in the system is that all new emails that I receive from my Leads are automatically related to them. Right now, the emails are related to leads, but not the other way around (i.e. it’s one-way only). I would like to have the leads related to the emails.

This only seems to work if I either:

  • manually add the correct Lead to the Email inside of SuiteCRM (by viewing a specific email and selecting the lead on its Leads panel), or
  • by composing an email to the Lead from within SuiteCRM (e.g. by clicking Emails > Compose) and selecting the Lead under “Related To”.

Then I can see the Leads under the email I’m viewing.

This is strange, but there is another relationship there:

and it looks like the one that is being used… maybe you can try checking these in the database, if you’re technical enough for that. Often there are two rows created in those tables, one for each side of the relationship.

You can try figuring out how a normally (manually) linked email looks like under the hood, and compare that to the imported emails.

It’s likely a bug (or at least an incomplete development) that is not fully relating the two records…

1 Like

Thank you for your input!

I’ve tried digging deeper, and I’ve found out that the History subpanel that I thought shows linked emails, actually shows both linked and unlinked.

Here’s what I’ve found:

In modules/Leads/metadata/subpaneldefs.php this is the part regarding History subpanel:

'history' => array(
            // ... 
            'collection_list' => array(
                'meetings' => array(
                     // ...
                ),
                'tasks' => array(
                     // ...
                ),
                'calls' => array(
                     // ...
                ),

                 // ... skipping for brevity

                'emails' => array(
                    'module' => 'Emails',
                    'subpanel_name' => 'ForHistory',
                    'get_subpanel_data' => 'emails',
                ),
                'linkedemails' => array(
                    'module' => 'Emails',
                    'subpanel_name' => 'ForUnlinkedEmailHistory',
                    'get_subpanel_data' => 'function:get_unlinked_email_query',
                    'generate_select' => true,
                    'function_parameters' => array('return_as_array' => 'true'),
                ),
            ),
            // ...

And here’s the SQL statement from function:get_unlinked_email_query from include/utils.php:

    $return_array['select'] = 'SELECT emails.id ';
    $return_array['from'] = 'FROM emails ';
    $return_array['where'] = '';
    $return_array['join'] = " JOIN (select DISTINCT email_id from emails_email_addr_rel eear

	join email_addr_bean_rel eabr on eabr.bean_id ='$bean->id' and eabr.bean_module = '$bean->module_dir' and
	eabr.email_address_id = eear.email_address_id and eabr.deleted=0
	where eear.deleted=0 and eear.email_id not in
	(select eb.email_id from emails_beans eb where eb.bean_module ='$bean->module_dir' and eb.bean_id = '$bean->id')
	) derivedemails on derivedemails.email_id = emails.id";

which as I undertand it returns all emails for the specific lead except for the linked emails.

So that means that imported emails are not being linked to my Leads in any way, not one-way, not any another way, unless I manually link them and/or directly send emails to my leads from within SuiteCRM.

I want the emails to be automatically linked to my Leads and vice-versa. Not sure how I should proceed.
Any tips are greatly appreciated!

I don’t really have an answer for you… sorry.

I’ve recently rediscovered the above PR, I had totally forgotten about it and I don’t know why it hasn’t been merged. Now it’s in need of clean-up and being brought up to date…

But it seems to set the basics for that work. When an email arrives and you want to link it somewhere, often there is guesswork involved: can I find a matching email address? Often the answer will be “yes, several records have it”, because there might be a target, a lead and a contact for that same email…other times you have the email but you want it more specifically linked, say to the current opportunity being discussed.

If I understand that PR correctly, it sends an id inside outgoing emails that allows for much more thorough linking when the email replies come back. Once you have that, it should be simple to add some logic to get it just like you need, according to your business processes.

A simpler way you can try (although much less reliable, I would say) is to try some sort of Workflow or logic hook when the email bean is getting saved…

1 Like

Thanks @pgr for your support!

The way I understood that PR is that it aims to related emails to the original email that was sent from within SuiteCRM. That’s not really what I wanted. I wanted inbound emails to be related to leads (also to accounts and contacts).

Also, that PR looks kinda messy to me (over 1300 modified files), so I’ll leave it for experts to investigate :slight_smile:

I incorrectly assumed that this feature is already implemented, because I saw “related” emails in the History subpanel, which aren’t actually related emails, but are simply matched against the lead’s email when viewed there.

In other words, I don’t see any automatic matching happening under the hood, and perhaps that’s a good thing, for there can be many different use cases and so the system doesn’t impose a specific way of using it.

If there is any way I can see a list of Leads and their corresponding Emails on the same page that can be filtered/searched, that would be very helpful.

I’ve tried using Reports module to achieve this but since there is no link between Emails and Leads I couldn’t get this to work.

I’m going to keep looking and will post here if I’ll manage to solve this.

The PR is not as complicated as it seems right now, it’s showing tons of commits that don’t really belong there. It just needs a clean-up. You can get a better view from here, there you have 99% of it, there were just a few fixes afterwards.

The id that it adds might not be the final thing needed, but it is likely the missing link so that what you need can be achieved with ease. You have the id of your sent email; that is already linked to your Lead, so you have the exact Lead you want, all you have to do is create a relationship from that lead to the new incoming email. The PR (as it is) does user assignments, but it can easily be improved to do other things.

I am quite interested in this problem of parsing incoming info and relating to existing info, BTW. I’ve been toying around with ways of making this a “hookable” moment in the CRM, a moment where custom code could be provided.

Finally, about your idea of trying to get a page with a list of Leads and emails. There are ways to create subpanels with specific SQL queries driving them.

1 Like

Thanks again for the valuable suggestions!

I’ve checked that code and it makes sense.

However, while checking modules/InboundEmail/InboundEmail.php I think I came across something that looks exactly like what I’ve been looking for:

     ...
                ///////////////////////////////////////////////////////////////////
                ////	LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL
                //$contactAddress = $this->handleLinking($email);
                ////	END LINK APPROPRIATE BEANS TO NEWLY SAVED EMAIL
                ///////////////////////////////////////////////////////////////////
     ...

The function handleLinking for some reason is commented out! Any idea why?

The function actually seems to do the linking for imported emails, here’s a snippet:

        if ($leadIds = $this->getRelatedId($relationShipAddress, 'leads')) {
            $GLOBALS['log']->debug('I-E linking email to Lead');
            $email->load_relationship('leads');
            $email->leads->add($leadIds);

            foreach ($leadIds as $leadId) {
                $lead = new Lead();
                $lead->retrieve($leadId);
                $lead->load_relationship('emails');
                $lead->emails->add($email->id);
            }
        }

        if ($contactIds = $this->getRelatedId($relationShipAddress, 'contacts')) {
            $GLOBALS['log']->debug('I-E linking email to Contact');
            // link the contact to the email
            $email->load_relationship('contacts');
            $email->contacts->add($contactIds);
        }

        if ($accountIds = $this->getRelatedId($relationShipAddress, 'accounts')) {
            $GLOBALS['log']->debug('I-E linking email to Account');
            // link the account to the email
            $email->load_relationship('accounts');
            $email->accounts->add($accountIds);
        }

Not sure what “I-E” refers to in the debug messages, but it seems that users can just uncomment that function and this functionality should work.

I’ll test this out.

Edit: here’s the permalink to the function:

Nice find.

“I-E” maybe just means Import Email?

I have no idea why it’s commented out, but it’s probably because of those ambiguities and guesses that I mentioned earlier. There are many ways to link records and some probably don’t work well for every business out there.

Did this work as expected?

Would it parse previously retrieved emails and relate them, or only new emails?

Sorry I didn’t follow up earlier, and now I no longer remember exactly how it went. :see_no_evil:

I think the code worked, although I figured that’s not a very good idea to bloat Email records with links to Leads module, since we don’t really use the Emails module for viewing emails.

Regarding your question: the code only runs for newly imported emails (it also says so in the comment).
So if you want to process some of your previous emails, you can bulk import them in your Emails module.

If your emails were previously imported but not linked, you may want to try deleting them from the database, which should only delete the imported copy of them, and then performing the bulk import as mentioned above.

However, don’t take my word for it since I haven’t tried this. The delete-reimport scenario might work in the case with IMAP setup but maybe your setup uses POP3.