Advertisement
Guest User

Untitled

a guest
May 8th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.08 KB | None | 0 0
  1. <?php ini_set('display_errors', 0);?>
  2. <?php include('includes/helpers/PHPMailerAutoload.php');?>
  3. <?php include('includes/helpers/integrations/zapier/triggers/functions.php');?>
  4. <?php
  5. include('includes/config.php');
  6. //--------------------------------------------------------------//
  7. function dbConnect() { //Connect to database
  8. //--------------------------------------------------------------//
  9. // Access global variables
  10. global $mysqli;
  11. global $dbHost;
  12. global $dbUser;
  13. global $dbPass;
  14. global $dbName;
  15. global $dbPort;
  16.  
  17. // Attempt to connect to database server
  18. if(isset($dbPort)) $mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName, $dbPort);
  19. else $mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
  20.  
  21. // If connection failed...
  22. if ($mysqli->connect_error) {
  23. fail();
  24. }
  25.  
  26. global $charset; mysqli_set_charset($mysqli, isset($charset) ? $charset : "utf8");
  27.  
  28. return $mysqli;
  29. }
  30. //--------------------------------------------------------------//
  31. function fail() { //Database connection fails
  32. //--------------------------------------------------------------//
  33. print 'Database error';
  34. exit;
  35. }
  36. // connect to database
  37. dbConnect();
  38. ?>
  39. <?php include('includes/helpers/short.php');?>
  40. <?php include('includes/helpers/locale.php');?>
  41. <?php
  42. //setup cron
  43. $q = 'SELECT id, cron, send_rate, ses_endpoint FROM login LIMIT 1';
  44. $r = mysqli_query($mysqli, $q);
  45. if ($r)
  46. {
  47. while($row = mysqli_fetch_array($r))
  48. {
  49. $cron = $row['cron'];
  50. $userid = $row['id'];
  51. $send_rate = $row['send_rate'];
  52. $ses_endpoint = $row['ses_endpoint'];
  53.  
  54. if($cron==0)
  55. {
  56. $q2 = 'UPDATE login SET cron=1 WHERE id = '.$userid;
  57. $r2 = mysqli_query($mysqli, $q2);
  58. if ($r2) exit;
  59. }
  60. }
  61. }
  62.  
  63. $the_offset = '';
  64. $offset = isset($_GET['offset']) ? $_GET['offset'] : '';
  65.  
  66. //Check campaigns database
  67. $q = 'SELECT timezone, sent, id, app, userID, to_send, to_send_lists, recipients, timeout_check, send_date, lists, from_name, from_email, reply_to, title, label, plain_text, html_text, query_string, opens_tracking, links_tracking FROM campaigns WHERE (send_date !="" AND lists !="" AND timezone != "") OR (to_send > recipients) ORDER BY sent DESC';
  68. $r = mysqli_query($mysqli, $q);
  69.  
  70. if ($r && mysqli_num_rows($r) > 0)
  71. {
  72. while($row = mysqli_fetch_array($r))
  73. {
  74. //prepare variables
  75. $timezone = $row['timezone'];
  76. $sent = $row['sent'];
  77. $campaign_id = $row['id'];
  78. $app = $row['app'];
  79. $userID = $row['userID'];
  80. $send_date = $row['send_date'];
  81. $email_list = $row['lists'];
  82. $time = time();
  83. $current_recipient_count = $row['recipients'];
  84. $timeout_check = $row['timeout_check'];
  85. $from_name = stripslashes($row['from_name']);
  86. $from_email = stripslashes($row['from_email']);
  87. $reply_to = stripslashes($row['reply_to']);
  88. $title = stripslashes($row['title']);
  89. $campaign_title = $row['label']=='' ? $title : stripslashes(htmlentities($row['label'],ENT_QUOTES,"UTF-8"));
  90. $plain_text = stripslashes($row['plain_text']);
  91. $html = stripslashes($row['html_text']);
  92. $query_string = stripslashes($row['query_string']);
  93. $to_send_num = $row['to_send'];
  94. $to_send = $to_send_num;
  95. $to_send_lists = $row['to_send_lists'];
  96. $opens_tracking = $row['opens_tracking'];
  97. $links_tracking = $row['links_tracking'];
  98.  
  99. //Set language
  100. $q_l = 'SELECT login.language FROM campaigns, login WHERE campaigns.id = '.$campaign_id.' AND login.app = campaigns.app';
  101. $r_l = mysqli_query($mysqli, $q_l);
  102. if ($r_l && mysqli_num_rows($r_l) > 0) while($row = mysqli_fetch_array($r_l)) $language = $row['language'];
  103. set_locale($language);
  104.  
  105. //get user details
  106. $q2 = 'SELECT s3_key, s3_secret, name, username, timezone FROM login WHERE id = '.$userID;
  107. $r2 = mysqli_query($mysqli, $q2);
  108. if ($r2)
  109. {
  110. while($row = mysqli_fetch_array($r2))
  111. {
  112. $s3_key = $row['s3_key'];
  113. $s3_secret = $row['s3_secret'];
  114. $user_name = $row['name'];
  115. $user_email = $row['username'];
  116. $user_timezone = $row['timezone'];
  117. }
  118. }
  119.  
  120. //Set default timezone
  121. date_default_timezone_set($timezone!='0' && $timezone!='' ? $timezone : $user_timezone);
  122.  
  123. //get smtp settings
  124. $q3 = 'SELECT smtp_host, smtp_port, smtp_ssl, smtp_username, smtp_password FROM apps WHERE id = '.$app;
  125. $r3 = mysqli_query($mysqli, $q3);
  126. if ($r3 && mysqli_num_rows($r3) > 0)
  127. {
  128. while($row = mysqli_fetch_array($r3))
  129. {
  130. $smtp_host = $row['smtp_host'];
  131. $smtp_port = $row['smtp_port'];
  132. $smtp_ssl = $row['smtp_ssl'];
  133. $smtp_username = $row['smtp_username'];
  134. $smtp_password = $row['smtp_password'];
  135. }
  136. }
  137.  
  138. //check if we should send email now
  139. if((($time>=$send_date && $time<$send_date+300) && $sent=='') || (($send_date<$time) && $sent=='') || ($send_date=='0' && $timezone=='0'))
  140. {
  141. //if resuming
  142. if($offset!='')
  143. $q = 'UPDATE campaigns SET send_date=NULL, lists=NULL, timezone=NULL WHERE id = '.$campaign_id;
  144. else
  145. $q = 'UPDATE campaigns SET sent = "'.$time.'", send_date=NULL, lists=NULL, timezone=NULL WHERE id = '.$campaign_id;
  146. $r = mysqli_query($mysqli, $q);
  147. if ($r){}
  148.  
  149. //if sending for the first time
  150. if($offset=='')
  151. {
  152. //If links tracking is enabled, insert links into database
  153. if($links_tracking)
  154. {
  155. //Insert web version link
  156. if(strpos($html, '</webversion>')==true)
  157. {
  158. mysqli_query($mysqli, 'INSERT INTO links (campaign_id, link) VALUES ('.$campaign_id.', "'.APP_PATH.'/w/'.short($campaign_id).'")');
  159. }
  160.  
  161. //Insert into links
  162. $links = array();
  163. //extract all links from HTML
  164. preg_match_all('/href=["\']([^"\']+)["\']/i', $html, $matches, PREG_PATTERN_ORDER);
  165. $matches = array_unique($matches[1]);
  166. foreach($matches as $var)
  167. {
  168. $var = $query_string!='' ? ((strpos($var,'?') !== false) ? $var.'&'.$query_string : $var.'?'.$query_string) : $var;
  169. if(substr($var, 0, 1)!="#" && substr($var, 0, 6)!="mailto" && substr($var, 0, 3)!="tel" && substr($var, 0, 3)!="sms" && substr($var, 0, 13)!="[unsubscribe]" && substr($var, 0, 12)!="[webversion]" && !strpos($var, 'fonts.googleapis.com'))
  170. {
  171. array_push($links, $var);
  172. }
  173. }
  174. //extract unique links
  175. for($i=0;$i<count($links);$i++)
  176. {
  177. $q = 'INSERT INTO links (campaign_id, link) VALUES ('.$campaign_id.', "'.$links[$i].'")';
  178. $r = mysqli_query($mysqli, $q);
  179. if ($r){}
  180. }
  181. }
  182.  
  183. //Get and update number of recipients to send to
  184. $q = 'SELECT id FROM subscribers WHERE list in ('.$email_list.') AND unsubscribed = 0 AND bounced = 0 AND complaint = 0 AND confirmed = 1 GROUP BY email';
  185. $r = mysqli_query($mysqli, $q);
  186. if ($r)
  187. {
  188. $to_send = mysqli_num_rows($r);
  189. $to_send_num = $to_send;
  190. $q2 = 'UPDATE campaigns SET to_send = '.$to_send.', to_send_lists = "'.$email_list.'" WHERE id = '.$campaign_id;
  191. $r2 = mysqli_query($mysqli, $q2);
  192. if ($r2){}
  193. }
  194. }
  195. else
  196. {
  197. //if resuming
  198. $email_list = $to_send_lists;
  199. //get currently unsubscribed
  200. $uc = 'SELECT id FROM subscribers WHERE unsubscribed = 1 AND last_campaign = '.$campaign_id;
  201. $currently_unsubscribed = mysqli_num_rows(mysqli_query($mysqli, $uc));
  202. //get currently bounced
  203. $bc = 'SELECT id FROM subscribers WHERE bounced = 1 AND last_campaign = '.$campaign_id;
  204. $currently_bounced = mysqli_num_rows(mysqli_query($mysqli, $bc));
  205. //get currently complaint
  206. $cc = 'SELECT id FROM subscribers WHERE complaint = 1 AND last_campaign = '.$campaign_id;
  207. $currently_complaint = mysqli_num_rows(mysqli_query($mysqli, $cc));
  208. //calculate offset (offset should exclude currently unsubscribed, bounced or complaint)
  209. $the_offset = ' OFFSET '.($offset-($currently_unsubscribed+$currently_bounced+$currently_complaint));
  210. }
  211.  
  212. //Replace links in newsletter and put tracking image
  213. $q = 'SELECT id, name, email, list, custom_fields FROM subscribers WHERE list in ('.$email_list.') AND unsubscribed = 0 AND bounced = 0 AND complaint = 0 AND confirmed = 1 GROUP BY email ORDER BY id ASC LIMIT 18446744073709551615'.$the_offset;
  214. $r = mysqli_query($mysqli, $q);
  215. if ($r && mysqli_num_rows($r) > 0)
  216. {
  217. $subscriber_id = '';
  218. $email = '';
  219. $subscriber_list = '';
  220.  
  221. while($row = mysqli_fetch_array($r))
  222. {
  223. //prevent execution timeout
  224. set_time_limit(0);
  225.  
  226. $subscriber_id = $row['id'];
  227. $name = trim($row['name']);
  228. $email = trim($row['email']);
  229. $subscriber_list = $row['list'];
  230. $custom_values = $row['custom_fields'];
  231.  
  232. $html_treated = $html;
  233. $plain_treated = $plain_text;
  234. $title_treated = $title;
  235.  
  236. //replace new links on HTML code
  237. $q2 = 'SELECT id, link FROM links WHERE campaign_id = '.$campaign_id;
  238. $r2 = mysqli_query($mysqli, $q2);
  239. if ($r2 && mysqli_num_rows($r2) > 0)
  240. {
  241. while($row2 = mysqli_fetch_array($r2))
  242. {
  243. $linkID = $row2['id'];
  244. if($query_string!='')
  245. {
  246. $link = (strpos($row2['link'],'?'.$query_string) !== false) ? str_replace('?'.$query_string, '', $row2['link']) : str_replace('&'.$query_string, '', $row2['link']);
  247. }
  248. else $link = $row2['link'];
  249.  
  250. //If link tracking is enabled, replace links with trackable links
  251. if($links_tracking)
  252. {
  253. //replace new links on HTML code
  254. $html_treated = str_replace('href="'.$link.'"', 'href="'.APP_PATH.'/l/'.short($subscriber_id).'/'.short($linkID).'/'.short($campaign_id).'"', $html_treated);
  255. $html_treated = str_replace('href=\''.$link.'\'', 'href="'.APP_PATH.'/l/'.short($subscriber_id).'/'.short($linkID).'/'.short($campaign_id).'"', $html_treated);
  256.  
  257. //replace new links on Plain Text code
  258. $plain_treated = str_replace($link, APP_PATH.'/l/'.short($subscriber_id).'/'.short($linkID).'/'.short($campaign_id), $plain_treated);
  259. }
  260. }
  261. }
  262.  
  263. //tags for subject
  264. preg_match_all('/\[([a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+),\s*fallback=/i', $title_treated, $matches_var, PREG_PATTERN_ORDER);
  265. preg_match_all('/,\s*fallback=([a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*)\]/i', $title_treated, $matches_val, PREG_PATTERN_ORDER);
  266. preg_match_all('/(\[[a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+,\s*fallback=[a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*\])/i', $title_treated, $matches_all, PREG_PATTERN_ORDER);
  267. preg_match_all('/\[([^\]]+),\s*fallback=/i', $title_treated, $matches_var, PREG_PATTERN_ORDER);
  268. preg_match_all('/,\s*fallback=([^\]]*)\]/i', $title_treated, $matches_val, PREG_PATTERN_ORDER);
  269. preg_match_all('/(\[[^\]]+,\s*fallback=[^\]]*\])/i', $title_treated, $matches_all, PREG_PATTERN_ORDER);
  270. $matches_var = $matches_var[1];
  271. $matches_val = $matches_val[1];
  272. $matches_all = $matches_all[1];
  273. for($i=0;$i<count($matches_var);$i++)
  274. {
  275. $field = $matches_var[$i];
  276. $fallback = $matches_val[$i];
  277. $tag = $matches_all[$i];
  278.  
  279. //if tag is Name
  280. if($field=='Name')
  281. {
  282. if($name=='')
  283. $title_treated = str_replace($tag, $fallback, $title_treated);
  284. else
  285. $title_treated = str_replace($tag, $row[strtolower($field)], $title_treated);
  286. }
  287. else //if not 'Name', it's a custom field
  288. {
  289. //if subscriber has no custom fields, use fallback
  290. if($custom_values=='')
  291. $title_treated = str_replace($tag, $fallback, $title_treated);
  292. //otherwise, replace custom field tag
  293. else
  294. {
  295. $q5 = 'SELECT custom_fields FROM lists WHERE id = '.$subscriber_list;
  296. $r5 = mysqli_query($mysqli, $q5);
  297. if ($r5)
  298. {
  299. while($row2 = mysqli_fetch_array($r5)) $custom_fields = $row2['custom_fields'];
  300. $custom_fields_array = explode('%s%', $custom_fields);
  301. $custom_values_array = explode('%s%', $custom_values);
  302. $cf_count = count($custom_fields_array);
  303. $k = 0;
  304.  
  305. for($j=0;$j<$cf_count;$j++)
  306. {
  307. $cf_array = explode(':', $custom_fields_array[$j]);
  308. $key = str_replace(' ', '', $cf_array[0]);
  309.  
  310. //if tag matches a custom field
  311. if($field==$key)
  312. {
  313. //if custom field is empty, use fallback
  314. if($custom_values_array[$j]=='')
  315. $title_treated = str_replace($tag, $fallback, $title_treated);
  316. //otherwise, use the custom field value
  317. else
  318. {
  319. //if custom field is of 'Date' type, format the date
  320. if($cf_array[1]=='Date')
  321. $title_treated = str_replace($tag, strftime("%a, %b %d, %Y", $custom_values_array[$j]), $title_treated);
  322. //otherwise just replace tag with custom field value
  323. else
  324. $title_treated = str_replace($tag, $custom_values_array[$j], $title_treated);
  325. }
  326. }
  327. else
  328. $k++;
  329. }
  330. if($k==$cf_count)
  331. $title_treated = str_replace($tag, $fallback, $title_treated);
  332. }
  333. }
  334. }
  335. }
  336.  
  337. //tags for HTML
  338. preg_match_all('/\[([a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+),\s*fallback=/i', $html_treated, $matches_var, PREG_PATTERN_ORDER);
  339. preg_match_all('/,\s*fallback=([a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*)\]/i', $html_treated, $matches_val, PREG_PATTERN_ORDER);
  340. preg_match_all('/(\[[a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+,\s*fallback=[a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*\])/i', $html_treated, $matches_all, PREG_PATTERN_ORDER);
  341. preg_match_all('/\[([^\]]+),\s*fallback=/i', $html_treated, $matches_var, PREG_PATTERN_ORDER);
  342. preg_match_all('/,\s*fallback=([^\]]*)\]/i', $html_treated, $matches_val, PREG_PATTERN_ORDER);
  343. preg_match_all('/(\[[^\]]+,\s*fallback=[^\]]*\])/i', $html_treated, $matches_all, PREG_PATTERN_ORDER);
  344. $matches_var = $matches_var[1];
  345. $matches_val = $matches_val[1];
  346. $matches_all = $matches_all[1];
  347. for($i=0;$i<count($matches_var);$i++)
  348. {
  349. $field = $matches_var[$i];
  350. $fallback = $matches_val[$i];
  351. $tag = $matches_all[$i];
  352.  
  353. //if tag is Name
  354. if($field=='Name')
  355. {
  356. if($name=='')
  357. $html_treated = str_replace($tag, $fallback, $html_treated);
  358. else
  359. $html_treated = str_replace($tag, $row[strtolower($field)], $html_treated);
  360. }
  361. else //if not 'Name', it's a custom field
  362. {
  363. //if subscriber has no custom fields, use fallback
  364. if($custom_values=='')
  365. $html_treated = str_replace($tag, $fallback, $html_treated);
  366. //otherwise, replace custom field tag
  367. else
  368. {
  369. $q5 = 'SELECT custom_fields FROM lists WHERE id = '.$subscriber_list;
  370. $r5 = mysqli_query($mysqli, $q5);
  371. if ($r5)
  372. {
  373. while($row2 = mysqli_fetch_array($r5)) $custom_fields = $row2['custom_fields'];
  374. $custom_fields_array = explode('%s%', $custom_fields);
  375. $custom_values_array = explode('%s%', $custom_values);
  376. $cf_count = count($custom_fields_array);
  377. $k = 0;
  378.  
  379. for($j=0;$j<$cf_count;$j++)
  380. {
  381. $cf_array = explode(':', $custom_fields_array[$j]);
  382. $key = str_replace(' ', '', $cf_array[0]);
  383.  
  384. //if tag matches a custom field
  385. if($field==$key)
  386. {
  387. //if custom field is empty, use fallback
  388. if($custom_values_array[$j]=='')
  389. $html_treated = str_replace($tag, $fallback, $html_treated);
  390. //otherwise, use the custom field value
  391. else
  392. {
  393. //if custom field is of 'Date' type, format the date
  394. if($cf_array[1]=='Date')
  395. $html_treated = str_replace($tag, strftime("%a, %b %d, %Y", $custom_values_array[$j]), $html_treated);
  396. //otherwise just replace tag with custom field value
  397. else
  398. $html_treated = str_replace($tag, $custom_values_array[$j], $html_treated);
  399. }
  400. }
  401. else
  402. $k++;
  403. }
  404. if($k==$cf_count)
  405. $html_treated = str_replace($tag, $fallback, $html_treated);
  406. }
  407. }
  408. }
  409. }
  410. //tags for Plain text
  411. preg_match_all('/\[([a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+),\s*fallback=/i', $plain_treated, $matches_var, PREG_PATTERN_ORDER);
  412. preg_match_all('/,\s*fallback=([a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*)\]/i', $plain_treated, $matches_val, PREG_PATTERN_ORDER);
  413. preg_match_all('/(\[[a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+,\s*fallback=[a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*\])/i', $plain_treated, $matches_all, PREG_PATTERN_ORDER);
  414. preg_match_all('/\[([^\]]+),\s*fallback=/i', $plain_treated, $matches_var, PREG_PATTERN_ORDER);
  415. preg_match_all('/,\s*fallback=([^\]]*)\]/i', $plain_treated, $matches_val, PREG_PATTERN_ORDER);
  416. preg_match_all('/(\[[^\]]+,\s*fallback=[^\]]*\])/i', $plain_treated, $matches_all, PREG_PATTERN_ORDER);
  417. $matches_var = $matches_var[1];
  418. $matches_val = $matches_val[1];
  419. $matches_all = $matches_all[1];
  420. for($i=0;$i<count($matches_var);$i++)
  421. {
  422. $field = $matches_var[$i];
  423. $fallback = $matches_val[$i];
  424. $tag = $matches_all[$i];
  425.  
  426. //if tag is Name
  427. if($field=='Name')
  428. {
  429. if($name=='')
  430. $plain_treated = str_replace($tag, $fallback, $plain_treated);
  431. else
  432. $plain_treated = str_replace($tag, $row[strtolower($field)], $plain_treated);
  433. }
  434. else //if not 'Name', it's a custom field
  435. {
  436. //if subscriber has no custom fields, use fallback
  437. if($custom_values=='')
  438. $plain_treated = str_replace($tag, $fallback, $plain_treated);
  439. //otherwise, replace custom field tag
  440. else
  441. {
  442. $q5 = 'SELECT custom_fields FROM lists WHERE id = '.$subscriber_list;
  443. $r5 = mysqli_query($mysqli, $q5);
  444. if ($r5)
  445. {
  446. while($row2 = mysqli_fetch_array($r5)) $custom_fields = $row2['custom_fields'];
  447. $custom_fields_array = explode('%s%', $custom_fields);
  448. $custom_values_array = explode('%s%', $custom_values);
  449. $cf_count = count($custom_fields_array);
  450. $k = 0;
  451.  
  452. for($j=0;$j<$cf_count;$j++)
  453. {
  454. $cf_array = explode(':', $custom_fields_array[$j]);
  455. $key = str_replace(' ', '', $cf_array[0]);
  456.  
  457. //if tag matches a custom field
  458. if($field==$key)
  459. {
  460. //if custom field is empty, use fallback
  461. if($custom_values_array[$j]=='')
  462. $plain_treated = str_replace($tag, $fallback, $plain_treated);
  463. //otherwise, use the custom field value
  464. else
  465. {
  466. //if custom field is of 'Date' type, format the date
  467. if($cf_array[1]=='Date')
  468. $plain_treated = str_replace($tag, strftime("%a, %b %d, %Y", $custom_values_array[$j]), $plain_treated);
  469. //otherwise just replace tag with custom field value
  470. else
  471. $plain_treated = str_replace($tag, $custom_values_array[$j], $plain_treated);
  472. }
  473. }
  474. else
  475. $k++;
  476. }
  477. if($k==$cf_count)
  478. $plain_treated = str_replace($tag, $fallback, $plain_treated);
  479. }
  480. }
  481. }
  482. }
  483.  
  484. //set web version links
  485. $html_treated = str_replace('<webversion', '<a href="'.APP_PATH.'/w/'.short($subscriber_id).'/'.short($subscriber_list).'/'.short($campaign_id).'" ', $html_treated);
  486. $html_treated = str_replace('</webversion>', '</a>', $html_treated);
  487. $html_treated = str_replace('[webversion]', APP_PATH.'/w/'.short($subscriber_id).'/'.short($subscriber_list).'/'.short($campaign_id), $html_treated);
  488. $plain_treated = str_replace('[webversion]', APP_PATH.'/w/'.short($subscriber_id).'/'.short($subscriber_list).'/'.short($campaign_id), $plain_treated);
  489.  
  490. //set unsubscribe links
  491. $html_treated = str_replace('<unsubscribe', '<a href="'.APP_PATH.'/unsubscribe/'.short($email).'/'.short($subscriber_list).'/'.short($campaign_id).'" ', $html_treated);
  492. $html_treated = str_replace('</unsubscribe>', '</a>', $html_treated);
  493. $html_treated = str_replace('[unsubscribe]', APP_PATH.'/unsubscribe/'.short($email).'/'.short($subscriber_list).'/'.short($campaign_id), $html_treated);
  494. $plain_treated = str_replace('[unsubscribe]', APP_PATH.'/unsubscribe/'.short($email).'/'.short($subscriber_list).'/'.short($campaign_id), $plain_treated);
  495.  
  496. //Email tag
  497. $html_treated = str_replace('[Email]', $email, $html_treated);
  498. $plain_treated = str_replace('[Email]', $email, $plain_treated);
  499. $title_treated = str_replace('[Email]', $email, $title_treated);
  500.  
  501. //convert date tags
  502. date_default_timezone_set($timezone!='' && $timezone!='0' ? $timezone : $user_timezone);
  503. $today = time();
  504. $currentdaynumber = strftime('%d', $today);
  505. $currentday = strftime('%A', $today);
  506. $currentmonthnumber = strftime('%m', $today);
  507. $currentmonth = strftime('%B', $today);
  508. $currentyear = strftime('%Y', $today);
  509. $unconverted_date = array('[currentdaynumber]', '[currentday]', '[currentmonthnumber]', '[currentmonth]', '[currentyear]');
  510. $converted_date = array($currentdaynumber, $currentday, $currentmonthnumber, $currentmonth, $currentyear);
  511. $html_treated = str_replace($unconverted_date, $converted_date, $html_treated);
  512. $plain_treated = str_replace($unconverted_date, $converted_date, $plain_treated);
  513. $title_treated = str_replace($unconverted_date, $converted_date, $title_treated);
  514.  
  515. //If opens tracking is enabled, add 1 x 1 px tracking image
  516. if($opens_tracking)
  517. {
  518. //add tracking 1 by 1px image
  519. $html_treated .= '<img src="'.APP_PATH.'/t/'.short($campaign_id).'/'.short($subscriber_id).'" alt="" style="width:1px;height:1px;"/>';
  520. }
  521.  
  522. //Get server path
  523. $server_path_array = explode('scheduled.php', $_SERVER['SCRIPT_FILENAME']);
  524. $server_path = $server_path_array[0];
  525.  
  526. //send email
  527. $mail = new PHPMailer();
  528. if($s3_key!='' && $s3_secret!='')
  529. {
  530. //if there is an attachment, don't use curl_multi
  531. if(file_exists($server_path.'uploads/attachments/'.$campaign_id))
  532. $mail->IsAmazonSES(false, $campaign_id, $subscriber_id, $user_timezone);
  533. //otherwise send with curl_multi
  534. else
  535. $mail->IsAmazonSES(true, $campaign_id, $subscriber_id, $user_timezone, $send_rate);
  536. $mail->AddAmazonSESKey($s3_key, $s3_secret);
  537. }
  538. else if($smtp_host!='' && $smtp_port!='' && $smtp_username!='' && $smtp_password!='')
  539. {
  540. $mail->IsSMTP();
  541. $mail->SMTPDebug = 0;
  542. $mail->SMTPAuth = true;
  543. $mail->SMTPSecure = $smtp_ssl;
  544. $mail->Host = $smtp_host;
  545. $mail->Port = $smtp_port;
  546. $mail->Username = $smtp_username;
  547. $mail->Password = $smtp_password;
  548. }
  549. $mail->Timezone = $user_timezone;
  550. $mail->CharSet = "UTF-8";
  551. $mail->From = $from_email;
  552. $mail->FromName = $from_name;
  553. $mail->Subject = $title_treated;
  554. $mail->AltBody = $plain_treated;
  555. $mail->Body = $html_treated;
  556. $mail->IsHTML(true);
  557. $mail->AddAddress($email, $name);
  558. $mail->AddReplyTo($reply_to, $from_name);
  559. $mail->AddCustomHeader('List-Unsubscribe: <'.APP_PATH.'/unsubscribe/'.short($email).'/'.short($subscriber_list).'/'.short($campaign_id).'>');
  560. //check if attachments are available for this campaign to attach
  561. if(file_exists($server_path.'uploads/attachments/'.$campaign_id))
  562. {
  563. foreach(glob($server_path.'uploads/attachments/'.$campaign_id.'/*') as $attachment){
  564. if(file_exists($attachment))
  565. $mail->AddAttachment($attachment);
  566. }
  567. }
  568. $mail->Send();
  569.  
  570. //increment recipient count if not using AWS or SMTP
  571. if($s3_key=='' && $s3_secret=='')
  572. {
  573. //increment recipients number in campaigns table
  574. $q5 = 'UPDATE campaigns SET recipients = recipients+1 WHERE id = '.$campaign_id;
  575. mysqli_query($mysqli, $q5);
  576.  
  577. //update last_campaign
  578. $q14 = 'UPDATE subscribers SET last_campaign = '.$campaign_id.' WHERE id = '.$subscriber_id;
  579. mysqli_query($mysqli, $q14);
  580. }
  581. }
  582.  
  583. //====================== Send remaining in queue ======================//
  584. $headers = array();
  585. $date_value = date(DATE_RFC2822);
  586. $headers[] = "Date: {$date_value}";
  587. $signature = base64_encode(hash_hmac("sha1", $date_value, $s3_secret, TRUE));
  588. $headers[] = "X-Amzn-Authorization: AWS3-HTTPS "
  589. ."AWSAccessKeyId=".$s3_key.","
  590. ."Algorithm=HmacSHA1,Signature={$signature}";
  591. $headers[] = "Content-Type: application/x-www-form-urlencoded";
  592.  
  593. $q4 = 'SELECT id, query_str, subscriber_id FROM queue WHERE campaign_id = '.$campaign_id.' AND sent = 0';
  594. $r4 = mysqli_query($mysqli, $q4);
  595. if ($r4 && mysqli_num_rows($r4) > 0)
  596. {
  597. while($row = mysqli_fetch_array($r4))
  598. {
  599. $request_url = 'https://'.$ses_endpoint;
  600. $queue_id = $row['id'];
  601. $query_str = stripslashes($row['query_str']);
  602. $subscriber_id = $row['subscriber_id'];
  603.  
  604. //send remaining in queue
  605. $cr = curl_init();
  606. curl_setopt($cr, CURLOPT_URL, $request_url);
  607. curl_setopt($cr, CURLOPT_POST, $query_str);
  608. curl_setopt($cr, CURLOPT_POSTFIELDS, $query_str);
  609. curl_setopt($cr, CURLOPT_HTTPHEADER, $headers);
  610. curl_setopt($cr, CURLOPT_HEADER, TRUE);
  611. curl_setopt($cr, CURLOPT_RETURNTRANSFER, TRUE);
  612. curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, 2);
  613. curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, 1);
  614. curl_setopt($cr, CURLOPT_CAINFO, $server_path.'certs/cacert.pem');
  615.  
  616. // Make the request and fetch response.
  617. $response = curl_exec($cr);
  618.  
  619. //Get message ID from response
  620. $messageIDArray = explode('<MessageId>', $response);
  621. $messageIDArray2 = explode('</MessageId>', $messageIDArray[1]);
  622. $messageID = $messageIDArray2[0];
  623.  
  624. $response_http_status_code = curl_getinfo($cr, CURLINFO_HTTP_CODE);
  625.  
  626. if ($response_http_status_code !== 200)
  627. {
  628. $q7 = 'SELECT errors FROM campaigns WHERE id = '.$campaign_id;
  629. $r7 = mysqli_query($mysqli, $q7);
  630. if ($r7)
  631. {
  632. while($row = mysqli_fetch_array($r7))
  633. {
  634. $errors = $row['errors'];
  635.  
  636. if($errors=='')
  637. $val = $subscriber_id.':'.$response_http_status_code;
  638. else
  639. {
  640. $errors .= ','.$subscriber_id.':'.$response_http_status_code;
  641. $val = $errors;
  642. }
  643. }
  644. }
  645.  
  646. //update campaigns' errors column
  647. $q6 = 'UPDATE campaigns SET errors = "'.$val.'" WHERE id = '.$campaign_id;
  648. mysqli_query($mysqli, $q6);
  649. }
  650. else
  651. {
  652. //increment recipients number in campaigns table
  653. $q6 = 'UPDATE campaigns SET recipients = recipients+1 WHERE recipients < to_send AND id = '.$campaign_id;
  654. mysqli_query($mysqli, $q6);
  655.  
  656. //update record in queue
  657. $q5 = 'UPDATE queue SET sent = 1, query_str = NULL WHERE id = '.$queue_id;
  658. mysqli_query($mysqli, $q5);
  659.  
  660. //update messageID of subscriber
  661. $q14 = 'UPDATE subscribers SET messageID = "'.$messageID.'" WHERE id = '.$subscriber_id;
  662. mysqli_query($mysqli, $q14);
  663. }
  664. }
  665. }
  666. else
  667. {
  668. $q12 = 'UPDATE campaigns SET to_send = (SELECT recipients) WHERE id = '.$campaign_id;
  669. $r12 = mysqli_query($mysqli, $q12);
  670. if ($r12)
  671. {
  672. $q13 = 'SELECT recipients FROM campaigns WHERE id = '.$campaign_id;
  673. $r13 = mysqli_query($mysqli, $q13);
  674. if ($r13) while($row = mysqli_fetch_array($r13)) $current_recipient_count = $row['recipients'];
  675. $to_send = $current_recipient_count;
  676. $to_send_num = $current_recipient_count;
  677. }
  678. }
  679. //======================= /Send remaining in queue ======================//
  680. }
  681. else
  682. {
  683. $q12 = 'UPDATE campaigns SET to_send = '.$current_recipient_count.' WHERE id = '.$campaign_id;
  684. $r12 = mysqli_query($mysqli, $q12);
  685. if ($r12)
  686. {
  687. $to_send = $current_recipient_count;
  688. $to_send_num = $current_recipient_count;
  689. }
  690. }
  691.  
  692. //=========================== Post processing ===========================//
  693.  
  694. $q8 = 'SELECT recipients FROM campaigns where id = '.$campaign_id;
  695. $r8 = mysqli_query($mysqli, $q8);
  696. if ($r8) while($row = mysqli_fetch_array($r8)) $no_of_recipients = $row['recipients'];
  697. if($no_of_recipients >= $to_send)
  698. {
  699. //tags for subject to me
  700. preg_match_all('/\[([a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+),\s*fallback=/i', $title, $matches_var, PREG_PATTERN_ORDER);
  701. preg_match_all('/,\s*fallback=([a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*)\]/i', $title, $matches_val, PREG_PATTERN_ORDER);
  702. preg_match_all('/(\[[a-zA-Z0-9!#%^&*()+=$@._\-\:|\/?<>~`"\'\s]+,\s*fallback=[a-zA-Z0-9!,#%^&*()+=$@._\-\:|\/?<>~`"\'\s]*\])/i', $title, $matches_all, PREG_PATTERN_ORDER);
  703. preg_match_all('/\[([^\]]+),\s*fallback=/i', $title, $matches_var, PREG_PATTERN_ORDER);
  704. preg_match_all('/,\s*fallback=([^\]]*)\]/i', $title, $matches_val, PREG_PATTERN_ORDER);
  705. preg_match_all('/(\[[^\]]+,\s*fallback=[^\]]*\])/i', $title, $matches_all, PREG_PATTERN_ORDER);
  706. $matches_var = $matches_var[1];
  707. $matches_val = $matches_val[1];
  708. $matches_all = $matches_all[1];
  709. for($i=0;$i<count($matches_var);$i++)
  710. {
  711. $field = $matches_var[$i];
  712. $fallback = $matches_val[$i];
  713. $tag = $matches_all[$i];
  714. //for each match, replace tag with fallback
  715. $title = str_replace($tag, $fallback, $title);
  716. }
  717. $title = str_replace('[Email]', $from_email, $title);
  718. $title = str_replace($unconverted_date, $converted_date, $title);
  719.  
  720. $title_to_me = '['._('Campaign sent').'] '.$title;
  721.  
  722. $app_path = APP_PATH;
  723.  
  724. if($to_send_num=='' || $to_send>$to_send_num) $to_send_num = $to_send;
  725.  
  726. $message_to_me_plain = _('Your campaign has been successfully sent to')." $to_send_num "._('recipients')."!
  727.  
  728. "._('View report')." - $app_path/report?i=$app&c=$campaign_id";
  729.  
  730. $message_to_me_html = "
  731. <div style=\"margin: -10px -10px; padding:50px 30px 50px 30px; height:100%;\">
  732. <div style=\"margin:0 auto; max-width:660px;\">
  733. <div style=\"float: left; background-color: #FFFFFF; padding:10px 30px 10px 30px; border: 1px solid #DDDDDD;\">
  734. <div style=\"float: left; max-width: 106px; margin: 10px 20px 15px 0;\">
  735. <img src=\"$app_path/img/email-icon.gif\" style=\"width: 100px;\"/>
  736. </div>
  737. <div style=\"float: left; max-width:470px;\">
  738. <p style=\"line-height: 21px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 12px;\">
  739. <strong style=\"line-height: 21px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 18px;\">"._('Your campaign has been sent')."!</strong>
  740. </p>
  741. <div style=\"line-height: 21px; min-height: 100px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 12px;\">
  742. <p style=\"line-height: 21px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 12px;\">"._('Your campaign has been successfully sent to')." $to_send_num "._('recipients')."!</p>
  743. <p style=\"line-height: 21px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 12px; margin-bottom: 25px; background-color:#EDEDED; padding: 15px;\">
  744. <strong>"._('Campaign').": </strong>$campaign_title<br/>
  745. <strong>"._('Recipients').": </strong>$to_send_num<br/>
  746. <strong>"._('View report').": </strong><a style=\"color:#4371AB; text-decoration:none;\" href=\"$app_path/report?i=$app&c=$campaign_id\">$app_path/report?i=$app&c=$campaign_id</a>
  747. </p>
  748. <p style=\"line-height: 21px; font-family: Helvetica, Verdana, Arial, sans-serif; font-size: 12px;\">
  749. </p>
  750. </div>
  751. </div>
  752. </div>
  753. </div>
  754. </div>
  755. ";
  756.  
  757. $q4 = 'UPDATE campaigns SET recipients = '.$to_send_num.' WHERE id = '.$campaign_id;
  758. mysqli_query($mysqli, $q4);
  759.  
  760. $q9 = 'DELETE FROM queue WHERE campaign_id = '.$campaign_id;
  761. mysqli_query($mysqli, $q9);
  762.  
  763. $q11 = 'SELECT errors, to_send_lists FROM campaigns WHERE id = '.$campaign_id;
  764. $r11 = mysqli_query($mysqli, $q11);
  765. if ($r11)
  766. {
  767. while($row = mysqli_fetch_array($r11))
  768. {
  769. $error_recipients_ids = $row['errors'];
  770. $tsl = $row['to_send_lists'];
  771. }
  772.  
  773. if($error_recipients_ids=='')
  774. {
  775. $q10 = 'UPDATE subscribers SET bounce_soft = 0 WHERE list IN ('.$tsl.')';
  776. mysqli_query($mysqli, $q10);
  777. }
  778. else
  779. {
  780. $error_recipients_ids_array = explode(',', $error_recipients_ids);
  781. $eid_array = array();
  782. foreach($error_recipients_ids_array as $id_val)
  783. {
  784. $id_val_array = explode(':', $id_val);
  785. array_push($eid_array, $id_val_array[0]);
  786. }
  787. $error_recipients_ids = implode(',', $eid_array);
  788. $q10 = 'UPDATE subscribers SET bounce_soft = 0 WHERE list IN ('.$tsl.') AND id NOT IN ('.$error_recipients_ids.')';
  789. mysqli_query($mysqli, $q10);
  790. }
  791. }
  792.  
  793. //send email to sender
  794. $mail2 = new PHPMailer();
  795. if($s3_key!='' && $s3_secret!='')
  796. {
  797. $mail2->IsAmazonSES();
  798. $mail2->AddAmazonSESKey($s3_key, $s3_secret);
  799. }
  800. else if($smtp_host!='' && $smtp_port!='' && $smtp_ssl!='' && $smtp_username!='' && $smtp_password!='')
  801. {
  802. $mail2->IsSMTP();
  803. $mail2->SMTPDebug = 0;
  804. $mail2->SMTPAuth = true;
  805. $mail2->SMTPSecure = $smtp_ssl;
  806. $mail2->Host = $smtp_host;
  807. $mail2->Port = $smtp_port;
  808. $mail2->Username = $smtp_username;
  809. $mail2->Password = $smtp_password;
  810. }
  811. $mail2->Timezone = $user_timezone;
  812. $mail2->CharSet = "UTF-8";
  813. $mail2->From = $from_email;
  814. $mail2->FromName = $from_name;
  815. $mail2->Subject = $title_to_me;
  816. $mail2->AltBody = $message_to_me_plain;
  817. $mail2->Body = $message_to_me_html;
  818. $mail2->IsHTML(true);
  819. $mail2->AddAddress($from_email, $from_name); //send email to brand account owner
  820. $mail2->AddBCC($user_email, $user_name); //send email to main account owner
  821. $mail2->Send();
  822.  
  823. //Zapier Trigger 'new_user_subscribed' event
  824. zapier_trigger_new_campaign_sent($title_treated, $from_name, $from_email, $reply_to, strftime("%a, %b %d, %Y, %I:%M%p", $sent), APP_PATH.'/w/'.short($campaign_id), $app);
  825.  
  826. //quit
  827.  
  828. }
  829.  
  830.  
  831. //========================== /Post processing ===========================//
  832. }
  833.  
  834. //check if sending timed out
  835. if($current_recipient_count > 0 && $current_recipient_count < $to_send_num && $offset == '')
  836. {
  837. //check time out
  838. $tc_array = explode(':', $timeout_check);
  839. $tc_prev = $tc_array[0];
  840. $tc_now = $current_recipient_count;
  841. $tc = $tc_now.':'.$tc_prev;
  842.  
  843. //update status of timeout
  844. $q = 'UPDATE campaigns SET timeout_check = "'.$tc.'" WHERE id = '.$campaign_id;
  845. mysqli_query($mysqli, $q);
  846.  
  847. //compare prev count with current recipients number count
  848. //if timed out
  849. if($tc_now == $tc_prev)
  850. {
  851. $q = 'UPDATE campaigns SET timeout_check = NULL, send_date = "0", timezone = "0" WHERE id = '.$campaign_id;
  852. mysqli_query($mysqli, $q);
  853.  
  854. //continue sending
  855. $ch = curl_init();
  856. curl_setopt($ch, CURLOPT_HEADER, 0);
  857. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  858. curl_setopt($ch, CURLOPT_URL, APP_PATH.'/scheduled.php?offset='.$current_recipient_count);
  859. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  860. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  861. $data = curl_exec($ch);
  862. }
  863. }
  864. else if($current_recipient_count == $to_send_num)
  865. {
  866. $q = 'UPDATE campaigns SET timeout_check = NULL WHERE id = '.$campaign_id;
  867. mysqli_query($mysqli, $q);
  868. }
  869.  
  870.  
  871. }
  872. //call file again from here!!!
  873. }
  874. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement