【问题标题】:Why it takes so long to send multiple emails with Sendgrid?为什么用 Sendgrid 发送多封电子邮件需要这么长时间?
【发布时间】:2014-01-11 10:15:22
【问题描述】:

我有一个体育博彩提示网站,在我发布的每个提示中,我都会使用 SendGrid 向每个用户(总共约 700 个用户)发送一封电子邮件。问题在于交货时间。电子邮件从发送后甚至延迟了半个小时。 有谁知道为什么以及如何解决它?

我使用 SMTP 发送它。 这是我的一些代码:

            $catre = array();
            $subiect = $mailPronostic['subiect'];
            $titlu = $mailPronostic['titlu'];
            $text = $mailPronostic['text'];
            $data = new DateTime($this->_dataPronostic);

            foreach($users as $user){
                array_push($catre, $user->_emailUser);
            }

            $data = urlencode($data->format("d-m-Y H:i"));
            $echipe = urlencode($this->_gazdaPronostic." vs ".$this->_oaspetePronostic);
            $pronostic = urlencode($predictii[$this->_textPronostic]);
            $cota = $this->_cotaPronostic;
            $mesaj = file_get_contents("http://plivetips.com/mailFiles/mailPronostic.php?text=".urlencode($text)."&titlu=".urlencode($titlu)."&data=$data&echipe=$echipe&pronostic=$pronostic&cota=$cota");
            //return mail(null, $subiect, $mesaj, $header);

            $from = array('staff@plivetips.com' => 'PLIVEtips');
            $to = $catre;
            $subject = "PLIVEtips Tip";

            $username = 'user';
            $password = 'pass';

            $transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 587);
            $transport->setUsername($username);
            $transport->setPassword($password);
            $swift = Swift_Mailer::newInstance($transport);
            $message = new Swift_Message($subject);

            $message->setFrom($from);
            $message->setBody($mesaj, 'text/html');

            $numSent = 0;
            foreach ($to as $address => $name)
            {
                if (is_int($address)) {
                    $message->setTo($name);
                } else {
                    $message->setTo(array($address => $name));
                }

                $numSent += $swift->send($message, $failures);
            }

谢谢。

【问题讨论】:

    标签: php email smtp swiftmailer sendgrid


    【解决方案1】:

    脚本运行需要多长时间?我感觉剧本要花很长时间。如果是这种情况,我认为是因为您正在为每条消息打开一个连接。

    使用 SendGrid,您可以使用 X-SMTPAPI header 定义收件人,通过一个连接将消息发送给 1000 个收件人。最简单的方法是使用官方的sendgrid-php 库并使用setTos method

    添加到 X-SMTPAPI 标头的每个收件人都会收到一封唯一的电子邮件。基本上,SendGrid 的服务器将执行邮件合并。您的电子邮件内容看起来并不因每个用户而异,但如果确实如此,那么您可以在标题中使用 substitution tags 来指定每个收件人的自定义数据。

    如果你不想使用sendgrid-php,你可以在this example中查看如何构建header。

    【讨论】:

    • 我曾尝试使用 sendgrid 库,但遇到了一些问题(不知道是什么问题)。我会再试一次,如果我仍然有问题,我会回来回答。谢谢。
    • 谢谢。抱歉延迟:)
    猜你喜欢
    • 2015-05-09
    • 2013-10-27
    • 2012-09-07
    • 2010-12-17
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多