koredalin
koredalin

Reputation: 445

Yii2 SwiftMailer sends mails twice

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('[email protected]')
                ->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' => '[email protected]',
            '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);

Upvotes: 0

Views: 986

Answers (3)

Shifrin
Shifrin

Reputation: 154

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('[email protected]')
            ->setCc($email) // If you're using Bcc use "setBcc()"
            ->setSubject('Race "' . $raceName . '" has Started')
            ->setHtmlBody($htmlContent)
            ->send();

Upvotes: 2

koredalin
koredalin

Reputation: 445

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!

Upvotes: 1

Dimitar Atanasov
Dimitar Atanasov

Reputation: 169

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 protected];[email protected]"
  • something is wrong inside the send function of SwiftMailer

Upvotes: 1

Related Questions