将单独的电子邮件发送给多个收件人,而无需将密件抄送与 SparkPost API

Send individual emails to multiple recipients without using BCC with the SparkPost API

我们的 Django 应用程序使用 SparkPost 作为电子邮件提供商。我们正在实施的一项新功能应该允许用户创建自己的组织电子邮件并将其发送给他们希望的任何人。现在,这些电子邮件应该单独接收,而不是与多个收件人 ("to") 一起接收,这样用户就看不到彼此的地址。

我 运行 对 SparkPost transmissions API 进行了一些测试。这是您发送电子邮件的方式:

sp = SparkPost(API_KEY)
response = sp.transmissions.send(recipients=emails, html=body, from_email=sender, subject=self.subject)

其中 emails 是字符串文字列表。

在所有测试用例中,除了一个,我确实收到了一个收件人的单独电子邮件,就像我想要的一样。但在一种情况下,该电子邮件有多个 "to" 封电子邮件,您可以看到彼此的电子邮件地址。我完全没有更改代码,这只是 发生了

除了为每个收件人发送单独的传输之外,我还有什么办法可以做到这一点吗?如果涉及到性能,我很担心:

sp = SparkPost(API_KEY)
for email in emails:
    sp.transmissions.send(recipients=email, html=body, from_email=sender, subject=self.subject)

是的,最好在单个 REST 调用中执行此操作。

默认情况下,SparkPost REST 注入是密件抄送,并将向每个收件人发送单独的电子邮件。如您所见,您也可以拥有典型的 "CC" 行为,但您需要将 CC header 值设置为您希望其他人看到的地址。

所以在包含 CC 的示例中,您在 REST 调用中一定有类似这样的内容:

"headers": {
    "CC": "cc@thatperson.com"
},

CC Example:

{
  "recipients": [
    {
      "address": {
        "email": "to@thisperson.com"
      }
    },
    {
      "address": {
        "email": "cc@thatperson.com",
        "header_to": "to@thisperson.com"
      }
    }
  ],
  "content": {
    "from": "you@fromyou.com",
    "headers": {
      "CC": "cc@thatperson.com"
    },
    "subject": "To and CC",
    "text": "This mail was sent to to@thisperson.com while CCing cc@thatperson.com."
  }
}

BCC Example:

"recipients": [
    {
      "address": {
        "email": "to@thisperson.com"
      }
    },
    {
      "address": {
        "email": "bcc@thatperson.com",
        "header_to": "to@thisperson.com"
      }
    }
  ],
  "content": {
    "from": "you@fromyou.com"
    "subject": "To and BCC",
    "text": "This mail was sent To to@thisperson.com while BCCing an unnamed recipient. Sneaky."
  }
}

在您的用例中,您不想为任何收件人设置 "header_to": "to@thisperson.com"