Yii2 Mandrill email soft verification

368 Views Asked by At

i am using yii2 plugin nickcv and i want to know that email status just like soft verification or email delivered or not. can anyone help me??

Thanks in advance.

\Yii::$app->mailer
->compose('mailViewName', ['model' => $model])
->setTo('[email protected]')
->send();
3

There are 3 best solutions below

1
On BEST ANSWER

Can you try it. it works for me..!

    echo "<pre>";
    print_r(Yii::$app->mailer->getLastTransaction());die;

output will be :-

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [status] => rejected
            [_id] => abcabcabacbac
            [reject_reason] => soft-bounce
        )

)
2
On

Mandrill tracks emails on a per-message basis, so opens, clicks and delivery data are tracked for individual emails sent through your account. You can add tags and metadata to give further context to the emails you send.

Messages API

Mandrill automatically indexes a number of fields that you can use to search or find interesting trends relevant to your sending. Searching is available in the web application and via the Messages-API.

So you need to look into Messages API /messages/info.json, which retrieves the information for a single recently sent message. When you send a message mandrill responses with

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [status] => sent
            [reject_reason] => hard-bounce
            [_id] => abc123abc123abc123abc123abc123
        )

)

the _id=>abc123abc123abc123abc123abc123 can be used to check the status of the email by sending a call to the messages/info.json, php sample code is provided that can be easily converted to Yii2. see below

<?php
try {
    $mandrill = new Mandrill('YOUR_API_KEY');
    $id = 'abc123abc123abc123abc123';
    $result = $mandrill->messages->info($id);
    print_r($result);
    /*
    Array
    (
        [ts] => 1365190000
        [_id] => abc123abc123abc123abc123
        [sender] => [email protected]
        [template] => example-template
        [subject] => example subject
        [email] => [email protected]
        [tags] => Array
            (
                [0] => password-reset
            )

        [opens] => 42
        [opens_detail] => Array
            (
                [0] => Array
                    (
                        [ts] => 1365190001
                        [ip] => 55.55.55.55
                        [location] => Georgia, US
                        [ua] => Linux/Ubuntu/Chrome/Chrome 28.0.1500.53
                    )

            )

        [clicks] => 42
        [clicks_detail] => Array
            (
                [0] => Array
                    (
                        [ts] => 1365190001
                        [url] => http://www.example.com
                        [ip] => 55.55.55.55
                        [location] => Georgia, US
                        [ua] => Linux/Ubuntu/Chrome/Chrome 28.0.1500.53
                    )

            )

        [state] => sent
        [metadata] => Array
            (
                [user_id] => 123
                [website] => www.example.com
            )

        [smtp_events] => Array
            (
                [0] => Array
                    (
                        [ts] => 1365190001
                        [type] => sent
                        [diag] => 250 OK
                    )

            )

    )
    */
} catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Message - No message exists with the id 'McyuzyCS5M3bubeGPP-XVA'
    throw $e;
}
?>

Webhooks

Also you can setup WebHooks on you mandrill account and point it to your servers page say yoursite.com/mandrill-webhook and while creating the webhook you can specify which events would trigger this webhook, select send, open, click and save now whenever any of these events will trigger it will post an array with json that tells which even triggered. When you will setup the webhook for the first time you can send a test call to your url which will post all 3 of these events in like below.

