- Добавлен метод выполняющий отправку письма после выполнения подписки

parent 9fd0191f
...@@ -595,5 +595,56 @@ class User extends \common\components\ActiveRecordModel implements IdentityInter ...@@ -595,5 +595,56 @@ class User extends \common\components\ActiveRecordModel implements IdentityInter
} }
} }
} }
/**
* @param $template_id
*/
public function afterSubscribe($template_id, $params){
/** @var \DateTime $time_now */
$time_now=new \DateTime();
/** @var MessageTemplate $templateModel */
$templateModel = MessageTemplate::findOne($template_id);
/** @var Templates $template */
$template = new Templates($template_id, $params);
$email = $this->email;
$sender = new UnisenderAPI();
// Create the send list
$newList = $sender->createList();
$newListObject=Json::decode($newList);
if (array_key_exists('result', $newListObject) && array_key_exists('id', $newListObject['result'])) {
$newListId=$newListObject['result']['id'];
// Subscribe user to new List
$sender->subscribe(['list_ids' => $newListId, 'fields[email]' => $email, 'double_optin' => 1]);
// Create new message
$newMessage=$sender->createEmailMessage($this->name, $email, $templateModel->subject, $template->getTemplate(), $newListId);
// Decode result
$newMessageObject=Json::decode($newMessage);
if (array_key_exists('result', $newMessageObject) && array_key_exists('message_id', $newMessageObject['result'])) {
// Get the message ID
$newMessageId=$newMessageObject['result']['message_id'];
// Create new campaign
$newCampaign = $sender->createCampaign($newMessageId);
// Parse the result
$newCampaignObject = Json::decode($newCampaign);
if (array_key_exists('result', $newCampaignObject) && array_key_exists('campaign_id', $newCampaignObject['result'])) {
$newCampaignId = $newCampaignObject['result']['campaign_id'];
$schedule = new TriggerSchedule();
$schedule->sended=1;
$schedule->checked=0;
$schedule->message_id=$newMessageId;
$schedule->message=$template->getTemplate();
$schedule->email=$email;
$schedule->time=$time_now->format('Y-m-d H:i:s');
$schedule->date_create=$time_now->format('Y-m-d H:i:s');
$schedule->list_id=$newListId;
$schedule->campaign_id=$newCampaignId;
if (!$schedule->save()) {
echo 'Письмо не было отправлено';
}
}
}
}
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment