forked from WPoets/communication-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.php
More file actions
301 lines (230 loc) · 9.69 KB
/
notifications.php
File metadata and controls
301 lines (230 loc) · 9.69 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace aw2\notify;
\aw2_library::add_service('notify.wpmail','Send wp mail',['namespace'=>__NAMESPACE__]);
function wpmail($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;
extract(\aw2_library::shortcode_atts( array(
'email' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_id' => null,
'tracking_set' => null
), $atts, 'aw2_wpmail' ) );
// if email is null, return
if(is_null($email)) return;
if(!isset($email['to']['email_id']))$email['to']['email_id']='';
if(!isset($email['subject']))$email['subject']='';
if(!isset($email['message']))$email['message']='';
if(!isset($email['headers']))$email['headers']='';
if(!isset($email['attachment']))$email['attachment']='';
$tracking = array();
if(!empty($tracking_set))$tracking['tracking_set']=$tracking_set;
// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('mail', 'wpmail', $email, $log, $notification_object_type, $notification_object_id,$tracking);
wp_mail(
$email['to']['email_id'],
$email['subject'],
$email['message'],
$email['headers'],
$email['attachments']
);
$return_value = "success";
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
\aw2_library::add_service('notify.sendgrid','Send Sendgrid mail',['namespace'=>__NAMESPACE__]);
function sendgrid($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;
//including SENDGRID library
//require_once AWESOME_PATH.'/vendor/autoload.php';
extract(\aw2_library::shortcode_atts( array(
'email' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_id' => null,
'tracking_set' => null
), $atts, 'aw2_sendgrid' ) );
// if email is null, return
if(is_null($email)) return;
// Checking for values and setting them if not present.
if(!isset($email['from']['email_id']))$email['from']['email_id']='';
if(!isset($email['to']['email_id']))$email['to']['email_id']='';
if(!isset($email['message']))$email['message']='';
if(!isset($email['subject']))$email['subject']='';
//provider.apiKey or settings.sendgrid_apiKey
$apiKey = $email['provider']['key'];
if(empty($apiKey) || strlen($apiKey) === 0){
$return_value=\aw2_library::post_actions('all','No api key is not provided, check your settings for default api key!',$atts);
return $return_value;
}
$sendgrid_email = new \SendGrid\Mail\Mail();
$sendgrid_email->setFrom($email['from']['email_id'], null);
$sendgrid_email->setSubject($email['subject']);
//$email['to']['email_id']
if(isset($email['to']['email_id'])){
$to_emails = explode(",",$email['to']['email_id']);
foreach($to_emails as $val){
$sendgrid_email->addTo($val, null);
}
}
// Content
$sendgrid_email->addContent(
"text/html", $email['message']
);
// Works on only when the attachments are present
if(isset($email['attachments']['file'])){
//storing file array in variable
$file = $email['attachments']['file'];
//looping through the file content
for($i=0; $i<sizeof($file); $i++){
$name = $file[$i]['name'];
$path = $file[$i]['path'];
if(!empty($path)){
$file_encoded = base64_encode(file_get_contents($path));
$sendgrid_email->addAttachment($file_encoded, null, $name, "attachment", null);
}
}
}
//$email['cc']['email_id']
if(isset($email['cc']['email_id'])){
$cc_emails = explode(",",$email['cc']['email_id']);
foreach($cc_emails as $val){
$sendgrid_email->addCc($val, null);
}
}
//$email['bcc']['email_id']
if(isset($email['bcc']['email_id'])){
$bcc_emails = explode(",",$email['bcc']['email_id']);
foreach($bcc_emails as $val){
$sendgrid_email->addBcc($val, null);
}
}
//$email['reply_to']['email_id']
if(isset($email['reply_to']['email_id'])){
$reply_to_emails = explode(",",$email['reply_to']['email_id']);
foreach($reply_to_emails as $val){
$sendgrid_email->setReplyTo($val, null);
}
}
$sendgrid = new \SendGrid($apiKey);
try {
$response = $sendgrid->send($sendgrid_email);
//get headers from the response->headers();
$header = $response->headers();
foreach ($header as $val) {
$val_array = explode(':', $val);
if($val_array[0] == 'X-Message-Id'){
//getting the message id from the header response
$messageId = $val_array[1];
//setting up tracking array
$tracking['tracking_id'] = trim($messageId) ;
$tracking['tracking_status'] = 'sent_to_provider';
$tracking['tracking_stage'] = 'sent_to_provider';
if(!empty($tracking_set))$tracking['tracking_set']=$tracking_set;
break;
}
}
$return_value = $response->statusCode();
if($return_value == 202){
$return_value = "success";
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
$return_value = "error";
}
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
\aw2_library::add_service('notify.kookoo','Send Kookoo SMS',['namespace'=>__NAMESPACE__]);
function kookoo($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;
extract(\aw2_library::shortcode_atts( array(
'sms' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_id' => null
), $atts, 'aw2_kookoo' ) );
// if sms is null, return
if(is_null($sms)) return;
if(!isset($sms['to']['mobile_number']))$sms['to']['mobile_number']='';
if(!isset($sms['message']))$sms['message']='';
if(!isset($sms['provider']['key']))$sms['provider']['key']='';
// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('sms', 'kookoo', $sms, $log, $notification_object_type, $notification_object_id);
// api base url
$url = 'http://www.kookoo.in/outbound/outbound_sms.php';
$apiKey = $sms['provider']['key'];
if(empty($apiKey) || strlen($apiKey) === 0){
$return_value=\aw2_library::post_actions('all','No api key is not provided, check you settings for default api key!',$atts);
return $return_value;
}
// parameter to send in sms
$param = array(
'api_key' => $apiKey,
'phone_no' => '0'.$sms['to']['mobile_number'],
'message' => $sms['message']
);
$url = $url . "?" . http_build_query($param, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
$result = simplexml_load_string($result);
$return_value = 'error';
if(isset($result->status)){
$return_value= $result->status;
}
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
\aw2_library::add_service('notify.msg91','Send msg91 SMS',['namespace'=>__NAMESPACE__]);
function msg91($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content,$shortcode)==false)return;
extract(\aw2_library::shortcode_atts( array(
'sms' => null,
'log' => null,
'notification_object_type' => null,
'notification_object_id' => null
), $atts, 'aw2_kookoo' ) );
// if $sms is not present
if(is_null($sms)){
return \aw2_library::post_actions('all','Sms array is required!',$atts);
}
// Log data in db
require_once __DIR__ .'/includes/notification_helper.php';
\notification_log('sms', 'msg91', $sms, $log, $notification_object_type, $notification_object_id);
// check if values are present or not
if(!isset($sms['to']['mobile_number']))$sms['to']['mobile_number']='';
if(!isset($sms['message']))$sms['message']='';
if(!isset($sms['provider']['key']))$sms['provider']['key']='';
// api base url
$url = 'http://api.msg91.com/api/v2/sendsms';
$apiKey = $sms['provider']['key'];
// if api key is not present
if(empty($apiKey) || strlen($apiKey) === 0){
return $return_value=\aw2_library::post_actions('all','No api key is not provided, check you settings for default api key!',$atts);
}
// create sms payload Array
$payloadArr = array();
$payloadArr['sender'] = $sms['provider']['sender'];
$payloadArr['route'] = $sms['provider']['route'];
$payloadArr['country'] = $sms['provider']['country'];
$payloadArr['sms'][0]['message'] = $sms['message'];
$payloadArr['sms'][0]['to'][0] = $sms['to']['mobile_number'];
$payload = json_encode($payloadArr);
// use curl to send data
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("authkey:$apiKey","Content-Type:application/json"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = json_decode(curl_exec($ch));
curl_close($ch);
$return_value= $result->type == 'success' ? 'success' : 'error';
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}