-
Notifications
You must be signed in to change notification settings - Fork 7
/
mail.php
85 lines (80 loc) · 2.54 KB
/
mail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
define('MODX_API_MODE', true);
require_once($_SERVER['DOCUMENT_ROOT'].'/index.php');
$modx = new modX();
$modx->initialize('web');
$method = $_SERVER['REQUEST_METHOD'];
if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest') {
$modx->sendRedirect($modx->makeUrl($modx->getOption('site_start'),'','','full'));
}
/**
* @var $pdo pdoTools
*/
$pdo = $modx->getService('pdoTools');
$project_name = $modx->getOption('site_name');
$form_subject = trim($_POST["form_subject"]);
$from = $modx->getOption('emailsender');
$to = $modx->getOption('ms2_email_manager');
$to = str_replace(' ', '', $to);
$to = explode(',',$to);
$name = htmlspecialchars(trim($_POST["Имя"]));
$phone = htmlspecialchars(trim($_POST["Телефон"]));
$props = array(
'name' => $name,
'phone' => $phone,
'createdon' => date('Y-m-d H:i:s'),
'manager' => 1,
'status' => 1,
);
if ($form_subject) {
$props['data']['Форма'] = $form_subject;
}
if ($_POST['upload_file']) {
$props['data']['Прикрепленный файл'] = '<a href="'.$_POST['upload_file'].'" target="_blank">ссылка на файл</a>';
}
if ($name) {
$msg['Имя'] = $name;
}
if ($phone) {
$msg['Телефон'] = $phone;
}
foreach ($_POST as $key => $value) {
if (strpos($key, 'rec_') !== false) {
$key = str_replace('rec_', '', $key);
$msg[$key] = htmlspecialchars(trim($value));
$props['data'][$key] = htmlspecialchars(trim($value));
}
}
addAdmin($props);
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_FROM, $from);
$modx->mail->set(modMail::MAIL_FROM_NAME, $project_name);
foreach ($to as $item) {
$modx->mail->address('to', $item);
}
$modx->mail->set(modMail::MAIL_SUBJECT, $form_subject);
if ($_POST['upload_file']) {
$modx->mail->attach($_SESSION['form_file_path']);
}
$modx->mail->set(modMail::MAIL_BODY, $pdo->getChunk('FormMailSent', array(
'data' => $msg
)));
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
function addAdmin($props) {
global $modx;
if (!is_array($props)) {
$modx->log(1, 'Не получены параметры');
return ;
}
if (!$callBack = $modx->getService('callback', 'callBack', $modx->getOption('callback_core_path', null,
$modx->getOption('core_path') . 'components/callback/') . 'model/callback/')) {
$modx->log(1, 'Не загружена модель CallBack');
return ;
}
$new = $modx->newObject('callBackItem', $props);
$new->save();
}