使用Swiftmailer将唯一参数传递给SendGrid

时间:2017-02-25 13:09:54

标签: php sendgrid swiftmailer

您好我使用php邮件程序Swiftmailer使用SMTP API通过SendGrid发送。我们可以将唯一参数传递给SendGrid以帮助跟踪电子邮件。我已成功通过Swiftmailer传递唯一标头:

$headers = $message->getHeaders();

        $args = '  {
    "customerAccountNumber": "55555",
    "activationAttempt": "1",
    "New Argument 1": "New Value 1",
    "New Argument 2": "New Value 2",
    "New Argument 3": "New Value 3",
    "New Argument 4": "New Value 4"

}';
        $headers->addTextHeader('unique_args', $args);

虽然这些值已成功注入已发送电子邮件的标题中,但它们不会被SendGrid传回我的事件webhook或控制面板中。

我也试过这个,但是没有用:

{
 "query" : "sample",
 "search_engine" : "google" //can be anything else such as bing or etc.
}

这可能是SendGrid支持的一个问题,但由于它与Swiftmailer有关,我想我先试试这里。谢谢!

2 个答案:

答案 0 :(得分:2)

方法addTextHeader用于一个值

$headers->addTextHeader('unique_args', $args);

您应该使用addParameterizedHeader方法,而不是像这样

$headers->addParameterizedHeader(
   ["customerAccountNumber" => "55555"],
   ["activationAttempt" => "1"]
);

答案 1 :(得分:2)

SendGrid expects a JSON key/value structure in the X-SMTPAPI header entry

我有一个使用SwiftMailer 5.0.3的旧项目。这是我将类别值发送到SendGrid的方式:

$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode(array(
    'category' => $category,
)));

将该模式应用于您的示例:

$headers = $message->getHeaders();

$args = array(
    'unique_args' => array(
        'AccountId' => $accountId,
        "customerAccountNumber" => "55555",
        "activationAttempt" => "1",
        "New Argument 1" => "New Value 1",
        "New Argument 2" => "New Value 2",
        "New Argument 3" => "New Value 3",
        "New Argument 4" => "New Value 4",
    ),
);

$headers->addTextHeader('X-SMTPAPI', json_encode($args));