php - 使用 Sendgrid 的 JSON 编码 header

标签 php symfony-1.4 email sendgrid

我正在尝试使用 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/

相关文章:

symfony1 - 带 * 的 Doctrine 查询

file-upload - Symfony 文件上传 - "Array"存储在数据库中而不是实际文件名

email - 通过javamail使用pop3获取电子邮件的接收日期的问题

ruby-on-rails - 设计是否适用于具有相同帐户的多个 email_id ?

php - 升级到 3.0.0 后无法连接到 Cassandra

php - 如何将特征与与继承的静态方法冲突的非静态方法一起使用?

symfony1 - 如何使用 Symfony $request->getParameter() 检索数组

javascript - 如何使用 php 文件中的 GET 来获取我在 .js 文件中声明的变量?

php - 联系表7数据库扩展查询

php - 使用 Mandrill api 更改格式发送邮件