Jump to content

Sending massive amount of emails


Pain

Recommended Posts

Hello. I have a problem. 

 

I am trying to send approximately 1200 emails at once. Each email has to be retrieved from the database. 

<?php
...
$query = mysql_query("SELECT * FROM mass_subscribers");
while($row = mysql_fetch_assoc($query)) {
   $email = $row['email'];
   $name = $row['name'];
 
   $mail = new SendGrid\Mail();
   $mail->
   setTo($email)->
   setFrom('me@yahoo.com')->
   setSubject($subject)->
   setText($message)->
   setHtml($message);
  
   $sendgrid->web->send($mail);
}
...
?>
 
Problem is the loop gets executed 1200 times which causes 500 error. Is there any way around this? Thank you, any advice appreciated guys.
Link to comment
https://forums.phpfreaks.com/topic/284368-sending-massive-amount-of-emails/
Share on other sites

I did put all the emails into an array. 

$email = array();
$name = array();
 
 
 $query = mysql_query("SELECT * FROM test");
 while($row = mysql_fetch_assoc($query)) {
 
            $name[] = $row['name'];
            $email[] = $row['email'];
 
 
 
         }
 
$email = implode(",", $email);
$name = implode(" ", $name);
This is now the main problem as the email gets sent to the first recipient in the database only.
 
$mail = new SendGrid\Mail();
             $mail->
             addTo($email)->
             setFrom('test@test.com')->
             setSubject($subject)->
             setText($message)->
             setHtml($message);
  
             $sendgrid->web->send($mail); 

PHPs built-in mail () function works properly, but i have to use sendgrid for this one. I'm lost:/

Try to add manually more than one email.

Something like -

<?php
$mail = new SendGrid\Mail();
$mail->
  addTo('foo1@bar.com','foo2@bar.com','foo3@bar.com')->
  setFrom('me@bar.com')->
  setSubject('Subject goes here')->
  setText('Hello World!')->
  setHtml('<strong>Hello World!</strong>');

// send emails
  $sendgrid->
  web->
  send($mail);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.