AddEventHandler('im', "OnAfterNotifyAdd", "OnAfterImHandler");
function OnAfterImHandler($id, array $message) {
// Do not use SITE_ID
// @see http://devbitrix.ru/bitrix/refinements/ne-ispolzuyte-konstantu-site_id-pri-sozdanii-pochtovyih-sobyitiy-v-funktsiyah-agentah/
$siteId = CSite::GetByID(SITE_ID)->fetch()['ID'];
// Create email event type
(new CEventType)->Add([
'EVENT_NAME' => 'EMAIL_TEST',
'NAME' => 'Тестим мыло',
'LID' => 'ru',
'DESCRIPTION' => 'Foobar'
]);
$reciever = CUser::GetByID($message['TO_USER_ID'])->Fetch();
// Create email template
(new CEventMessage)->Add([
'ACTIVE' => 'Y',
'EVENT_NAME' => 'EMAIL_TEST',
'SITE_ID' => $siteId,
'EMAIL_FROM' => '',
'EMAIL_TO' => $reciever['EMAIL'],
'SUBJECT' => 'Тестим мыло (тема)',
'BODY_TYPE' => 'text',
'MESSAGE' => $message['MESSAGE_OUT'],
]);
// Send message to email query
CEvent::Send('EMAIL_TEST', $siteId, $message);
}