Yii2 Mandrill邮箱软验证

Yii2 Mandrill email soft verification

我正在使用 yii2 插件 nickcv 我想知道电子​​邮件状态,就像软验证或电子邮件是否已送达一样。谁能帮帮我??

提前致谢。

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

这可以通过在发送前设置 header 秒来实现。

您正在寻找的 header 将是:X-MC-Track

阅读文档here

您可以通过

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

也许它需要一些测试和调整,您应该使用像 https://mailcatcher.me/ 这样的 loca 测试 smtp 服务器,这将帮助您检查是否设置了 header。

Mandrill 按每封邮件跟踪电子邮件,因此会跟踪通过您的帐户发送的每封电子邮件的打开、点击和发送数据。您可以添加标签和元数据,为您发送的电子邮件提供更多背景信息。

消息API

Mandrill 会自动为许多字段编制索引,您可以使用这些字段来搜索或查找与您的发送相关的有趣趋势。可以在 Web 应用程序中和通过 Messages-API.

进行搜索

所以您需要查看消息 API /messages/info.json,它会检索最近发送的单个消息的信息。当您发送消息时山魈回复

Array
(
    [0] => Array
        (
            [email] => recipient.email@example.com
            [status] => sent
            [reject_reason] => hard-bounce
            [_id] => abc123abc123abc123abc123abc123
        )

)

_id=>abc123abc123abc123abc123abc123 可用于通过向 messages/info.json 发送调用来检查电子邮件的状态,php 提供了可以轻松转换为 Yii2 的示例代码。见下文

<?php
try {
    $mandrill = new Mandrill('YOUR_API_KEY');
    $id = 'abc123abc123abc123abc123';
    $result = $mandrill->messages->info($id);
    print_r($result);
    /*
    Array
    (
        [ts] => 1365190000
        [_id] => abc123abc123abc123abc123
        [sender] => sender@example.com
        [template] => example-template
        [subject] => example subject
        [email] => recipient.email@example.com
        [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;
}
?>

网络钩子

您还可以在您的 mandrill 帐户上设置 WebHooks 并将其指向您的服务器页面说 yoursite.com/mandrill-webhook 并且在创建 webhook 时您可以指定哪些事件将触发此 webhook,select sendopenclick 并立即保存,只要这些事件中的任何一个将触发,它将 post 一个带有 json 的数组,该数组告诉甚至触发了哪个。当您第一次设置 webhook 时,您可以向您的 url 发送测试调用,这将 post 所有 3 个事件如下所示。

Array
(
    [mandrill_events] => [
  {
    "event": "send",
    "msg": {
      "ts": 1365109999,
      "subject": "This an example webhook message",
      "email": "example.webhook@mandrillapp.com",
      "sender": "example.sender@mandrillapp.com",
      "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": "example.webhook@mandrillapp.com",
      "sender": "example.sender@mandrillapp.com",
      "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": "example.webhook@mandrillapp.com",
      "sender": "example.sender@mandrillapp.com",
      "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
  }]
)

你可以试试吗。它对我有用..!

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

输出将是:-

Array
(
    [0] => Array
        (
            [email] => tets.test@gmail.comm
            [status] => rejected
            [_id] => abcabcabacbac
            [reject_reason] => soft-bounce
        )

)