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=$campaign->email_html_content;
$htmlcontent = str_ireplace(["[username]","[firstname]","[lastname]"], ["test username",'test firstname','test lastname'], $htmlcontent);
$textcontent=$campaign->email_text_content;
$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])
->setTo($email)
->setSubject($campaign->email_subject)
->send();
if (!$ret) {
Yii::trace(Yii::t('app/model', 'Send email failed after save result: ' . __METHOD__));
}
}
return $ret;
}