-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_mailer.php
230 lines (158 loc) · 6.18 KB
/
r_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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
class mailer{
public $to, $from, $headers, $message, $template, $recipients, $method;
public function __construct($jsoncallback='', $to='', $from='', $headers='', $message='', $template='', $method=''){
if( @$to['email'] != "" && @$from['email'] != "" ){
echo $message['hybrid']."\n\n";
echo $message['text']."\n\n";
//print_r( $message['html']['body'] )."\n\n";
$this->to = $to;
$this->from = $from;
$this->headers = $headers;
$this->message = $message;
$this->template = $template;
$this->method = $method;
$this->headers["subject"] = (@$headers["subject"] == "") ? "An email to you" : $headers["subject"];
/* BEGIN CONSTRUCTING THE EMAIL LAYOUT */
$this->buildMail();
}else{
echo $jsoncallback."(".json_encode(array( "response" => "ERROR", "data" => array( "message" => "NOT ALL REQUIRED PARAMETERS HAVE BEEN MET!", "command" => ""))).")";
}
}
public function buildMail(){
if( $this->method["type"] == "mandrill" ){
if( strpos($this->to['email'], ',') !== false ){
/* Building a recipient list from the provided email addresses */
$recipientList = explode( ",", $this->to['email'] );
$recipientName = explode( ",", $this->to['name'] );
$numEmail = count($recipientList);
$numNames = count($recipientName);
$i = 0;
$recipients = "";
$this->errorList = "";
foreach($recipientList as $recipient){
$name = (@$recipientName[$i] != "" && @$recipientName[$i] != NULL)? @$recipientName[$i] : $this->to["name"];
if( filter_var($recipient, FILTER_VALIDATE_EMAIL) ){
$recipients .= '{
"email": '.json_encode($recipient).',
"name": '.json_encode($name).'
}';
}else{
$this->errorList .= '{"email": '.json_encode($recipient).', "name": '.json_encode($name).' }'."\n\n";
}
$i++;
}
$this->recipients['mandrill'] = $recipients;
/* print_r($this->recipients['mandrill']); */
}else{
$this->recipients['mandrill'] = '{ "email":'.@json_encode($this->to["email"]).',"name":'.@json_encode($this->to["name"]).'}';
}
if(
($this->message['hybrid'] == "" || $this->message['text'] == "")
&&
($this->message['html']['header'] != "" )
&&
($this->template == "" || $this->template['head'] == "" )
) :
/*Construct the email using the default layout */
include "mail/mailTemplate.php";
$template = new mailTemplate( $this->message['html']['header'], $this->message['html']['body'], $this->message['html']['footer'] );
$this->message['html'] = $template->build001();
unset($template);
elseif( $this->template['head'] != "" ):
/*Construct a message from a template*/
$message = @$this->template['head'].
@$this->template['bs'].
@$this->template['bhs'].
@$this->message['html']['header'].
@$this->template['bhe'].
@$this->template['bms'].
@$this->message['html']['body'].
@$this->template['bme'].
@$this->template['fms'].
@$this->message['html']['footer'].
@$this->template['fme'].
@$this->template['end'];
$this->message['html'] = $message;
elseif($this->message['hybrid'] != ""):
/*Prepare the hybrid mail content for sending*/
$this->message['html'] = $this->message['hybrid'];
else:
/*Prepare the text mail content for sending*/
$this->message['html'] = $this->message['text'];
endif;
}
/* End of email recipient list builder */
/* SEND THE EMAIL */
$this->mailIt();
}
private function mailIt(){
if( $this->method["type"] == "php" ){
if(mail( $this->to['email'], $this->from[email]."<".$this->from['name'].">", $this->message['html'], $this->headers['subject'] )){
echo $jsoncallback."(".json_encode(array( "response" => "SUCCESS", "data" => array( "message" => "EMAIL SENT to ".$this->to['email']."!", "command" => ""))).")";
exit;
}else{
echo $jsoncallback."(".json_encode(array( "response" => "ERROR", "data" => array( "message" => "FAILED TO SEND EMAIL!", "command" => ""))).")";
exit;
}
}elseif( $this->method["type"] == "mandrill" && $this->method["key"] != "" ){
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
/* DeFaUlT I&E API KEY "C2JgjLf0pN0os9FMvJrc7Q" */
$postString = '{
"key": '.$this->method["key"].',
"message": {
"html": '.$this->message["html"].',
"text": '.$this->message["text"].',
"subject": '.$this->headers["subject"].',
"from_email": '.$this->from["email"].',
"from_name": '.$this->from["name"].',
"to": [
'.
$this->recipients['mandrill']
.'
],
"headers": {
},
"track_opens": true,
"track_clicks": true,
"auto_text": true,
"url_strip_qs": true,
"preserve_recipients": true,
"merge": true,
"global_merge_vars": [
],
"merge_vars": [
],
"tags": [
],
"google_analytics_domains": [
],
"google_analytics_campaign": "...",
"metadata": [
],
"recipient_metadata": [
],
"attachments": [
]
},
"async": false
}';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch, CURLOPT_URL, $uri );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postString );
$result = curl_exec($ch);
echo $result;
}else{
echo $jsoncallback."(".json_encode(array( "response" => "ERROR", "data" => array( "message" => "NOT ALL REQUIRED METHOD PARAMETERS HAVE BEEN MET! [The API actively refused the connection]", "command" => ""))).")";
exit;
}
}
/*END OF CLASS*/
}
?>