Yii2 Mandrill 邮箱软验证

Posted

技术标签:

【中文标题】Yii2 Mandrill 邮箱软验证【英文标题】:Yii2 Mandrill email soft verification 【发布时间】:2018-12-08 14:54:40 【问题描述】:

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

提前致谢。

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

【问题讨论】:

什么是“软验证”? 山魈提供 【参考方案1】:

Mandrill 会根据每封邮件跟踪电子邮件,因此会跟踪通过您的帐户发送的各个电子邮件的打开、点击和递送数据。您可以添加标签和元数据,为您发送的电子邮件提供更多上下文。

消息 API

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

因此,您需要查看 Messages 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来检查邮件的状态,提供了可以轻松转换为Yii2的php示例代码。见下文

<?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,选择 sendopenclick并立即保存,只要这些事件中的任何一个将触发,它就会发布一个带有json 的数组,告诉它甚至触发了哪个。当您第一次设置 webhook 时,您可以向您的 url 发送一个测试调用,该调用将在下面发布所有这 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
  ]
)

【讨论】:

我认为它的核心 php 代码不适用于 yii2,它在核心 php 中的工作但不在 yii2 中 我觉得你的理由不够充分,因为没有无法在 Yii 中运行的 php 代码,唯一需要更改的是命名空间 Mandril\Mandrill @nageennayak【参考方案2】:

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

    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
        )

)

【讨论】:

太棒了,我就是想要它。【参考方案3】:

这可以通过在发送前设置标头来完成。

您要查找的标题是:X-MC-Track

阅读文档here

你可以设置它们

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

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

【讨论】:

以上是关于Yii2 Mandrill 邮箱软验证的主要内容,如果未能解决你的问题,请参考以下文章

Yii2.0 rules验证规则

Yii2.0 rules验证规则

yii2 验证规则使用方法

yii2 unique 验证

Yii2.0 rules验证规则大全

Yii2.0 rules验证规则大全