Just tested mailgun through its API.
Everything is working fine.
Short: How to track unique opens for a specific mail through webhooks.
Ok I think I got an acceptable workflow - through the “custom variables”.
You can define different values for each recipient, so you can send a unique id and then track that id on the open events. And just save one open for each sender or update the open time.
My sending code (PHP):
$result = $mg->sendMessage($domain, array(
'from' => 'foo@bar.de>',
'to' => 'recipient1@mail.de, recipient2@mail.de',
'subject' => 'Hello %recipient.first% from %recipient.group%!',
'text' => 'Test of Mailgun',
'html' => '<html>It is so simple to send a message.<br/>Right?</html>',
'o:tag' => array('test'),
'o:tracking-opens' => 'yes',
'v:my-custom-data' => '{"my_message_id": %recipient.id%}',
'recipient-variables' => '{
"recipient1@mail.de": {"first":"Recipient1", "group":"group1", "id":1},
"recipient2@mail.de": {"first":"Recipient2", "group":"group2", "id":2}
}'
));
Then in each event you get a response with the unique ids.
Open event of first email:
"user-variables": {
"my-custom-data": "{\"my_message_id\": 1}"
},
Open event of second email:
"user-variables": {
"my-custom-data": "{\"my_message_id\": 2}"
},
Best way to track email openings is so called "pixel". First you need to inject pixel in your email.
For example:
public function insertPixel($user,$template)
{
$output = $template.'<img src="'.Yii::app()->homeUrl.'/mailing/pixel/track?id='.$this->campaign->id.'&user='.$user.'&rand='.rand().'">';
return $output;
}
Which points to php endpoint. At that endpoint you will get openings and do w/e you want with them.
For example:
public function actionTrack()
{
if (isset($_GET["id"])&&isset($_GET["user"])){
Yii::app()->db->createCommand("UPDATE mailing_campaigns SET open_count = open_count + 1 WHERE id=:id")
->bindParam(":id",$_GET["id"],PDO::PARAM_INT)
->execute();
}
header('Content-Type: image/gif');
echo "\x47\x49\x46\x38\x37\x61\x1\x0\x1\x0\x80\x0\x0\xfc\x6a\x6c\x0\x0\x0\x2c\x0\x0\x0\x0\x1\x0\x1\x0\x0\x2\x2\x44\x1\x0\x3b";
exit;
}
This code adjusts open's counter for mailing campaign for example and returns 1x1 transparent .gif
image.
It's not 100% precise because some people not loading images in emails, but its best way I know so far.