Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
messagemail.php
1<?php
10
31
32Loc::loadMessages(__FILE__);
33
39{
41
43 protected $configuration;
44
47
49
53 public function __construct()
54 {
55 $this->configuration = new Message\Configuration();
56 }
57
63 public function getName()
64 {
65 return Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_NAME');
66 }
67
73 public function getCode()
74 {
75 return self::CODE;
76 }
77
83 public function getSupportedTransports()
84 {
85 return array(Transport\Adapter::CODE_MAIL);
86 }
87
92 protected function setConfigurationOptions()
93 {
94 if ($this->configuration->hasOptions())
95 {
96 return;
97 }
98
99 $this->configuration->setArrayOptions(array(
100 array(
101 'type' => 'string',
102 'code' => 'SUBJECT',
103 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_SUBJECT'),
104 'required' => true,
105 'value' => '',
106 'show_in_list' => true,
107 'hint' => array(
108 'menu' => array_map(
109 function ($item)
110 {
111 return array(
112 'id' => '#' . ($item['CODE'] ?? '') . '#',
113 'text' => $item['NAME'] ?? '',
114 'title' => $item['DESC'] ?? '',
115 'items' => isset($item['ITEMS']) ? array_map(
116 function ($item)
117 {
118 return array(
119 'id' => '#' . ($item['CODE'] ?? ''). '#',
120 'text' => $item['NAME'] ?? '',
121 'title' => $item['DESC'] ?? ''
122 );
123 }, $item['ITEMS']
124 ) : []
125 );
126 },
127 array_merge(
128 Helper::getPersonalizeFieldsFromConnectors(),
130 )
131 ),
132 ),
133 ),
134 array(
135 'type' => 'email',
136 'code' => 'EMAIL_FROM',
137 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_EMAIL_FROM'),
138 'required' => true,
139 'value' => '',
140 'show_in_list' => true,
141 'readonly_view' => function($value)
142 {
143 return (new Mail\Address())->set($value)->get();
144 },
145 //'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
146 ),
147 array(
148 'type' => 'mail-editor',
149 'code' => 'MESSAGE',
150 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_MESSAGE'),
151 'required' => true,
152 'templated' => true,
153 'value' => '',
154 'items' => array(),
155 ),
156 array(
157 'type' => 'list',
158 'code' => 'PRIORITY',
159 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_PRIORITY'),
160 'required' => false,
161 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
162 'value' => '',
163 'show_in_list' => true,
164 'items' => array(
165 array('code' => '', 'value' => '(' . Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_NO') . ')'),
166 array('code' => '1 (Highest)', 'value' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_PRIORITY_HIGHEST')),
167 array('code' => '3 (Normal)', 'value' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_PRIORITY_NORMAL')),
168 array('code' => '5 (Lowest)', 'value' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_PRIORITY_LOWEST')),
169 ),
170 'hint' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_PRIORITY_HINT'),
171 ),
172 array(
173 'type' => 'string',
174 'code' => 'LINK_PARAMS',
175 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_LINK_PARAMS'),
176 'required' => false,
177 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
178 'value' => '',
179 'show_in_list' => true,
180 'items' => array(),
181 ),
182 array(
183 'type' => 'file',
184 'code' => 'ATTACHMENT',
185 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_ATTACHMENT'),
186 'required' => false,
187 'multiple' => true,
188 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
189 'value' => '',
190 'items' => array(),
191 ), [
193 'code' => 'TRACK_MAIL',
194 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_TRACK_MAIL'),
195 'hint' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_TRACK_MAIL_HINT'),
196 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
197 'show_in_list' => false,
198 'required' => false,
199 'value' => Option::get('sender', 'track_mails')
200 ],
201 [
202 'type' => Message\ConfigurationOption::TYPE_CONSENT,
203 'code' => 'APPROVE_CONFIRMATION',
204 'name' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_APPROVE_CONFIRMATION'),
205 'hint' => Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_CONFIG_APPROVE_CONFIRMATION_HINT'),
206 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
207 'show_in_list' => false,
208 'required' => false,
209 'value' => Option::get('sender', 'mail_consent'),
210 ], [
211 'type' => Message\ConfigurationOption::TYPE_CONSENT_CONTENT,
212 'code' => 'APPROVE_CONFIRMATION_CONSENT',
213 'group' => Message\ConfigurationOption::GROUP_ADDITIONAL,
214 'show_in_list' => false,
215 'required' => false,
216 'show_preview' => true
217 ],
218 ));
219
220 TimeLimiter::prepareMessageConfiguration($this->configuration);
221
222 $list = array(
223 array(
224 'type' => 'template-type',
225 'code' => 'TEMPLATE_TYPE',
226 'name' => 'Template type',
227 'value' => '',
228 ),
229 array(
230 'type' => 'template-id',
231 'code' => 'TEMPLATE_ID',
232 'name' => 'Template id',
233 'value' => '',
234 ),
235 );
236
237 foreach ($list as $optionData)
238 {
239 $optionData = $optionData + array(
240 'type' => 'string',
241 'name' => '',
242 'internal' => true,
243 );
244 $this->configuration->addOption(new Message\ConfigurationOption($optionData));
245 }
246 }
247
255 public function loadConfiguration($id = null)
256 {
258 Entity\Message::create()
259 ->setCode($this->getCode())
260 ->loadConfiguration($id, $this->configuration);
261
262
263 // do not remove: increment ref count to closure for rewriting.
264 $instance = $this;
265 $this->closureRefCountFix = function () use (&$instance)
266 {
267 return $instance->getMailBody();
268 };
269
270 $trackMail = $this->configuration->getOption('TRACK_MAIL')->getValue();
271 if (is_null($trackMail))
272 {
273 $this->configuration->getOption('TRACK_MAIL')->setValue(Option::get('sender', 'track_mails'));
274 }
275
276 $optionLinkParams = $this->configuration->getOption('LINK_PARAMS');
277 if ($optionLinkParams)
278 {
279 $optionLinkParams->setView(
280 function () use ($id, $optionLinkParams)
281 {
282 ob_start();
283 $GLOBALS['APPLICATION']->IncludeComponent(
284 'bitrix:sender.mail.link.editor', '',
285 array(
286 "INPUT_NAME" => "%INPUT_NAME%",
287 "VALUE" => $optionLinkParams->getValue(),
288 "USE_DEFAULT" => (
289 !$optionLinkParams->getValue()
290 &&
291 !$id
292 &&
293 !Application::getInstance()->getContext()->getRequest()->isPost()
294 ),
295 "PLACEHOLDERS" => array(
296 array(
297 "code" => "campaign",
298 "inputName" => "%INPUT_NAME_SUBJECT%"
299 )
300 ),
301 "DEFAULT_VALUE" => Option::get(
302 'sender',
303 'mail_utm',
304 'utm_source=newsletter&utm_medium=mail&utm_campaign=%campaign%'
305 )
306 )
307 );
308 return ob_get_clean();
309 }
310 );
311 }
312
313 $optionFrom = $this->configuration->getOption('EMAIL_FROM');
314 if ($optionFrom)
315 {
316 $optionFrom->setView(
317 function () use ($optionFrom)
318 {
319 ob_start();
320 $GLOBALS['APPLICATION']->IncludeComponent(
321 'bitrix:sender.mail.sender', '',
322 array(
323 "INPUT_NAME" => "%INPUT_NAME%",
324 "VALUE" => $optionFrom->getValue()
325 )
326 );
327 return ob_get_clean();
328 }
329 );
330 }
331
332 $this->configuration->set('BODY', $this->closureRefCountFix);
333
334 $mailHeaders = array('Precedence' => 'bulk');
335 $mailHeaders = self::fillHeadersByOptionHeaders($mailHeaders);
336 $this->configuration->set('HEADERS', $mailHeaders);
337 TimeLimiter::prepareMessageConfigurationView($this->configuration);
338
340 }
341
349 {
350 $this->configuration = $configuration;
351
352 try
353 {
354 $mailBody = $this->getMailBody();
355 }
356 catch (SystemException $exception)
357 {
358 $result = new Result();
359 $result->addError(new Error($exception->getMessage()));
360
361 return $result;
362 }
363
364 if (Integration\Bitrix24\Service::isCloud())
365 {
366 if ($mailBody && mb_strpos($mailBody, '#UNSUBSCRIBE_LINK#') === false)
367 {
368 $result = new Result();
369 $result->addError(new Error(Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_ERR_NO_UNSUB_LINK')));
370 }
371
372 if (
373 $mailBody
374 && $configuration->getOption('APPROVE_CONFIRMATION')->getValue() === 'Y'
375 && !$configuration->getOption('APPROVE_CONFIRMATION_CONSENT')->getValue()
376 )
377 {
378 $result = new Result();
379 $result->addError(new Error(Loc::getMessage('SENDER_INTEGRATION_MAIL_MESSAGE_ERR_NO_APPROVE_CONFIRMATION_CONSENT')));
380
381 return $result;
382 }
383 }
384 parse_str(
385 $this->configuration->getOption('LINK_PARAMS')->getValue(),
386 $utmTags
387 );
388
389 $utm = [];
390 foreach ($utmTags as $utmTag => $value)
391 {
392 $utm[] = [
393 'CODE' => $utmTag,
394 'VALUE' => $value
395 ];
396 }
397
398 //TODO: compare with allowed email list
399 $emailFrom = $this->configuration->getOption('EMAIL_FROM')->getValue();
400 $emailFrom = (new Mail\Address($emailFrom))->get();
401 $this->configuration->getOption('EMAIL_FROM')->setValue($emailFrom);
402
403 $trackMail = $this->configuration->getOption('TRACK_MAIL')->getValue();
404
405 if (!$trackMail)
406 {
407 $this->configuration->getOption('TRACK_MAIL')->setValue('N');
408 }
409 return Entity\Message::create()
410 ->setCode($this->getCode())
411 ->setUtm($utm)
412 ->saveConfiguration($this->configuration);
413 }
414
421 public function copyConfiguration($id)
422 {
423 return Entity\Message::create()
424 ->setCode($this->getCode())
425 ->copyConfiguration($id);
426 }
427
433 protected function removePhp($html)
434 {
435 static $isCloud = null;
436 if ($isCloud === null)
437 {
438 $isCloud = Integration\Bitrix24\Service::isCloud();
439 Loader::includeModule('fileman');
440 }
441
442 if ($isCloud)
443 {
444 return Block\EditorMail::removePhpFromHtml($html);
445 }
446
447 return $html;
448 }
449
455 public function getMailBody()
456 {
457 Loader::includeModule('fileman');
458
459 $msg = $this->configuration->getOption('MESSAGE')->getValue();
460 $template = $this->getTemplate();
461 if (!$template)
462 {
463 return $this->removePhp($msg);
464 }
465 if (!isset($template['FIELDS']) || !$template['FIELDS']['MESSAGE']['ON_DEMAND'])
466 {
467 return $this->removePhp($msg);
468 }
469
470 $templateHtml = null;
471 if (isset($template['FIELDS']) && isset($template['FIELDS']['MESSAGE']))
472 {
473 $templateHtml = $template['FIELDS']['MESSAGE']['VALUE'];
474 }
475 if (!$templateHtml && isset($template['HTML']))
476 {
477 $templateHtml = $template['HTML'];
478 }
479 if (!$templateHtml)
480 {
481 return $this->removePhp($msg);
482 }
483
484 $document = new Document;
485 $document->loadHTML($templateHtml);
486
487 try
488 {
489 if(!Block\Content\Engine::create($document)->setContent($msg)->fill())
490 {
491 return '';
492 }
493 }
494 catch (SystemException $exception)
495 {
496 throw new Posting\StopException();
497 }
498
499 StyleInliner::inlineDocument($document);
500 $msg = $document->saveHTML();
501 unset($document);
502
503 $msg = $this->removePhp($msg);
504 $msgTmp = Block\Editor::replaceCharset($msg, '#SENDER_MAIL_CHARSET#', true);
505 if ($msgTmp)
506 {
507 $msg = $msgTmp;
508 }
509
510 if (Option::get('sender', 'use_inliner_for_each_template_mail', 'N') != 'Y')
511 {
512 $this->configuration->set('BODY', $msg);
513 }
514
515 return $msg;
516 }
517
522 protected function getTemplate()
523 {
524 if (!$this->configuration->get('TEMPLATE_TYPE') || !$this->configuration->get('TEMPLATE_ID'))
525 {
526 return null;
527 }
528
529 return Templates\Selector::create()
530 ->withMessageCode(static::CODE)
531 ->withTypeId($this->configuration->get('TEMPLATE_TYPE'))
532 ->withId($this->configuration->get('TEMPLATE_ID'))
533 ->get();
534 }
535
541 protected static function fillHeadersByOptionHeaders(array $headers = array())
542 {
543 static $headerList = null;
544 if ($headerList === null)
545 {
546 $headerList = array();
547 // add headers from module options
548 $optionHeaders = Option::get('sender', 'mail_headers', '');
549 $optionHeaders = !empty($optionHeaders) ? unserialize($optionHeaders, ['allowed_classes' => false]) : array();
550 foreach ($optionHeaders as $optionHeader)
551 {
552 $optionHeader = trim($optionHeader);
553 if (!$optionHeader)
554 {
555 continue;
556 }
557
558 $optionHeaderParts = explode(':', $optionHeader);
559 $optionHeaderName = isset($optionHeaderParts[0]) ? $optionHeaderParts[0] : '';
560 $optionHeaderName = trim($optionHeaderName);
561 $optionHeaderValue = isset($optionHeaderParts[1]) ? $optionHeaderParts[1] : '';
562 $optionHeaderValue = trim($optionHeaderValue);
563 if (!$optionHeaderName || !$optionHeaderValue)
564 {
565 continue;
566 }
567
568 $headerList[$optionHeaderName] = $optionHeaderValue;
569 }
570 }
571
572 foreach ($headerList as $optionHeaderName => $optionHeaderValue)
573 {
574 $headers[$optionHeaderName] = $optionHeaderValue;
575 }
576
577 return $headers;
578 }
579}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static fillHeadersByOptionHeaders(array $headers=array())
saveConfiguration(Message\Configuration $configuration)
$GLOBALS['____1444769544']
Definition license.php:1