这真的是这个问题的延伸
但是我的代码发生了一些变化,所以我想我应该开始一个新问题。我正在使用 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;
}
}
我到了这里
更新 28.8.13
如果我按照尼克的建议进行操作并删除 hdr 调用并运行以下命令:
$headers->addTextHeader('X-Test', 'Some Text');
echo $headers->toString();
然后我得到这个:
消息 ID:<1377710372.521e3124a5c6c@localhost> 日期:2013 年 8 月 28 日星期三 13:19:32 -0400 主题:测试,更多来自:MIME 版本:1.0 内容类型:文本/纯文本;charset=utf-8 Content-Transfer-Encoding:quoted-printable X-Test: Some Text
似乎暗示问题出在 SmtpApiHeader 类上,但它只是 Sendgrid 文档的副本。奇怪的。
更新 30.8.13
我能够获得Sendgrid php 库(根据 Swift 在上一个问题中的记录)以使其正常工作。似乎是处理延迟和添加过滤器的最佳方式。将其合并到 Symfony 1.4 中有点棘手,但周五的测试显示速度和一致性显着提高。