2

我正在尝试使用 sendgrid 更改“订阅轨道”的过滤器状态。我想我发送的标题不正确,但不完全确定。在 symfony 1.4 框架内工作。

首先我创建一个标题设置的对象

$hdr = new SmtpApiHeader();
$hdr->addFilterSetting('subscriptiontrack', 'enable', 0);
$hdr->as_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());
        }
    }
}

电子邮件已正确发送,但取消订阅选项仍显示在底部。这是标头对象的问题还是标头编码的问题?添加到标头时,变量是否仍然是对象?

4

1 回答 1

2

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;
}

This would output something to the effect of:

X-SMTPAPI: { "filters": { "subscriptiontrack": { "settings": { "enable": 0 } } } }

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.

Try switching the header line to:

$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();

And have you thought about using the official PHP library instead? https://github.com/sendgrid/sendgrid-php

于 2013-06-14T12:08:33.867 回答