forked from cakephp/plugins.cakephp.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferredEmail.php
More file actions
180 lines (157 loc) · 3.65 KB
/
Copy pathDeferredEmail.php
File metadata and controls
180 lines (157 loc) · 3.65 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace App\Job;
use App\Traits\LogTrait;
use Cake\Core\Configure;
use Cake\Mailer\Email;
use Exception;
use josegonzalez\Queuesadilla\Job\Base;
class DeferredEmail
{
use LogTrait;
/**
* Whether email was sent or not
*
* @var boolean
*/
protected $sent = false;
/**
* True if email was cancelled, false otherwise
*
* @var boolean
*/
protected $canceled = false;
/**
* Test mode for emails
*
* @var boolean
*/
protected $test = false;
/**
* Variables for the view
*
* @var array
*/
public $viewVars = [];
/**
* Email build step
*
* @return void
*/
public function build()
{
if (!defined('FULL_BASE_URL')) {
define('FULL_BASE_URL', Configure::read('App.fullBaseUrl'));
}
}
/**
* Allows an email to be canceled in the build step
*
* @return void
*/
public function cancel()
{
$this->canceled = true;
}
/**
* Allow emails to be sent in a delayed fashion via
* CakeDjjob
*
* Will set the view variables for the current job
*
* @return void
*/
public function perform(Base $job)
{
$this->set($job->data());
return $this->send();
}
/**
* The name of an email profile or an array of initial settings
*
* @return string|array
*/
public function profile()
{
return 'default';
}
/**
* An Email instance
*
* @return \Cake\Mailer\Email
*/
public function emailClass()
{
return new Email();
}
/**
* Send step of email
*
* @return void
*/
public function send()
{
if ($this->sent) {
throw new Exception("This " . get_class($this) . " was already sent");
}
$this->email = $this->emailClass();
$this->email->profile($this->profile());
$this->email->transport($this->transport());
$this->build(); // perform expensive work as late as possible
if ($this->canceled) {
return false;
}
if ($this->test) {
$this->email->to($this->testEmailAddress());
}
try {
if (!empty($this->viewVars)) {
$this->email->viewVars($this->viewVars);
}
$this->sent = $this->email->send();
} catch (Exception $e) {
$this->sent = false;
$this->error($e->getMessage());
}
return $this->sent;
}
/**
* Returns the default test email address
*
* @return string
*/
public function testEmailAddress()
{
return 'mail@example.com';
}
/**
* The name of an email transport
*
* @return string
*/
public function transport()
{
return 'default';
}
/**
* Saves a variable or an associative array of variables for use inside a template.
*
* @param string|array $name A string or an array of data.
* @param string|array|null|bool $value Value in case $name is a string (which then works as the key).
* Unused if $name is an associative array, otherwise serves as the values to $name's keys.
* @return $this
*/
public function set($name, $value = null)
{
if (is_array($name)) {
if (is_array($value)) {
$data = array_combine($name, $value);
} else {
$data = $name;
}
} else {
$data = [$name => $value];
}
$this->viewVars = $data + $this->viewVars;
return $this;
}
}