-
Notifications
You must be signed in to change notification settings - Fork 137
/
QueueEmailTask.php
152 lines (133 loc) · 3.43 KB
/
QueueEmailTask.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Queue\Shell\Task;
use Cake\Log\Log;
use Cake\Mailer\Email;
use Exception;
/**
* @author Mark Scherer
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class QueueEmailTask extends QueueTask {
/**
* List of default variables for EmailComponent
*
* @var array
*/
public $defaults = [
'to' => null,
'from' => null,
];
/**
* @var int
*/
public $timeout = 120;
/**
* @var int
*/
public $retries = 1;
/**
* @var \Cake\Mailer\Email
*/
public $Email;
/**
* "Add" the task, not possible for QueueEmailTask
*
* @return void
*/
public function add() {
$this->err('Queue Email Task cannot be added via Console.');
$this->out('Please use createJob() on the QueuedTask Model to create a Proper Email Task.');
$this->out('The Data Array should look something like this:');
$this->out(var_export([
'settings' => [
'to' => 'email@example.com',
'subject' => 'Email Subject',
'from' => 'system@example.com',
'template' => 'sometemplate',
],
'vars' => [
'content' => 'hello world',
],
], true));
$this->out('Alternativly, you can pass the whole EmailLib to directly use it.');
}
/**
* @param mixed $data Job data
* @param int|null $id The id of the QueuedTask
* @return bool Success
*/
public function run(array $data, $id) {
if (!isset($data['settings'])) {
$this->err('Queue Email task called without settings data.');
return false;
}
/* @var \Cake\Mailer\Email $email */
$email = $data['settings'];
if (is_object($email) && $email instanceof Email) {
try {
$transportClassNames = $email->configuredTransport();
$result = $email->transport($transportClassNames[0])->send();
if (!isset($config['log']) || !empty($config['logTrace']) && $config['logTrace'] === true) {
$config['log'] = 'email_trace';
} elseif (!empty($config['logTrace'])) {
$config['log'] = $config['logTrace'];
}
if (isset($config['logTrace']) && !$config['logTrace']) {
$config['log'] = false;
}
if (!empty($config['logTrace'])) {
$this->_log($result, $config['log']);
}
return (bool)$result;
} catch (Exception $e) {
$error = $e->getMessage();
$error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL . $e->getTraceAsString();
Log::write('email_error', $error);
return false;
}
}
$class = 'Tools\Mailer\Email';
if (!class_exists($class)) {
$class = 'Cake\Mailer\Email';
}
$this->Email = new $class();
$settings = array_merge($this->defaults, $data['settings']);
foreach ($settings as $method => $setting) {
call_user_func_array([$this->Email, $method], (array)$setting);
}
$message = null;
if (!empty($data['vars'])) {
if (isset($data['vars']['content'])) {
$message = $data['vars']['content'];
}
$this->Email->viewVars($data['vars']);
}
return (bool)$this->Email->send($message);
}
/**
* Log message
*
* @param array $contents log-data
* @param mixed $log int for loglevel, array for merge with log-data
* @return void
*/
protected function _log($contents, $log) {
$config = [
'level' => LOG_DEBUG,
'scope' => 'email',
];
if ($log !== true) {
if (!is_array($log)) {
$log = ['level' => $log];
}
$config = array_merge($config, $log);
}
/* for now
Log::write(
$config['level'],
PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'],
$config['scope']
);
*/
}
}