Sending doubled emails, can anyone help please?

Can someone help me find out why this code is sending 2 emails instead of one? I’ve been trying to figure out but either I’m too overworked already or my skills have me blind to the (probably) obvious.

Here’s the Controller:

public function actionEmailTest() {

        Yii::$app->response->format = Response::FORMAT_JSON;

        $campaign_id = Yii::$app->getRequest()->post('campaign_id');

        $receiver_email = Yii::$app->getRequest()->post('receiver_email');

        $model = $this->findModel($campaign_id);

        if ($model) {

            \app\helpers\EmailSender::sendTestEmail($model, explode("\n", $receiver_email));

            return ['success' => true, 'message' => Yii::t('app', 'Email sent to test receiver')];

        } else {

            return ['success' => true, 'message' => Yii::t('app', 'Invalid campaign')];



Here’s \app\helpers\EmailSender:sendTestEmail

public static function sendTestEmail($campaign, $emailList) {

        foreach ($emailList as $email) {

            $email=  trim($email);

            $mailer = static::getMailer($campaign->smtpserver_id);

            $mailer->htmlLayout = false;

            $mailer->textLayout = false;



            $htmlcontent = str_ireplace(["[username]","[firstname]","[lastname]"], ["test username",'test firstname','test lastname'], $htmlcontent);




            $textcontent = str_ireplace(["[username]","[firstname]","[lastname]"], ["test username",'test firstname','test lastname'], $textcontent);


            $ret = $mailer->compose(['html' => 'contact/email', 'text' => 'contact/text'], ['html_content' => $htmlcontent, 'text_content' => $textcontent])

                    ->setFrom([$campaign->email_from_address => $campaign->email_from_name])




            if (!$ret) {

                Yii::trace(Yii::t('app/model', 'Send email failed after save result: ' . __METHOD__));



        return $ret;



I cannot see that you are sending the email twice in the Code.

That should be okay.

I can imagine that you have 2 receivers with same email-address? ;)

You do:

// in controller 

\app\helpers\EmailSender::sendTestEmail($model, explode("\n", $receiver_email));

// in your helper

public static function sendTestEmail($campaign, $emailList) {

        foreach ($emailList as $email) { // HERE YOU LOOP!

You could for example give out $emailList to see if it contains more than one address.


That’s what I don’t understand, I add a group of email addressed to send emails to and all of them get dupplicated.

What do you mean by "give out $emailList"?


With giving out "$emailList" I mean: Check the content of $mailList.

Verify it has correct values…

So you can be sure that the email-addresses are NOT twice in your $mailList array.

For example with:


Another possible reason for your problem could be:

Somewhere else in your code you execute "actionEmailTest" twice or in a loop.


Thank you!

Will take a deeper look to see if I’m lucky