【问题标题】:PHP - huge array and looping statementPHP - 巨大的数组和循环语句
【发布时间】:2011-09-23 22:56:20
【问题描述】:

我有一个旧的客户电子邮件列表(大约 1100 封电子邮件),我想给他们都发电子邮件。 此列表位于一个巨大的 php 数组中。

我正在使用 sendgrid,他们在这里有一个不错的 php howto:http://sendgrid.com/documentation/display/api/SMTPPHPExample

我正在使用他们的代码,并且在一个部分中他们有:

$toList = array('destination1@example.com', 'destination2@example.com');

现在,为了不溢出 sendgrids 服务器或陷入垃圾邮件过滤器,我想一次只发送 100 封电子邮件,如何从我的大阵列中抓取 100 封电子邮件,将它们放入此阵列,运行sendgrid 登录,发送电子邮件,然后返回,抓取下一个 100 并再次运行循环。

我不是太精通php,所以我不知道如何设置这个循环。

非常感谢您的帮助!

【问题讨论】:

  • 我很确定 SendGrid 在他们的服务中内置了这种类型的导入和限制。你为什么自己做
  • 它只有 1000,为什么不从 1 循环到 1000?
  • 不要为此使用To:,甚至不要使用CC:。我相信您的客户不会喜欢他们的电子邮件地址出现在每个 ELSE 的电子邮件中。如果您必须分批发送,请改用BCC: 隐藏每个人的地址。否则,每个收件人只发送一封电子邮件。
  • 该列表实际上是大约 1100 人(刚刚编辑了原始问题),sendgrid 还希望您在 1000 个字符处换行,所以我猜坚持所有 1000 封电子邮件会打破这一点。马克,你知道怎么改成密件抄送吗?

标签: php arrays email loops sendgrid


【解决方案1】:

我想你说的是array array_chunk ( array $input , int $size[, bool $preserve_keys = false ] )。它根据 $size 中的值将一个数组拆分为多个较小的数组。

【讨论】:

    【解决方案2】:
    > Now, to not overflow sendgrids servers
    > or get caught in spam filters I would
    > like to only send batches of 100
    > emails at a time, how can I grab 100
    > emails from my big array, put them
    > into this array, run through the
    > sendgrid login, send email, then go
    > back, grab the next 100 and run the
    > loop again.
    

    就像其他用户在 cmets 中所说的那样,我认为您不应该担心 sendgrid 溢出,因为他们使用消息队列发送电子邮件或不会杀死那里的服务器,因为您不是唯一使用 sendgrid 的用户。根据 Sendgrid 网站,他们有一些非常大的网站,例如 Slideshare/Hootsuite/Get Satisfaction/FourSquare,它们发送了大量电子邮件。


    在你说的cmets中:

    > The list is actually about 1100 people
    > (just edited the original question),
    > also sendgrid wants you to break the
    > lines at 1000 characters, so i'm
    > guessing sticking all 1000 emails will
    > break that. Marc, do you know how to
    > change it to BCC?
    

    根据smtp best practices

    您必须确保标题是 折叠到 1,000 以下的行长 人物。不这样做可以 导致中间 MTA 分裂 在非空间上为您提供标题 边界,这将导致空间 插入到最终结果中 电子邮件。

    我假设 PHP 库会自动为您执行此操作,您甚至不必担心这一点。如果您担心,您应该发送电子邮件至support@sendgrid.com 询问:)??


    但如果你真的想拆分,你应该阅读cwallenpoole's answer,我认为这很不错。但是在拆分数组后,您还应该sleep 然后进行节流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2019-05-02
      • 2016-03-14
      • 2023-03-30
      • 2020-11-21
      相关资源
      最近更新 更多