为什么用Sendgrid发送多封电子邮件需要这么长时间?

时间:2014-01-11 10:15:23

标签: php email smtp swiftmailer sendgrid

我有一个体育博彩技巧网站,在我发布的每个提示上,我都会使用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);
            }

THX。

1 个答案:

答案 0 :(得分:3)

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

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

添加到X-SMTPAPI标头的收件人将分别发送一封唯一的电子邮件。基本上SendGrid的服务器将执行邮件合并。您的电子邮件内容看起来与每个用户不同,但如果有,则您可以在标题中使用substitution tags为每个收件人指定自定义数据。

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

相关问题