1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Borut Balažek <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Mailer |
11
|
|
|
{ |
12
|
|
|
protected $app; |
13
|
|
|
|
14
|
|
|
protected $swiftMessageInstance; |
15
|
|
|
protected $swiftMessageInstanceTemplate; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Silex\Application $app |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Application $app) |
21
|
|
|
{ |
22
|
|
|
$this->app = $app; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Prepares the (swift) email and sends it. |
27
|
|
|
* |
28
|
|
|
* @return integer |
29
|
|
|
*/ |
30
|
|
|
public function swiftMessageInitializeAndSend(array $data = array()) |
31
|
|
|
{ |
32
|
|
|
$swiftMessageInstance = \Swift_Message::newInstance(); |
33
|
|
|
|
34
|
|
|
if (!isset($data['subject'])) { |
35
|
|
|
throw new Exception('You need to specify a subject'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (!isset($data['to'])) { |
39
|
|
|
throw new Exception('You need to specify a recipient'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$from = isset($data['from']) |
43
|
|
|
? $data['from'] |
44
|
|
|
: array($this->app['email'] => $this->app['emailName']) |
45
|
|
|
; |
46
|
|
|
$to = $data['to']; |
47
|
|
|
|
48
|
|
|
$swiftMessageInstance |
49
|
|
|
->setSubject($data['subject']) |
50
|
|
|
->setTo($to) |
51
|
|
|
->setFrom($from) |
52
|
|
|
; |
53
|
|
|
|
54
|
|
|
if (isset($data['cc'])) { |
55
|
|
|
$swiftMessageInstance->setCc($data['cc']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($data['bcc'])) { |
59
|
|
|
$swiftMessageInstance->setBcc($data['bcc']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$templateData = array( |
63
|
|
|
'app' => $this->app, |
64
|
|
|
'user' => $this->app['user'], |
65
|
|
|
'email' => $to, |
66
|
|
|
'swiftMessage' => $swiftMessageInstance, |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if (isset($data['templateData'])) { |
70
|
|
|
$templateData = array_merge( |
71
|
|
|
$templateData, |
72
|
|
|
$data['templateData'] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (isset($data['body'])) { |
77
|
|
|
$bodyType = isset($data['bodyType']) |
78
|
|
|
? $data['bodyType'] |
79
|
|
|
: 'text/html' |
80
|
|
|
; |
81
|
|
|
$isTwigTemplate = isset($data['contentIsTwigTemplate']) |
82
|
|
|
? $data['contentIsTwigTemplate'] |
83
|
|
|
: true |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
$swiftMessageBody = $this->app['mailer.css_to_inline_styles_converter']( |
87
|
|
|
$data['body'], |
88
|
|
|
$templateData, |
89
|
|
|
$isTwigTemplate |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$swiftMessageInstance->setBody($swiftMessageBody, $bodyType); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->app['mailer']->send($swiftMessageInstance); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/***** Swift Message Instance *****/ |
99
|
|
|
public function getSwiftMessageInstance() |
100
|
|
|
{ |
101
|
|
|
return $this->swiftMessageInstance; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setSwiftMessageInstance(\Swift_Message $swiftMessageInstance) |
105
|
|
|
{ |
106
|
|
|
$this->swiftMessageInstance = $swiftMessageInstance; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Sends the (swift) email |
113
|
|
|
*/ |
114
|
|
|
public function send($swiftMessage = false) |
115
|
|
|
{ |
116
|
|
|
if (!$swiftMessage) { |
117
|
|
|
$swiftMessage = $this->getSwiftMessageInstance(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->app['mailer']->send($swiftMessage); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Short for swift image |
125
|
|
|
*/ |
126
|
|
|
public function image($path) |
127
|
|
|
{ |
128
|
|
|
return \Swift_Image::fromPath($path); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|