-
Notifications
You must be signed in to change notification settings - Fork 36
/
EmailerComponent.php
126 lines (105 loc) · 3.22 KB
/
EmailerComponent.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
<?php
/**
* EmailerComponent
*
* This allows the requestAction call to bypass the usage of
* Router::url which can increase performance. The url based arrays are the
* same as the ones that HtmlHelper::link uses with one difference - if you
* are using named or passed parameters, you must put them in a second array
* and wrap them with the correct key. This is because requestAction only merges
* the named args array into the Controller::params member array and does
* not place the named args in the key 'named'.
*
* @code
* $this->Emailer->sendDirectMail(
* array('someone@example.com'),
* array(
* 'subject' => 'this is a test email',
* 'body' => 'The body of the email',
* 'template' => 'User - Registration'
* )
* );
*
* @author dogmatic
*/
class EmailerComponent extends EmailComponent {
/**
* component settings
*
* @var array
*/
public $settings = array();
/**
* default configs
*
* @var array
*/
protected $_default = array();
/**
* Component initialize function.
*/
public function initialize(Controller $Controller, $settings = array()) {
$this->settings = array_merge($this->_default, (array)$settings);
$this->settings();
return true;
}
public function startup(Controller $Controller) {
$this->Controller = $Controller;
$this->settings();
return true;
}
public function settings() {
$this->reset();
$this->delivery = Configure::read('Newsletter.send_method');
if (Configure::read('Newsletter.send_method') == 'smtp') {
$this->smtpOptions = array(
'port' => Configure::read('Newsletter.smtp_out_going_port'),
'timeout' => Configure::read('Newsletter.smtp_timeout'),
'host' => Configure::read('Newsletter.smtp_host'),
'username' => Configure::read('Newsletter.smtp_username'),
'password' => Configure::read('Newsletter.smtp_password'),
'greeting' => Configure::read('Newsletter.greeting')
);
}
$this->sendAs = Configure::read('Newsletter.send_as');
$name = Configure::read('Website.name');
if (Configure::read('Newsletter.from_name')) {
$name = Configure::read('Newsletter.from_name');
}
if (empty($name)) {
$name = 'Infinitas Mailer';
}
$this->template = Configure::read('Newsletter.template');
$this->defaultFromName = $name;
// $this->from = $name . ' <' . Configure::read('Newsletter.from_email') . '>';
$this->from = Configure::read('Newsletter.from_email');
$this->trackViews = Configure::read('Newsletter.track_views');
}
public function sendDirectMail($userDetails, $email = array()) {
$this->trackViews = false;
if (empty($userDetails)) {
return false;
}
$email = array_merge(array(
'body' => '',
'template' => ''
), $email);
try {
$template = ClassRegistry::init('Newsletter.Template')->getTemplate($email['template']);
} catch(Exception $e) {
$this->Controller->notice(
$e->getMessage(),
array('redirect' => true)
);
}
$html = $template['Template']['header'] . $email['body'] . $template['Template']['footer'];
$Email = new InfinitasEmail();
$Email->sendMail(array(
'to' => $userDetails,
'from' => $email['from'],
'subject' => $email['subject'],
'html' => $html,
'text' => strip_tags(str_replace(array('<br/>', '<br>', '</p><p>'), "\n\r", $html))
));
}
}