// a small trick to make a global subscriber object , no worries, we unset it.
Yii::app()->hooks->addAction('console_command_send_campaigns_before_send_to_subscriber', function($campaign, $subscriber, $customer, $server, $emailParams){
if (!MW_IS_CLI) {
return;
}
Yii::app()->params['__tmpSubscriberObj__' . $campaign->campaign_id .'__'. $subscriber->email] = $subscriber;
});
Yii::app()->hooks->addFilter('delivery_server_before_send_email', function($params, $server){
if (!MW_IS_CLI || !($campaign = $server->getDeliveryObject()) || !is_object($campaign) || !($campaign instanceof Campaign)) {
return $params;
}
if (!isset($params['to'])) {
return $params;
}
list($toEmail) = $server->getMailer()->findEmailAndName($params['to']);
$key = '__tmpSubscriberObj__' . $campaign->campaign_id .'__'. $toEmail;
$subscriber = isset(Yii::app()->params[$key]) ? Yii::app()->params[$key] : null;
if (empty($subscriber)) {
return $params;
}
unset(Yii::app()->params[$key]);
$fromEmail = $campaign->from_email;
$searchReplace = CampaignHelper::getCommonTagsSearchReplace($fromEmail, $campaign, $subscriber);
$fromEmail = str_replace(array_keys($searchReplace), array_values($searchReplace), $fromEmail);
$params['from'] = array($fromEmail => $campaign->from_name);
return $params;
});