简体   繁体   English

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

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

Our Django app uses SparkPost as an email provider.我们的 Django 应用程序使用 SparkPost 作为电子邮件提供商。 A new feature we are implementing should allow users to create their own organizational emails and send them to whoever they wish.我们正在实施的一项新功能应允许用户创建自己的组织电子邮件并将其发送给他们希望的任何人。 Now, these emails should be received as individual ones, not with multiple recipients ("to") so that users can't see each other's address.现在,这些电子邮件应该作为单独的邮件接收,而不是多个收件人(“收件人”),这样用户就无法看到彼此的地址。

I have run a few tests with the SparkPost transmissions API .我已经用SparkPost 传输 API运行了一些测试。 This is how you send an email:这是您发送电子邮件的方式:

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

Where emails is a list of string literals.其中emails是字符串文字列表。

In all test cases except one I did get individual emails with a single recipient just as I was after.在除一个之外的所有测试用例中,我确实收到了一个收件人的单独电子邮件,就像我所追求的那样。 But in one case the email had multiple "to" emails, and you could see each other's email address.但在一种情况下,电子邮件有多个“收件人”电子邮件,您可以看到彼此的电子邮件地址。 I changed absolutely nothing in the code, this just happened .我在代码中完全没有改变,这只是发生了

Is there any way I could do that other than sending an individual transmission for each recipient?除了为每个收件人发送单独的传输之外,我还有什么办法可以做到这一点吗? I'm worried about performance if it comes to that:如果涉及到,我担心性能:

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

Yes, it is best to do this in a single REST call.是的,最好在单个 REST 调用中执行此操作。

By default, SparkPost REST injections are BCC and will send individual emails to each recipient.默认情况下,SparkPost REST 注入是密件抄送,并将向每个收件人发送单独的电子邮件。 As you have seen you can also have the typical "CC" behavior but you would need to set the CC header values with the addresses you want to be seen by others.正如您所见,您也可以拥有典型的“CC”行为,但您需要使用您希望其他人看到的地址设置CC标头值。

So in the example where a CC was included you must have had something like this in the REST call:因此,在包含 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."
  }
}

In your usecase you would not want to set "header_to": "to@thisperson.com" for any of the recipients.在您的用例中,您不希望为任何收件人设置"header_to": "to@thisperson.com"

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

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