为Sendgrid启用订阅跟踪

时间:2013-08-23 19:14:40

标签: symfony-1.4 email-integration swiftmailer sendgrid

这实际上是此question

的扩展

但我的代码有所改变,所以我想我应该开始一个新问题。我正在使用SMTP-API来加速我们的服务器和Sendgrid之间的交付。

$hdr = new SmtpApiHeader();
$toList = $mailTo;
$hdr->addTo($toList);
$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);

$to = array('email@email.edu'=>'Carey Estes');
$transport = $connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl');
$transport ->setUsername($username);
$transport ->setPassword($password);
$swift = Swift_Mailer::newInstance($transport);

$message = new Swift_Message($subject);

$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $hdr->asJSON());

$message->setFrom($mailFrom);
$message->setBody($contents, 'text/html');
$message->setTo($to);

if ($recipients = $swift->send($message, $failures))
{
    return true;
}

与之前的问题类似,我无法操纵电子邮件中的取消订阅链接。之前,我无法将其关闭......现在使用新的设置,我似乎无法打开它。

$hdr->addFilterSetting('subscriptiontrack', 'enable', 1);

应该使电子邮件中的取消订阅链接正确吗?无论是0还是1,都不会出现取消订阅链接。

在上一个问题中,我错误地在头文件中构造了JSON。现在是这样吗?

更新

这是$ header变量的var_dump,我看起来不对:

object(Swift_Mime_SimpleHeaderSet)#315 (5) { ["_factory":"Swift_Mime_SimpleHeaderSet":private]=> object(Swift_Mime_SimpleHeaderFactory)#316 (3) { ["_encoder":"Swift_Mime_SimpleHeaderFactory":private]=> object(Swift_Mime_HeaderEncoder_QpHeaderEncoder)#317 (2) { ["_charStream":protected]=> object(Swift_CharacterStream_NgCharacterStream)#318 (9) { ["_charReader":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_charReaderFactory":"Swift_CharacterStream_NgCharacterStream":private]=> object(Swift_CharacterReaderFactory_SimpleCharacterReaderFactory)#319 (2) { ["_map":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(15) { ["utf-?8"]=> array(2) { ["class"]=> string(32) "Swift_CharacterReader_Utf8Reader" ["constructor"]=> array(0) { } } ["(us-)?ascii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(iso|iec)-?8859-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["windows-?125[0-9]"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["cp-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["ansi"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["macintosh"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?7"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?8-?.+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["mik"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(cork|t1)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["v?iscii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(ucs-?2|utf-?16)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(2) } } ["(ucs-?4|utf-?32)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(4) } } [".*"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } } ["_loaded":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(0) { } } ["_charset":"Swift_CharacterStream_NgCharacterStream":private]=> string(5) "utf-8" ["_datas":"Swift_CharacterStream_NgCharacterStream":private]=> string(0) "" ["_datasSize":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_map":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_mapType":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_charCount":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_currentPos":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) } ["_filter":protected]=> NULL } ["_paramEncoder":"Swift_Mime_SimpleHeaderFactory":private]=> object(Swift_Encoder_Rfc2231Encoder)#320 (1) { ["_charStream":"Swift_Encoder_Rfc2231Encoder":private]=> object(Swift_CharacterStream_NgCharacterStream)#321 (9) { ["_charReader":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_charReaderFactory":"Swift_CharacterStream_NgCharacterStream":private]=> object(Swift_CharacterReaderFactory_SimpleCharacterReaderFactory)#319 (2) { ["_map":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(15) { ["utf-?8"]=> array(2) { ["class"]=> string(32) "Swift_CharacterReader_Utf8Reader" ["constructor"]=> array(0) { } } ["(us-)?ascii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(iso|iec)-?8859-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["windows-?125[0-9]"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["cp-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["ansi"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["macintosh"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?7"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?8-?.+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["mik"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(cork|t1)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["v?iscii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(ucs-?2|utf-?16)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(2) } } ["(ucs-?4|utf-?32)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(4) } } [".*"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } } ["_loaded":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(0) { } } ["_charset":"Swift_CharacterStream_NgCharacterStream":private]=> string(5) "utf-8" ["_datas":"Swift_CharacterStream_NgCharacterStream":private]=> string(0) "" ["_datasSize":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_map":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_mapType":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_charCount":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_currentPos":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) } } ["_charset":"Swift_Mime_SimpleHeaderFactory":private]=> string(5) "utf-8" } ["_headers":"Swift_Mime_SimpleHeaderSet":private]=> array(8) { ["content-transfer-encoding"]=> array(1) { [0]=> object(Swift_Mime_Headers_UnstructuredHeader)#314 (9) { ["_value":"Swift_Mime_Headers_UnstructuredHeader":private]=> string(16) "quoted-printable" ["_specials":"Swift_Mime_Headers_AbstractHeader":private]=> array(0) { } ["_grammar":"Swift_Mime_Headers_AbstractHeader":private]=> array(0) { } ["_name":"Swift_Mime_Headers_AbstractHeader":private]=> string(25) "Content-Transfer-Encoding" ["_encoder":"Swift_Mime_Headers_AbstractHeader":private]=> object(Swift_Mime_HeaderEncoder_QpHeaderEncoder)#317 (2) { ["_charStream":protected]=> object(Swift_CharacterStream_NgCharacterStream)#318 (9) { ["_charReader":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_charReaderFactory":"Swift_CharacterStream_NgCharacterStream":private]=> object(Swift_CharacterReaderFactory_SimpleCharacterReaderFactory)#319 (2) { ["_map":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(15) { ["utf-?8"]=> array(2) { ["class"]=> string(32) "Swift_CharacterReader_Utf8Reader" ["constructor"]=> array(0) { } } ["(us-)?ascii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(iso|iec)-?8859-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["windows-?125[0-9]"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["cp-?[0-9]+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["ansi"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["macintosh"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?7"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["koi-?8-?.+"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["mik"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(cork|t1)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["v?iscii"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } ["(ucs-?2|utf-?16)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(2) } } ["(ucs-?4|utf-?32)"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(4) } } [".*"]=> array(2) { ["class"]=> string(45) "Swift_CharacterReader_GenericFixedWidthReader" ["constructor"]=> array(1) { [0]=> int(1) } } } ["_loaded":"Swift_CharacterReaderFactory_SimpleCharacterReaderFactory":private]=> array(0) { } } ["_charset":"Swift_CharacterStream_NgCharacterStream":private]=> string(5) "utf-8" ["_datas":"Swift_CharacterStream_NgCharacterStream":private]=> string(0) "" ["_datasSize":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_map":"Swift_CharacterStream_NgCharacterStream":private]=> NULL ["_mapType":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_charCount":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) ["_currentPos":"Swift_CharacterStream_NgCharacterStream":private]=> int(0) } ["_filter":protected]=> NULL } ["_lineLength":"Swift_Mime_Headers_AbstractHeader":private]=> int(78) ["_lang":"Swift_Mime_Headers_AbstractHeader":private]=> NULL ["_charset":"Swift_Mime_Headers_AbstractHeader":private]=> string(5) "utf-8" ["_cachedValue":"Swift_Mime_Headers_AbstractHeader":private]=> NULL } } ["content-type"]=> array(1) { [0]=> object(Swift_Mime_Headers_ParameterizedHeader)#327 (12) { ["_paramEncoder":"Swift_Mime_Headers_ParameterizedHeader":private]=> NULL ["_params":"Swift_Mime_Headers_ParameterizedHeader":private]=> array(1) { ["charset"]=> string(5) "utf-8" } ["_tokenRe":"Swift_Mime_Headers_ParameterizedHeader":private]=> string(63) "(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)" ["_value":"Swift_Mime_Headers_UnstructuredHeader":private]=> string(10) "text/plain" ["_specials":"Swift_Mime_Headers_AbstractHeader":private]=> array(12) { [0]=> string(1) "(" [1]=> string(1) ")" [2]=> string(1) "<" [3]=> string(1) ">" [4]=> string(1) "[" [5]=> string(1) "]" [6]=> string(1) ":" [7]=> string(1) ";" [8]=> string(1) "@" [9]=> string(1) "," [10]=> string(1) "." [11]=> string(1) """ }

