How to send bulk emails to user using swiftmailer?

i am using yii2 and swiftmailer to send job titles to subscriber of my website. i send every 24 hours or every 2 days…based on the new jobs posted in my website.
i have 300 subscribers, my hosting allow me to send 500 emails per a single hour.
here is my code
// the following code works fine, becuase the number of users between codeno 20…65= is 45
my students codeno starts with 1…
but if the number of students is beyond 45, it gives error… sending error", so solve this, i jus create multiple pages, but i want to solve this problem so that i can send to all my students.
if i make the following code, it will showing me sending error
/* ->andWhere([’>=’,‘codeno’, 10])
->andWhere([’<=’,‘codeno’, 100])
*/
public function sendJobEmail($email)
{

$students = Student::find()
->andWhere([’>=’,‘codeno’, 20])
->andWhere([’<=’,‘codeno’, 65])
->all();
try
{

foreach( $students as $student)

{
$body1 = Yii::$app->view->renderFile(’@common/mail/layouts/jobs.php’,
Yii::$app->mailer->compose()
->setTo($student->email)

->setFrom(array(‘info@mywebsite.com’ => ‘mywebsite.com’))
->setSubject(‘New Jobs posted’)
->setHtmlBody($body1)
->send();

          {
                    throw new \Exception('Error sending the email to '.$student->email);
                }
            }
        }
        return true; 

}
return 1;

     }

The function setTo can accepts an array of recipients, same content only needs to be sent once.

Yii::$app->mailer->compose()
  ->setTo(array_columns($students, "email"))
  ...;

If it doesn’t works, please provide more detail of your error.

Btw, if you need to send mail with different content, it is better to set up a queue(such as yii2-queue), and then push mail job to queue.

To send bulk emails by Swift Mailer,

  • configure SMTP transport,
  • create the email with the sender’s details and content.
  • iterate through recipients’ list,
  • setting each recipient personally and sending the email alone.

Ensure to manage errors and stick to SMTP server limits for successful delivery.

You can also use libraries including PHPMailer, Zend\Mail, or you can use other third-party services, such as Mailgun, iDealSMTP, or Amazon SES, which are best for sending bulk emails reliably and effectively.