I am trying to send emails by swiftmailer in foreach. But it always sends only to first email addressee times number of users. (say, there are 3 users, it will send 3 mails to first user). I have checked that every iteration of sendmail have different email address, that address is in $email instance ($email->getTo()) and as long as it cannot send email (ie. on localhost) it works fine. It creates 3 emails, with 3 different addresses. But when it actually can send it via SMTP it will send all mails to same person with same text rendered.
I am thinking that issue is with $app->mailer->compose, but i am not really sure. (seems like that instance is holding its data and it cannot be rewritten, till app resets)
Controller:
foreach ($invitations as $invitation_id) {
$model = Invitation::findOne($invitation_id);
if (!empty($model)) {
if (!$model->sendMail('Test mail')) {
$errors[] = $model->userName;
}
} else {
$errors[] = "Non existing $invitation_id?";
}
}
Model Invitation:
public function sendMail ($text){
$user = User::findOne($this->user_id);
$email = Yii::$app->mailer->compose('layouts/bulk-email', ['user' => $user, 'text' => $text])
->setFrom('test@test.com')
->setTo($user['mail'])
->setSubject('Test subject');
return $email->send();
}
I tried using the sendMultiple method (creating messages first to array, and then send them all at once), but instead they were all send 6 times to first address.
Is there something obvious i am missing?
Actual issue was that localhost is not caching ldap answers, but the dev server is. So the issue was actually what Muhammad Omer Aslam somewhat suggested :)