我正在尝试使用 sendgrid 更改“subscriptiontrack”的过滤器状态。我认为我发送的 header 不正确,但不完全确定。在 symfony 1.4 框架内工作。
首先我创建一个标题设置的对象
$hdr = new SmtpApiHeader();
$hdr->addFilterSetting('subscriptiontrack', 'enable', 0);
$hdr->as_string();
设置过滤器设置并对字符串进行编码
然后我将其从电子邮件类(class)中发送
sendTestEmail::sendEmail($contents, $mailFrom, $testGroup, $subject, $hdr);
SvaSmtpApiHeader.class.php
class SmtpApiHeader
{
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;
}
}
myEmail.class.php
<?php
class sendTestEmail
{
public static function sendEmail($contents, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)
{
try {
/*
* Load connection for mailer
*/
$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername(sfconfig::get('app_sendgrid_username'))->setPassword(sfconfig::get('app_sendgrid_password'));
// setup connection/content
$mailer = Swift_Mailer::newInstance($connection);
$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);
$message->setBody($contents, 'text/html');
// if contains SMTPAPI header add it
if (null !== $sgHeaders) {
$message->getHeaders()->addTextHeader('X-SMTPAPI', $sgHeaders);
}
// update the from address line to include an actual name
if (is_array($mailFrom) and count($mailFrom) == 2) {
$mailFrom = array(
$mailFrom['email'] => $mailFrom['name']
);
}
// add attachments to email
if ($attachments !== null and is_array($attachments)) {
foreach ($attachments as $attachment) {
$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);
$message->attach($attach);
}
}
// Send
$message->setFrom($mailFrom);
$mailer->send($message);
}
catch (Exception $e) {
throw new sfException('Error sending email out - ' . $e->getMessage());
}
}
}
电子邮件已正确发送,但取消订阅选项仍显示在底部。这是 header 对象的问题还是 header 编码的问题?添加到标题时变量仍然是对象吗?
最佳答案
您误解了 JSON 编码的工作原理。让我们看一下您的 as_string
方法:
function as_string()
{
$json = $this->asJSON();
$str = "X-SMTPAPI: " . wordwrap($json, 76, "\n ");
return $str;
}
这会输出一些效果:
X-SMTPAPI: { "filters": { "subscriptiontrack": { "settings": { "enable": 0 } } } }
您应该注意,这不是有效的 JSON,因为它带有“X-SMTPAPI”前缀。相反,您应该调用 asJSON
,但 SwiftMailer 不知道这一点。
尝试将标题行切换为:
$message->getHeaders()->addTextHeader('X-SMTPAPI', $sgHeaders->asJSON());
如果这不起作用,您能给我们转储一下:
$headers = $message->getHeaders();
echo $headers->toString();
您是否考虑过使用官方 PHP 库? https://github.com/sendgrid/sendgrid-php
关于php - 使用 Sendgrid 的 JSON 编码 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097217/