3

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

我使用了个性化,但每个人的电子邮件都出现在“收件人”字段中,这侵犯了他们的隐私。

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

因此,我的问题是,如何在没有每个人的电子邮件出现在“收件人”字段中的情况下向多个收件人发送电子邮件。

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

4

3 回答 3

13

将 SendGrid 的 Personalizations 用于多个收件人组时,您需要在 Personalization 数组中定义多个 1st-level 对象

所以而不是:

{"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 调用中,然后发送。

于 2017-05-09T19:09:58.867 回答
3

这是一个 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;
    }
于 2018-06-08T18:52:25.527 回答
1

为了以@jacobmovingfwd 为基础,这里有一个 Python 示例,它使用个性化的“收件人”字段将同一封电子邮件发送给多个收件人。我已经测试了代码,它对我有用。

# 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())
于 2018-06-02T13:10:28.317 回答