使用Sendgrid的JSON编码标头 [英] JSON encoding headers using Sendgrid
问题描述
我正在尝试使用sendgrid更改 subscriptiontrack的过滤器状态。我认为我发送的标题不正确,但不能完全确定。在symfony 1.4框架内工作。
I am trying to change the filter status for the 'subscriptiontrack' using sendgrid. I think I am sending the headers incorrectly, but not totally sure. Working inside a symfony 1.4 framework.
首先,我创建标头设置的对象
$hdr = new SmtpApiHeader();
$hdr->addFilterSetting('subscriptiontrack', 'enable', 0);
$hdr->as_string();
设置过滤器设置并编码字符串
which sets the filter settings and encodes the string
然后我将其从电子邮件班级中发送出去
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());
}
}
}
电子邮件已正确发送,但退订选项仍显示在底部。这是标题对象的问题还是标题编码的问题?将变量添加到标头时,它仍然是对象吗?
The email is getting sent properly, but the unsubscribe option is still showing up at the bottom. Is this an issue with the header object or a problem with encoding for the header? Is the variable is still an object when getting added to the headers?
推荐答案
您误解了JSON编码的工作原理。让我们看一下您的 as_string
方法:
You're misunderstanding how JSON encoding works. Let's take a look at your as_string
method:
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不知道。
You should note that this isn't valid JSON because it is prefixed with "X-SMTPAPI". Instead, you should be calling asJSON
, but SwiftMailer doesn't know that.
尝试将标题行切换为:
$message->getHeaders()->addTextHeader('X-SMTPAPI', $sgHeaders->asJSON());
如果这不起作用,您能给向我们转储以下内容:
If that doesn't work, can you give us a dump of:
$headers = $message->getHeaders();
echo $headers->toString();
您是否考虑过使用官方PHP库? https://github.com/sendgrid/sendgrid-php
And have you thought about using the official PHP library instead? https://github.com/sendgrid/sendgrid-php
这篇关于使用Sendgrid的JSON编码标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!