-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTemplate_mailer.php
148 lines (124 loc) · 3.43 KB
/
Template_mailer.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
<?php
class Template_mailer {
/**
* @var array
*/
protected $vars = array();
/**
* Constructor
*/
public function __construct()
{
if( ! isset(ee()->TMPL))
{
ee()->load->library('template', NULL, 'TMPL');
}
ee()->load->library('email');
ee()->email->initialize(array(
'mailtype' => 'html',
));
}
/**
* Magic method for function calls
*
* @param mixed
* @param array
* @return mixed|void
*/
public function __call($name, $args)
{
$name = strtolower($name);
$valid_methods = array(
'from',
'reply_to',
'to',
'cc',
'bcc',
'subject',
'set_alt_message',
'attach',
'print_debugger'
);
// if the method name maps to the email class, call it
if(in_array($name, $valid_methods))
{
$result = call_user_func_array(array(ee()->email, $name), $args);
if($result !== NULL)
{
return $result;
}
}
}
/**
* Set variables for tags in the email body
*
* @param string
* @param mixed
*/
public function set($name, $value = NULL)
{
if(is_array($name))
{
$this->vars = array_merge($this->vars, $name);
}
else
{
$this->vars[$name] = $value;
}
}
/**
* Sets the template to use for the message copy
*
* @param string
* @return string
*/
protected function template($template)
{
// split the template group and name
$segments = explode('/', $template);
// use index as name if not specified
if(count($segments) == 1)
{
$segments[1] = 'index';
}
list($template_group, $template_name) = $segments;
// set data items as global variables
foreach($this->vars as $key => $val)
{
ee()->config->_global_vars['email:'.$key] = $val;
}
// parse the template
ee()->TMPL->cache_status = 'NO_CACHE';
$template = ee()->TMPL->fetch_template($template_group, $template_name, false);
$body = ee()->TMPL->parse_globals($template);
$body = ee()->TMPL->parse_variables_row($body, $this->vars);
ee()->TMPL->parse($body, false);
$body = ee()->TMPL->final_template;
// cleanup unparsed conditionals and annotations
$body = preg_replace("/".LD."if\s+.*?".RD.".*?".LD.'\/if'.RD."/s", "", $body);
$body = preg_replace("/\{!--.*?--\}/s", '', $body);
// unset data items
foreach($this->vars as $key => $val)
{
unset(ee()->config->_global_vars['email:'.$key]);
}
return $body;
}
/**
* Sends the email
*
* @param string|array|null
* @return bool
*/
public function send($template)
{
// get template output
$body = $this->template($template);
// set message
ee()->email->message($body);
// send the email
$result = ee()->email->send();
ee()->email->clear();
return $result;
}
}