简体   繁体   English

Sendgrid Web API v3为每个收件人提供不同的替换值

[英]Sendgrid Web API v3 different substitution value for each recipient

I want to send personalized emails with SendGrid. 我想用SendGrid发送个性化的电子邮件。 The whole body is similar, it's just 3-4 one-word-substitions in each mail, so I thought of using SendGrid substitutions 整个身体是相似的,每封邮件只有3-4个单词替换,所以我想到使用SendGrid替换

  • Bob (bob@example.com) should get an Email saying "Hi Bob, lorem ipsum" 鲍勃(bob@example.com)应该收到一封电子邮件,上面写着“Hi Bob,lorem ipsum”
  • Alice (alice@example.com) should get an Email saying "Hi Alice, lorem ipsum" Alice(alice@example.com)应该收到一封电子邮件,上面写着“Hi Alice,lorem ipsum”

The environment is a CodeIgniter-Installation using the provided PHP-Class installed by composer. 环境是CodeIgniter-Installation,使用由composer安装的PHP-Class

The problematic function call is addSubstitution($key, $value) , which leads to an error 400 (Bad Request). 有问题的函数调用是addSubstitution($ key,$ value) ,这会导致错误400(错误请求)。 When submitting the requests without this call, everything works as expected (including my placeholders not substituted of course). 在没有此调用的情况下提交请求时,一切都按预期工作(包括我的占位符当然不能替换)。 I'm getting a clean 202 , the emails are arriving. 我收到了干净的202 ,电子邮件到了。 The error text provided by SendMail is {"errors":[{"message":"Bad Request","field":null,"help":null}]} which does not help much. SendMail提供的错误文本是{"errors":[{"message":"Bad Request","field":null,"help":null}]}这没有多大帮助。

I thought of adding an array of values to the substitution key. 我想到为替换键添加一个值数组。 This is copied from this and this code (using the SMTP API in the first example, unclear what in the second), but it seems, that the value of addSubstitution can only handle strings. 这是从这个这个代码复制的(在第一个例子中使用SMTP API,不清楚第二个中的内容),但似乎addSubstitutionvalue只能处理字符串。

To be clear: I need this functionality in it's generic approach. 要明确:我需要这个功能的通用方法。 My problem does not only concern the recipients names in the greeting, also a personalized unsubscribe link etc. I'm adding this hint because an answer like "use the Sendgrid-Marketing-API and upload your recipients in before" does not serve my needs. 我的问题不仅涉及问候语中的收件人姓名,还有个性化的取消订阅链接等。我正在添加此提示,因为“使用Sendgrid-Marketing-API并在之前上传您的收件人”之类的答案无法满足我的需求。

My PHP script (light version): 我的PHP脚本(简易版):

// General

$sg = new \SendGrid('api_key');

$recipients = array(
    array(
        'email' => 'bob@example.com',
        'name' => 'Bob'
    ),
    array(
        'email' => 'alice@example.com',
        'name' => 'Alice'
    )
);

$mail = new \SendGrid\Mail();

$from = new \SendGrid\Email('myname', 'myname@mycompany.com');
$mail->setFrom($from);

$mail->setSubject('New mail');

$content = new \SendGrid\Content('text/plain', 'Hi -name-, lorem ipsum');
$mail->addContent($content);

// Personalizations
$personalization = new \SendGrid\Personalization();

$substitutions_name = array();

foreach ($recipients as $recipient) {
    $email = new \SendGrid\Email(null, $recipient['email']);
    $personalization->addTo($email);
    array_push($substitutions_name, $recipient['name']);
}

$personalization->addSubstitution('-name-', $substitutions_name);

$mail->addPersonalization($personalization);

$response = $sg->client->mail()->send()->post($mail);

Is my approach generally wrong? 我的方法一般是错的吗? Is there another similar functionality in SendGrid which serves my needs? SendGrid中还有其他类似的功能可以满足我的需求吗?

Calling the SMTP-API, which seems to have the needed functionality, is no alternative as I don't want call php mail() in fast and long loops. 调用似乎具有所需功能的SMTP-API是不可替代的,因为我不想在快速和长循环中调用php mail()。

Update: As I am digging deeper and deeper my solution should work perfectly. 更新:随着我越来越深入,我的解决方案应该完美无缺。 This SO answer has exactly the same approach. 这个答案的答案完全相同。 But why am I still getting the 400-error? 但为什么我仍然得到400错误? The rest of the code works, as a simple try without the substitution-part shows. 其余的代码工作,作为一个没有替换部分显示的简单尝试。

Edit: The resulting JSON of the PHP-Script 编辑: PHP脚本生成的JSON

{
  "from": {
    "name": "myname",
    "email": "myname@mycompany.com"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "bob@example.com"
        },
        {
          "email": "alice@example.com"
        }
      ],
      "substitutions": {
        "-name-": [
          "Bob",
          "Alice"
        ]
      }
    }
  ],
  "subject": "New mail",
  "content": [
    {
      "type": "text/plain",
      "value": "Hi -name-, lorem ipsum"
    }
  ]
}

Update: Following bwests answer this is the solution for my problem (tested): 更新:以下bwests回答这是我的问题(测试)的解决方案:

[...]

$content = new \SendGrid\Content('text/plain', 'Hi -name-, lorem ipsum');
$mail->addContent($content);

foreach ($recipients as $recipient) {
    $personalization = new \SendGrid\Personalization();
    $email = new \SendGrid\Email(null, $recipient['email']);
    $personalization->addTo($email);
    $personalization->addSubstitution('-name-', $recipient['name']);
    $mail->addPersonalization($personalization);
}

$response = $sg->client->mail()->send()->post($mail);

In v3, substitution values cannot be arrays. 在v3中, substitution值不能是数组。 Personalizations are different from the legacy SMTP API, though the concepts are the same. 个性化与旧版SMTP API不同,但概念是相同的。

Per this example your payload should look like: 根据此示例,您的有效负载应如下所示:

{
  "from": {
    "name": "myname",
    "email": "myname@mycompany.com"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "alice@example.com"
        }
      ],
      "substitutions": {
        "-name-": "Alice"
      }
    },
    {
      "to": [
        {
          "email": "bob@example.com"
        }
      ],
      "substitutions": {
        "-name-": "Bob"
      }
    }    
  ],
  "subject": "New mail",
  "content": [
    {
      "type": "text/plain",
      "value": "Hi -name-, lorem ipsum"
    }
  ]
}

This change was made to make it easier to look at a single Personalization object and see all of the metadata for that specific email, and to reduce errors that were common due to trying to maintain consistent indices across arrays rather than using structtured data. 进行此更改是为了更容易查看单个Personalization对象并查看该特定电子邮件的所有元数据,并减少由于尝试跨阵列维护一致索引而不是使用结构化数据而导致的常见错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM