Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 KB | None | 0 0
  1. <?php
  2.  
  3. function __construct() {
  4. //$this->phpmailer = &singleton::get(__NAMESPACE__ . 'phpmailer', true);
  5.  
  6. $this->phpmailer = new PHPMailer(true);
  7.  
  8. }
  9.  
  10. //this kicks off the email processing
  11. public function run_queue() {
  12.  
  13. $queue = &singleton::get(__NAMESPACE__ . 'queue');
  14.  
  15. $queue->run('email');
  16.  
  17. }
  18.  
  19. //this function processes the email stored in the queue
  20. function process_email_queue(&$queue) {
  21.  
  22. $log = &singleton::get(__NAMESPACE__ . 'log');
  23.  
  24. //get the queue data
  25. $qarray = $queue['data'];
  26.  
  27. if (isset($qarray['from_name'])) {
  28. $array['from_name'] = $qarray['from_name'];
  29. }
  30. if (isset($qarray['from'])) {
  31. $array['from'] = $qarray['from'];
  32. }
  33. if (isset($qarray['html'])) {
  34. $array['html'] = $qarray['html'];
  35. }
  36. if (isset($qarray['file'])) {
  37. $array['file'] = $qarray['file'];
  38. }
  39. if (isset($qarray['string_file'])) {
  40. $array['string_file'] = $qarray['string_file'];
  41. }
  42. if (isset($qarray['cc'])) {
  43. $array['cc'] = $qarray['cc'];
  44. }
  45. if (isset($qarray['pop_account_id'])) {
  46. $array['pop_account_id'] = $qarray['pop_account_id'];
  47. }
  48.  
  49. $array['subject'] = $qarray['subject'];
  50. $array['body'] = $qarray['body'];
  51. $array['to']['to'] = $qarray['to']['to'];
  52. $array['to']['to_name'] = $qarray['to']['to_name'];
  53.  
  54. //print_r($array);
  55.  
  56. //let's try and send the email now
  57. if($this->send_email($array)) {
  58. //this means the email will be deleted from the queue (success)
  59. $queue['processed'] = true;
  60. }
  61. else {
  62. if ($queue['retry'] >= 5) {
  63. //this means the email will be deleted from the queue (full fail)
  64. $queue['processed'] = true;
  65.  
  66. $larray['event_severity'] = 'error';
  67. $larray['event_number'] = E_USER_ERROR;
  68. $larray['event_description'] = 'Unable to send email, removing from queue after 5 retries';
  69. $larray['event_file'] = __FILE__;
  70. $larray['event_file_line'] = __LINE__;
  71. $larray['event_type'] = 'email_not_sent';
  72. $larray['event_source'] = 'mailer';
  73. $larray['event_version'] = '1';
  74. $larray['log_backtrace'] = true;
  75.  
  76. $log->add($larray);
  77.  
  78. } else {
  79. $queue['retry']++;
  80. }
  81. }
  82. }
  83.  
  84. //add an email to the email queue
  85. public function queue_email($email_array) {
  86. $queue = &singleton::get(__NAMESPACE__ . 'queue');
  87.  
  88. $id = $queue->add(array('data' => $email_array, 'type' => 'email'));
  89.  
  90. return $id;
  91. }
  92.  
  93. public function send_email($array) {
  94.  
  95. $config = &singleton::get(__NAMESPACE__ . 'config');
  96. $log = &singleton::get(__NAMESPACE__ . 'log');
  97. $pop_accounts = &singleton::get(__NAMESPACE__ . 'pop_accounts');
  98. $smtp_accounts = &singleton::get(__NAMESPACE__ . 'smtp_accounts');
  99.  
  100. try {
  101.  
  102. //clear any current info
  103. $this->phpmailer->ClearAllRecipients();
  104. $this->phpmailer->ClearAttachments();
  105.  
  106. $this->phpmailer->From = 'do_not_reply@' . $config->get('domain');
  107.  
  108. $found_smtp_account = false;
  109.  
  110. if (isset($array['pop_account_id']) && !empty($array['pop_account_id'])) {
  111. $pop_array = $pop_accounts->get(array('id' => $array['pop_account_id'], 'get_other_data' => true));
  112.  
  113. if (!empty($pop_array) && !empty($pop_array[0]['smtp_hostname']) && $pop_array[0]['smtp_enabled'] == 1) {
  114.  
  115. $smtp['hostname'] = $pop_array[0]['smtp_hostname'];
  116. $smtp['port'] = $pop_array[0]['smtp_port'];
  117. $smtp['tls'] = $pop_array[0]['smtp_tls'];
  118. $smtp['username'] = $pop_array[0]['smtp_username'];
  119. $smtp['password'] = decode($pop_array[0]['smtp_password']);
  120. $smtp['authentication'] = $pop_array[0]['smtp_authentication'];
  121. $smtp['email_address'] = $pop_array[0]['smtp_email_address'];
  122.  
  123. $found_smtp_account = true;
  124. }
  125. }
  126. else if (isset($array['smtp_account_id']) && !empty($array['smtp_account_id'])) {
  127. $smtp_array = $smtp_accounts->get(array('id' => $array['smtp_account_id']));
  128.  
  129. if (!empty($smtp_array) && !empty($smtp_array[0]['hostname']) && $smtp_array[0]['enabled'] == 1) {
  130.  
  131. $smtp['hostname'] = $smtp_array[0]['hostname'];
  132. $smtp['port'] = $smtp_array[0]['port'];
  133. $smtp['tls'] = $smtp_array[0]['tls'];
  134. $smtp['username'] = $smtp_array[0]['username'];
  135. $smtp['password'] = decode($smtp_array[0]['password']);
  136. $smtp['authentication'] = $smtp_array[0]['authentication'];
  137. $smtp['email_address'] = $smtp_array[0]['email_address'];
  138.  
  139. $found_smtp_account = true;
  140. }
  141. }
  142.  
  143. if (!$found_smtp_account) {
  144. $smtp_array = $smtp_accounts->get(array('id' => $config->get('default_smtp_account')));
  145.  
  146. if (!empty($smtp_array) && !empty($smtp_array[0]['hostname']) && $smtp_array[0]['enabled'] == 1) {
  147.  
  148. $smtp['hostname'] = $smtp_array[0]['hostname'];
  149. $smtp['port'] = $smtp_array[0]['port'];
  150. $smtp['tls'] = $smtp_array[0]['tls'];
  151. $smtp['username'] = $smtp_array[0]['username'];
  152. $smtp['password'] = decode($smtp_array[0]['password']);
  153. $smtp['authentication'] = $smtp_array[0]['authentication'];
  154. $smtp['email_address'] = $smtp_array[0]['email_address'];
  155.  
  156. $found_smtp_account = true;
  157. }
  158. }
  159.  
  160. if ($found_smtp_account) {
  161.  
  162. //what server to send the email to
  163. $this->phpmailer->Host = $smtp['hostname'];
  164. $this->phpmailer->Mailer = 'smtp';
  165.  
  166. //setup authentication if required
  167. if ($smtp['authentication']) {
  168. $this->phpmailer->SMTPAuth = true; // turn on SMTP authentication
  169. $this->phpmailer->Username = $smtp['username'];
  170. $this->phpmailer->Password = $smtp['password'];
  171. }
  172.  
  173. if ($smtp['tls'] ) {
  174. $this->phpmailer->SMTPSecure = 'tls';
  175. }
  176.  
  177. $this->phpmailer->Port = (int) $smtp['port'];
  178.  
  179. //setup the basic email stuff
  180. if (isset($array['from'])) {
  181. $this->phpmailer->From = $array['from'];
  182. }
  183. else {
  184. if (!empty($smtp['email_address'])) {
  185. $this->phpmailer->From = $smtp['email_address'];
  186. }
  187. }
  188. }
  189. else {
  190. $this->phpmailer->Mailer = 'mail';
  191. if (isset($array['from'])) {
  192. $this->phpmailer->From = $array['from'];
  193. }
  194. }
  195.  
  196. //increase the default timeout to 15 seconds
  197. $this->phpmailer->Timeout = 15;
  198.  
  199. $this->phpmailer->CharSet = 'utf-8';
  200.  
  201. if (isset($array['html']) && ($array['html'] == true)) {
  202. $this->phpmailer->IsHTML(true);
  203. }
  204.  
  205. if (isset($array['from_name'])) {
  206. $this->phpmailer->FromName = $array['from_name'];
  207. }
  208. else {
  209. $this->phpmailer->FromName = $config->get('name');
  210. }
  211.  
  212. $this->phpmailer->Subject = $array['subject'];
  213. $this->phpmailer->Body = $array['body'];
  214.  
  215. if (isset($array['to']) && is_array($array['to'])) {
  216. if (!empty($array['to']['to'])) {
  217. $this->phpmailer->AddAddress($array['to']['to'], $array['to']['to_name']);
  218. }
  219. }
  220.  
  221. //print_r($array);
  222.  
  223. //cc users
  224. if (isset($array['cc']) && is_array($array['cc'])) {
  225. foreach($array['cc'] as $cc_item) {
  226. if (isset($cc_item['to_name'])) {
  227. $this->phpmailer->AddCC($cc_item['to'], $cc_item['to_name']);
  228. }
  229. else {
  230. $this->phpmailer->AddCC($cc_item['to'], '');
  231. }
  232. }
  233. }
  234.  
  235.  
  236. //add multiple files
  237. if (isset($array['file']) && is_array($array['file'])) {
  238. foreach ($array['file'] as $file) {
  239. if (file_exists($file['file'])) {
  240. $this->phpmailer->AddAttachment($file['file'], $file['file_name']);
  241. }
  242. }
  243. }
  244.  
  245. //add multiple files via a string (I haven't really tested this yet)
  246. if (isset($array['string_file']) && is_array($array['string_file'])) {
  247. foreach ($array['string_file'] as $string) {
  248. $this->phpmailer->AddStringAttachment($string['string'], $string['string_name']);
  249. }
  250. }
  251.  
  252. //let's try and send the email now
  253. $this->phpmailer->Send();
  254.  
  255. $array['event_severity'] = 'notice';
  256. $array['event_number'] = E_USER_NOTICE;
  257.  
  258. if (isset($array['to']) && is_array($array['to'])) {
  259. $array['event_description'] = 'Email sent to "' . safe_output($array['to']['to']) . '" from "' . $this->phpmailer->From . '" with subject "'.safe_output($array['subject']).'"';
  260. }
  261. else {
  262. $array['event_description'] = 'Email sent from "' . $this->phpmailer->From . '" with subject "'.safe_output($array['subject']).'"';
  263. }
  264.  
  265. $array['event_file'] = __FILE__;
  266. $array['event_file_line'] = __LINE__;
  267. $array['event_type'] = 'email_sent';
  268. $array['event_source'] = 'mailer';
  269. $array['event_version'] = '1';
  270. $array['log_backtrace'] = false;
  271.  
  272. $log->add($array);
  273.  
  274. return true;
  275. }
  276. catch (phpmailerException $e) {
  277. $array['event_severity'] = 'warning';
  278. $array['event_number'] = E_USER_WARNING;
  279. $array['event_description'] = $e->errorMessage();
  280. $array['event_file'] = __FILE__;
  281. $array['event_file_line'] = __LINE__;
  282. $array['event_type'] = 'email_not_sent';
  283. $array['event_source'] = 'mailer';
  284. $array['event_version'] = '1';
  285. $array['log_backtrace'] = true;
  286.  
  287. $log->add($array);
  288.  
  289. return false;
  290. } catch (Exception $e) {
  291. $array['event_severity'] = 'warning';
  292. $array['event_number'] = E_USER_WARNING;
  293. $array['event_description'] = $e->getMessage();
  294. $array['event_file'] = __FILE__;
  295. $array['event_file_line'] = __LINE__;
  296. $array['event_type'] = 'email_not_sent';
  297. $array['event_source'] = 'mailer';
  298. $array['event_version'] = '1';
  299. $array['log_backtrace'] = true;
  300.  
  301. $log->add($array);
  302.  
  303. return false;
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement