# Did the lead reply to email?

**URL:** <https://forum.mautic.org/t/did-the-lead-reply-to-email/6880>\
**Category:** General Discussion\
**Created:** [December 16, 2016, 7:35pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880 "2016-12-16T19:35:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![denispavliha](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@denispavliha](https://forum.mautic.org/u/denispavliha)\
**Post date:** [December 16, 2016, 7:35pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/1 "2016-12-16T19:35:07Z")

</div>

I got this idea on how to monitor if a lead replied to an email:

1. We use Mailgun to send emails out, and also have MX records for Mailgun to read incoming emails for a certain subdomain  
  
  
2. Every email Mautic sends out gets sent over Mailgun using that subdomain - e.g., if I want to send as denis.pavliha@domain.si I send as denis.pavliha@sub.domain.si instead, and the lead will then reply to denis.pavliha@sub.domain.si  
  
  
3. When Mailgun receives a reply to denis.pavliha@sub.domain.si, I set up a route that filters messages that are sent to @sub.domain.si and forwards them to denis.pavliha@domain.si **and at the same time, I trigger a webhook using Mailgun where all the email data (to, from, body, subject,…) are included.**  
  
  
4. I capture this Mailgun’s webhook using a custom-developed API that reads the From: field (so that I know which Mautic lead/contact I need to bind it to), and finally use this same API to inject a custom event into Mautic (to this lead).  
  
  
  
What do you guys think? Does it make sense?  
  
  
  
Whereas this last part of inserting data as custom event bound to a certain lead still bothers me… I don’t want to use forms and instead want to generate an event that can be shown under contacts (the same way a ‘contact identified’ or ‘page visited’ is shown). I have looked at many SQL tables (I have a self-hosted instance, of course) but still haven’t found a way to manage this task. Otherwise, the above described points 1 to 3 (and first part of 4) work as expected.  
  
  
  
I am really looking forward towards any feedback, either way.  
  
  
  
Best,  
  
D.

---

<div class="post-metadata">

**Author:** ![denispavliha](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@denispavliha](https://forum.mautic.org/u/denispavliha)\
**Post date:** [December 16, 2016, 7:35pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/2 "2016-12-16T19:35:07Z")

</div>

I got this idea on how to monitor if a lead replied to an email:

1. We use Mailgun to send emails out, and also have MX records for Mailgun to read incoming emails for a certain subdomain

2. Every email Mautic sends out gets sent over Mailgun using that subdomain - e.g., if I want to send as denis.pavliha@domain.si I send as denis.pavliha@sub.domain.si instead, and the lead will then reply to denis.pavliha@sub.domain.si

3. When Mailgun receives a reply to denis.pavliha@sub.domain.si, I set up a route that filters messages that are sent to @sub.domain.si and forwards them to denis.pavliha@domain.si and at the same time, I trigger a webhook using Mailgun where all the email data (to, from, body, subject,…) are included.

4. I capture this Mailgun’s webhook using a custom-developed API that reads the From: field (so that I know which Mautic lead/contact I need to bind it to), and finally use this same API to inject a custom event into Mautic (to this lead).

What do you guys think? Does it make sense?

Whereas this last part of inserting data as custom event bound to a certain lead still bothers me… I don’t want to use forms and instead want to generate an event that can be shown under contacts (the same way a ‘contact identified’ or ‘page visited’ is shown). I have looked at many SQL tables (I have a self-hosted instance, of course) but still haven’t found a way to manage this task. Otherwise, the above described points 1 to 3 (and first part of 4) work as expected.

I am really looking forward towards any feedback, either way.

Best,  
D.

---

<div class="post-metadata">

**Author:** ![denispavliha](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@denispavliha](https://forum.mautic.org/u/denispavliha)\
**Post date:** [December 17, 2016, 1:22pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/3 "2016-12-17T13:22:50Z")

</div>

I think I got it… php+mysqli

[code]// Mautic event data

```
$payload = Array(
	"email" => $email,
	"utm_medium" => "email",
	"utm_source" => "email",
	"utm_campaign" => $subject,
	"email_body" => $body
);  

// Mautic query

$data = 'a:' . count($payload) . ':{';

foreach ($payload as $key => $value)
{
	$data .= 's:' . strlen($key) . ':"' . $key . '";' . 's:' . strlen($value) . ':"' . $value . '";'; 
}

$data .= '}'; 

// Inject event into lead's timeline

$query = "INSERT INTO `lead_utmtags`(`id`, `lead_id`, `date_added`, `query`, `referer`, `remote_host`, `url`, `user_agent`, `utm_campaign`, `utm_content`, `utm_medium`, `utm_source`, `utm_term`) VALUES (0, (SELECT id FROM leads WHERE email='$email' ORDER BY date_added DESC LIMIT 0,1), NOW(), '$data', NULL, NULL, '', NULL, '$subject', NULL, 'email', 'email', NULL)";
mysqli_query($con, $query);

// Update lead's last activity

$query = "UPDATE leads SET last_active=NOW() WHERE email='$email'";
mysqli_query($con, $query);

```

[/code]

I can now see if a lead replied to an email within Mautic using UTM tags:

![](http://shrani.si/f/1s/8y/2EhdbjMp/mautic-mailgun.jpg)

Still looking forward if any of you may provide some feedback or e.g. a better practice.

Best regards,  
D.

---

<div class="post-metadata">

**Author:** ![Urano](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/urano/32/164_2.png) [@Urano](https://forum.mautic.org/u/Urano)\
**Post date:** [April 20, 2017, 11:39pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/4 "2017-04-20T23:39:47Z")

</div>

Wow, great implementation. I am looking to begin to customize mautic. This is a good template.

---

<div class="post-metadata">

**Author:** ![denispavliha](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@denispavliha](https://forum.mautic.org/u/denispavliha)\
**Post date:** [April 21, 2017, 5:09am UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/5 "2017-04-21T05:09:09Z")

</div>

Yeah, there are however some drawbacks:

- I am not using Mautic API or [posting data to Mautic forms](https://medium.com/@jan_linhart/the-simplest-way-how-to-submit-a-form-data-to-mautic-1454d3afd005) but rather writing directly to DB which is not the best practice, I believe.

- I am unable to make any drip decisions/actions/… based on the event with type “UTM Tags Recorded”. [I have asked a few times how this event type could be used for any actions/triggers but received no feedback whatsoever.](https://www.mautic.org/community/index.php/6508-action-trigger-on-utm-tags-recorded/p1#p19136) Maybe it would be better if I triggered some other event than writing this one into DB, e.g. generating a virtual page hit (since page hits can be used in drips).

Best,  
D.

---

<div class="post-metadata">

**Author:** ![poshaq](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@poshaq](https://forum.mautic.org/u/poshaq)\
**Post date:** [November 1, 2017, 2:03pm UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/6 "2017-11-01T14:03:52Z")

</div>

Hi Denis, this sure is ingenious - but isnt there any internal controller function that could be invoked to register these stats? I guess you’ve been looking for something similar - have you found something yet?

I 'd love to see someone in the community to share what they think as well.

---

<div class="post-metadata">

**Author:** ![denispavliha](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@denispavliha](https://forum.mautic.org/u/denispavliha)\
**Post date:** [November 2, 2017, 6:57am UTC](https://forum.mautic.org/t/did-the-lead-reply-to-email/6880/7 "2017-11-02T06:57:48Z")

</div>

Hi,

as mentioned [in this conversation](https://www.mautic.org/community/index.php/8645-how-to-know-if-a-contact-has-answered-an-email-or-not/last):

[quote=26613:@denispavliha]The drawback is that it’s a workaround (not a proper solution) and can’t be used for decisions in campaigns.

To use it as a decision point, my code should be changed that instead of writing into DB, it should call Mautic’s REST API and add the lead (identified by the email) to a campaign where you can continue with activities (it is doable for sure).[/quote]

So I would suggest to re-code that API which writes directly to DB, and instead call the Mautic REST API’s endpoint that best suits the case (e.g. add to segment, or.modify stage, or add to campaign, …).

Best,  
D.
