SendGrid:将电子邮件发送给多个收件人,而其他电子邮件不会显示在 "to" 字段中

SendGrid: Sending an email to multiple recipients without other emails being shown on the "to" field

我想向多个收件人发送一封电子邮件。

我用了personalizations,但是每个人的邮箱都出现在"to"字段,侵犯了他们的隐私。

我不想使用 BCC,因为这通常会直接变成垃圾邮件(例如 http://www.standss.com/blog/index.php/why-you-should-avoid-using-bcc-for-emails/)。

因此我的问题是,如何向多个收件人发送一封电子邮件,而不让每个人的电子邮件都出现在 "to" 字段中。

我能看到的唯一选择是使用循环向 API 发送单独的请求,当我有很多电子邮件要发送时,这是非常耗费资源和时间的循环!

将 SendGrid 的个性化设置与多个收件人组一起使用时,您需要定义 multiple 1st-level objects within the Personalization array

所以代替:

{"personalizations": [
{"to": [
    {"email": "recipient1@example.com"},
    {"email": "recipient2@example.com"}
]}]}

这将是一个共同的To:数组,可以互相看到,

你想要:

{"personalizations": [
{"to": [{"email": "recipient1@example.com"}]},
{"to": [{"email": "recipient2@example.com"}]}
]}

在每个个性化级别中,您可以自定义内容、主题、替换标签等几乎所有内容。

因此您可以构建完整的个性化设置,并迭代这 1000 次。拥有 1000 个收件人后,将他们捆绑到一个 API 调用中,然后发送。

在@jacobmovingfwd 的基础上,这里有一个 Python 中的示例,它向具有个性化 "to" 字段的多个收件人发送相同的电子邮件。我已经测试了代码,它对我有用。

# Given a list of email addresses that are strings
sublist = [...]    

mail = Mail()

for to_email in sublist:
    # Create new instance for each email
    personalization = Personalization()
    # Add email addresses to personalization instance
    personalization.add_to(Email(to_email))
    # Add personalization instance to Mail object
    mail.add_personalization(personalization)

# Add data that is common to all personalizations
mail.from_email = Email(from_email)
mail.subject = subject
mail.add_content(Content('text/plain', message_txt))
mail.add_content(Content('text/html', message_html))

# Send
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
response = sg.client.mail.send.post(request_body=mail.get())

这是一个 C# 版本,它为要单独邮寄的每个收件人克隆个性化,但仍然使用单个 SendGrid API 调用:

    public static void SendEachReceipient(SendGridMessage msg, IEnumerable<string> recipients)
    {
        if (msg == null || recipients == null || !recipients.Any())
            return;

        if (msg.Personalizations == null) //can easily be null if no substitutions have not been added
            msg.Personalizations = new List<Personalization>();

        var substitutionsCopy = msg.Personalizations.FirstOrDefault()?.Substitutions; //all substitutions (if any) are always all contained in the first personalization
        msg.Personalizations.Clear(); //we will start fresh - one personalization per each receipient to keep emails private from each other

        foreach (var email in recipients.Where(x => !string.IsNullOrEmpty(x)).Distinct())
        {
            var personalization = new Personalization();
            personalization.Substitutions = substitutionsCopy;
            personalization.Tos = new List<EmailAddress>() { new EmailAddress(email) };
            msg.Personalizations.Add(personalization);
        }

        var result = new SendGridClient("api-key").SendEmailAsync(msg).Result;
    }

您需要准备以下类型的 JSON

{
"personalizations": [
    {
        "to": [
            {
                "email": "mail here",
                "name": "name here"
            }
        ],
        "subject": "subject for individual person"
    },
    {
        "to": [
            {
                "email": "mail here",
                "name": "name here"
            }
        ],
        "subject": "if you want to send a dynamic subject then write here",
    }
],
"from": {
    "email": "mail here",
    "name": "name here"
},
"reply_to": {
    "email": "mail here",
    "name": "name here"
},
"subject": "subject here",
"content": [
    {
        "type": "text/html",
        "value": "<p>Hello from Twilio first!</p>"
    }
],
"attachments": [
    {
        "content": "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KCiAgICA8aGVhZD4KICAgICAgICA8bWV0YSBjaGFyc2V0PSJVVEYtOCI+CiAgICAgICAgPG1ldGEgaHR0cC1lcXVpdj0iWC1VQS1Db21wYXRpYmxlIiBjb250ZW50PSJJRT1lZGdlIj4KICAgICAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFsLXNjYWxlPTEuMCI+CiAgICAgICAgPHRpdGxlPkRvY3VtZW50PC90aXRsZT4KICAgIDwvaGVhZD4KCiAgICA8Ym9keT4KCiAgICA8L2JvZHk+Cgo8L2h0bWw+Cg==",
        "filename": "index.html",
        "type": "text/html",
        "disposition": "attachment"
    }
] 
}