EmailCommand.class.php
2.72 KB
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
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Created by PhpStorm.
* User: mnr
* Date: 06.11.14
* Time: 11:13
*/
namespace Cpeople\Classes\Forms\Commands;
use Cpeople\Classes\Forms\Command;
use Cpeople\Classes\Forms\Form;
class EmailCommand extends Command
{
protected $mailer;
protected $to;
protected $from;
protected $subject;
protected $body_template;
protected $data;
protected $files;
/**
* @param $isCritical
* @param $body
* @param array $to
* @param array $from
* @param null $subject
* @param array $files
*
* TODO: https://php.net/manual/ru/function.array-replace.php - передавать опции массивом, использовать array_replace для дефолтных значений
* TODO: передавать объект PHPMailer, а не хардкодить путь к нему
*/
public function __construct($isCritical, \PHPMailer $phpMailer, $options)
{
parent::__construct($isCritical);
$this->mailer = $phpMailer;
$defaultOptions = array(
'to' => array(cp_get_site_email()),
'from' => array("noreply@{$_SERVER['HTTP_HOST']}"),
'subject' => 'Сообщение на ' . $_SERVER['HTTP_HOST'],
'body' => '',
'files' => array()
);
$options = array_replace($defaultOptions, $options);
$this->to = $options['to'];
$this->from = $options['from'];
$this->subject = $options['subject'];
$this->body_template = $options['body'];
$this->files = $options['files'];
}
public function execute(Form $form)
{
$this->data = array_change_key_case($form->getData(), CASE_UPPER);
$body = preg_replace_callback('#{([A-Z_]+)}#i', array($this, 'replaceCallback'), $this->body_template);
$this->mailer->CharSet = 'UTF-8';
$this->mailer->setFrom($this->from[0], $this->from[1]);
$this->mailer->addAddress($this->to[0], $this->to[1]);
$this->mailer->Subject = $this->subject;
$this->mailer->msgHTML($body);
foreach($this->files as $file)
{
if(file_exists($file) && is_readable($file))
{
$this->mailer->addAttachment($file);
}
}
$result = $this->mailer->send();
if(!$result && $this->isCritical)
{
throw new \Exception('Email::Send false');
}
elseif(!$result && !$this->isCritical)
{
$form->setErrors(array($this->getErrorMessage($this->mailer->ErrorInfo)));
}
}
private function replaceCallback($match)
{
return isset($this->data[strtoupper($match[1])]) ? $this->data[strtoupper($match[1])] : '';
}
}