Jump to content

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.
Edited by Pain
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:/

Edited by Pain

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);

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.