更新

这是SmtpApiHeader类:

<?php 

class SmtpApiHeader
{
    var $data;

    function addTo($tos)
    {
        if (!isset($this->data['to'])) {
            $this->data['to'] = array();
        }
        $this->data['to'] = array_merge($this->data['to'], (array) $tos);
    }

    function addSubVal($var, $val)
    {
        if (!isset($this->data['sub'])) {
            $this->data['sub'] = array();
        }

        if (!isset($this->data['sub'][$var])) {
            $this->data['sub'][$var] = array();
        }
        $this->data['sub'][$var] = array_merge($this->data['sub'][$var], (array) $val);
    }

    function setUniqueArgs($val)
    {
        if (!is_array($val))
            return;
        // checking for associative array
        $diff = array_diff_assoc($val, array_values($val));
        if (((empty($diff)) ? false : true)) {
            $this->data['unique_args'] = $val;
        }
    }

    function setCategory($cat)
    {
        $this->data['category'] = $cat;
    }

    function addFilterSetting($filter, $setting, $value)
    {
        if (!isset($this->data['filters'])) {
            $this->data['filters'] = array();
        }

        if (!isset($this->data['filters'][$filter])) {
            $this->data['filters'][$filter] = array();
        }

        if (!isset($this->data['filters'][$filter]['settings'])) {
            $this->data['filters'][$filter]['settings'] = array();
        }
        $this->data['filters'][$filter]['settings'][$setting] = $value;
    }

    function asJSON()
    {
        $json = json_encode($this->data);
        // Add spaces so that the field can be folded
        $json = preg_replace('/(["\]}])([,:])(["\[{])/', '$1$2 $3', $json);
        return $json;
    }

    function as_string()
    {
        $json = $this->asJSON();
        $str  = "X-SMTPAPI: " . wordwrap($json, 76, "\n ");
        return $str;
    }

}

我得到了here

更新28.8.13

如果我按照Nick的建议去除hdr调用并运行它:

$headers->addTextHeader('X-Test', 'Some Text'); 
                echo $headers->toString();

然后我明白了:

消息ID:&lt; 1377710372.521e3124a5c6c@localhost>日期:2013年8月28日星期三13:19:32 -0400主题:测试,更多来自:MIME版本:1.0内容类型:text / plain; charset = utf-8 Content-Transfer-Encoding:quoted-printable X-Test:Some Text

似乎建议该问题与SmtpApiHeader类有关,但它只是来自Sendgrid文档的副本。奇怪。

更新30.8.13

我能够获得Sendgrid php library(根据前一个问题中的Swift的rec)来使这项工作正常进行。似乎是处理延迟和添加过滤器的最佳方法。将它整合到Symfony 1.4中有点棘手,但周五的测试显示速度和一致性显着提高。

1 个答案:

答案 0 :(得分:1)

我能够获得Sendgrid php library(根据前一个问题中的Swift的rec)来使这项工作正常进行。似乎是处理延迟和添加过滤器的最佳方法。将它整合到Symfony 1.4中有点棘手,但测试运行显示速度和一致性显着提高。