0

I write a game for football fans. So, I have to send similar mails to a group of people (not completely duplicated e-mail copies). When I send the mails in a cycle - Yii framework sends the mails twice. I suppose - it is because of the static variable Yii::$app. Can someone give me a hint, please. A code for example.

foreach ($oRace->user as $currUser) {
        $htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
        Yii::$app->mailer->compose()
                ->setFrom('info@example.com')
                ->setTo($currUser->mail)
                ->setSubject('Race "' . $raceName . '" has Started')
                ->setHtmlBody($htmlContent)
                ->send();
    }

Thanks all in advance!

My Mailer config.

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'mail.example.eu',
            'username' => 'support@example.com',
            'password' => 'password',
            'port' => '587',
            'encryption' => 'TLS',
        ]
    ],

One more thing. The last mail in the cycle is never duplicated (only the last).

Another failed option. Yii::$app->mailer->sendMultiple($allMails);

koredalin
  • 446
  • 3
  • 11

3 Answers3

2

I recommend you to use CC or BCC option in the email instead of using foreach loop to send emails. I hope this will help someone.

$email = [];

foreach ($oRace->user as $currUser) {
    $email[] = $currUser->mail;
}

$htmlContent = $this->renderPartial('start_race', ['oRace' => $oRace]);
Yii::$app->mailer->compose()
            ->setFrom('info@example.com')
            ->setCc($email) // If you're using Bcc use "setBcc()"
            ->setSubject('Race "' . $raceName . '" has Started')
            ->setHtmlBody($htmlContent)
            ->send();
Shifrin
  • 154
  • 1
  • 11
  • Thank you Shifrin. My case was too complicated. https://stackoverflow.com/questions/29862124/how-to-send-mail-to-multiple-recipients-in-yii2-mailer-or-how-to-add-setcc-in-yi/29916410 You can find more options here. Regards – koredalin Jun 30 '18 at 20:31
  • what if you want to send with a "Dear Username"? – Phemelo Khetho Feb 22 '23 at 16:28
  • @PhemeloKhetho You should use a mail template (view file) located under `@app/mail` by default. Then you can use it in the following way: `Yii::$app->mailer->compose('template-name', ['username' => $username]);` – Shifrin Feb 23 '23 at 07:10
  • @Shifrin Maybe you didn't understand my question properly. My Question is; Is it possible to send an email using the `cc` or `bcc` field while still customizing the salutation to each individual recipient, such as addressing them by their name with 'Dear Recipient Name', or is this not possible with `cc` or `bcc`? – Phemelo Khetho Feb 23 '23 at 12:32
  • @PhemeloKhetho Where do you want to address 'Dear Recipient Name'? In the subject or in the body of the email or elsewhere? – Shifrin Feb 24 '23 at 05:45
  • I found the following code block from the Yii documentation, see whether it can help to solve your problem. $messages = []; foreach ($users as $user) { $messages[] = Yii::$app->mailer->compose() // ... ->setTo($user->email); } Yii::$app->mailer->sendMultiple($messages); – Shifrin Feb 24 '23 at 05:48
1

From the provided code snippets, there are 3 possible reasons for that. Either:

  • $oRace->user contains every user twice
  • $currUser->mail contains the email twice like `email@example.com;email@example.com"
  • something is wrong inside the send function of SwiftMailer
Dimitar Atanasov
  • 141
  • 1
  • 15
1

After all - I have found that the issue was not with my Yii2 framework, but with my hosting mail server. I have used https://github.com/ChangemakerStudios/Papercut for listening what my framework sends. It receives mails on port 25, while it listens for events on port 37804. It's a little bit confusing. Yii2 web.php simple configuration for local mail server is:

$config = [
    'components' => 
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'localhost', // '127.0.0.1'
                'port' => '25',
            ]
        ],
    ],
];

Thank of all, who have read my post!

koredalin
  • 446
  • 3
  • 11