Question

I have a sport betting tips website and on every tip that i publish i send an email to every user (about 700 users in total) with SendGrid. The problem comes with the delivery time. The email is delayed even half an hour from the time of send. Does anyone know why and how could i fix it?

I am sending it with SMTP. Here is some of my code:

            $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.

Was it helpful?

Solution

how long does the script take to run? I have a feeling the script is taking a long time. If that's the case, I think it is because you are opening a connection for each message.

With SendGrid, you can send a message to 1000 recipients with one connection by using the X-SMTPAPI header to define the recipients. The easiest way to do this to use the official sendgrid-php library and use the setTos method.

Recipients added to the X-SMTPAPI header will each be sent a unique email. Basically SendGrid's servers will perform a mail merge. It doesn't look like your email content varies with each user, but if it does, then you may use substitution tags in the header to specify custom data per recipient.

If you don't want to use sendgrid-php, you can see how to build the header in this example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top