Array
(
    [mandrill_events] => [
  {
    "event": "send",
    "msg": {
      "ts": 1365109999,
      "subject": "This an example webhook message",
      "email": "[email protected]",
      "sender": "[email protected]",
      "tags": [
        "webhook-example"
      ],
      "opens": [],
      "clicks": [],
      "state": "sent",
      "metadata": {
        "user_id": 111
      },
      "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
      "_version": "exampleaaaaaaaaaaaaaaa"
    },
    "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa",
    "ts": 1530365793
  },
  {
    "event": "open",
    "msg": {
      "ts": 1365109999,
      "subject": "This an example webhook message",
      "email": "[email protected]",
      "sender": "[email protected]",
      "tags": [
        "webhook-example"
      ],
      "opens": [
        {
          "ts": 1365111111
        }
      ],
      "clicks": [
        {
          "ts": 1365111111,
          "url": "http://mandrill.com"
        }
      ],
      "state": "sent",
      "metadata": {
        "user_id": 111
      },
      "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
      "_version": "exampleaaaaaaaaaaaaaaa"
    },
    "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa1",
    "ip": "127.0.0.1",
    "location": {
      "country_short": "US",
      "country": "United States",
      "region": "Oklahoma",
      "city": "Oklahoma City",
      "latitude": 35.4675598145,
      "longitude": -97.5164337158,
      "postal_code": "73101",
      "timezone": "-05:00"
    },
    "user_agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100317 Postbox/1.1.3",
    "user_agent_parsed": {
      "type": "Email Client",
      "ua_family": "Postbox",
      "ua_name": "Postbox 1.1.3",
      "ua_version": "1.1.3",
      "ua_url": "http://www.postbox-inc.com/",
      "ua_company": "Postbox, Inc.",
      "ua_company_url": "http://www.postbox-inc.com/",
      "ua_icon": "http://cdn.mandrill.com/img/email-client-icons/postbox.png",
      "os_family": "OS X",
      "os_name": "OS X 10.6 Snow Leopard",
      "os_url": "http://www.apple.com/osx/",
      "os_company": "Apple Computer, Inc.",
      "os_company_url": "http://www.apple.com/",
      "os_icon": "http://cdn.mandrill.com/img/email-client-icons/macosx.png",
      "mobile": false
    },
    "ts": 1530365793
  },
  {
    "event": "click",
    "msg": {
      "ts": 1365109999,
      "subject": "This an example webhook message",
      "email": "[email protected]",
      "sender": "[email protected]",
      "tags": [
        "webhook-example"
      ],
      "opens": [
        {
          "ts": 1365111111
        }
      ],
      "clicks": [
        {
          "ts": 1365111111,
          "url": "http://mandrill.com"
        }
      ],
      "state": "sent",
      "metadata": {
        "user_id": 111
      },
      "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa2",
      "_version": "exampleaaaaaaaaaaaaaaa"
    },
    "_id": "exampleaaaaaaaaaaaaaaaaaaaaaaaaa2",
    "ip": "127.0.0.1",
    "location": {
      "country_short": "US",
      "country": "United States",
      "region": "Oklahoma",
      "city": "Oklahoma City",
      "latitude": 35.4675598145,
      "longitude": -97.5164337158,
      "postal_code": "73101",
      "timezone": "-05:00"
    },
    "user_agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100317 Postbox/1.1.3",
    "user_agent_parsed": {
      "type": "Email Client",
      "ua_family": "Postbox",
      "ua_name": "Postbox 1.1.3",
      "ua_version": "1.1.3",
      "ua_url": "http://www.postbox-inc.com/",
      "ua_company": "Postbox, Inc.",
      "ua_company_url": "http://www.postbox-inc.com/",
      "ua_icon": "http://cdn.mandrill.com/img/email-client-icons/postbox.png",
      "os_family": "OS X",
      "os_name": "OS X 10.6 Snow Leopard",
      "os_url": "http://www.apple.com/osx/",
      "os_company": "Apple Computer, Inc.",
      "os_company_url": "http://www.apple.com/",
      "os_icon": "http://cdn.mandrill.com/img/email-client-icons/macosx.png",
      "mobile": false
    },
    "url": "http://mandrill.com",
    "ts": 1530365793
  }]
)
0
On

This can be accomplished by setting headers before sending.

The header you are looking for will be : X-MC-Track

Read documentation here

And you will be able to set them by

$message = Yii::$app->mailer->compose(...);
$message->getSwiftMessage()->getHeaders()->addTextHeader('X-MC-Track', 'opens,clicks');

Maybe it needs some test and tweaking you should use a loca testing smtp server like https://mailcatcher.me/ that will help you checking that those headers are being set.