email.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /*-
  3. * Copyright (c) 2022 - 2023 Mark J Crane <[email protected]>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. /**
  27. * email class
  28. *
  29. * @method boolean send
  30. */
  31. if (!class_exists('email')) {
  32. class email {
  33. /**
  34. * declare the variables
  35. */
  36. private $app_name;
  37. private $app_uuid;
  38. private $name;
  39. public $domain_uuid;
  40. public $method;
  41. public $recipients;
  42. public $subject;
  43. public $body;
  44. public $from_address;
  45. public $from_name;
  46. public $priority;
  47. public $debug_level;
  48. public $attachments;
  49. public $read_confirmation;
  50. public $error;
  51. public $response;
  52. private $settings;
  53. /**
  54. * called when the object is created
  55. */
  56. public function __construct($params = []) {
  57. //assign the variables
  58. $this->app_name = 'email';
  59. $this->name = 'email';
  60. $this->app_uuid = '7a4fef67-5bf8-436a-ae25-7e3c03afcf96';
  61. $this->priority = 0;
  62. $this->debug_level = 3;
  63. $this->read_confirmation = false;
  64. //set the domain_uuid
  65. $this->domain_uuid = $params['domain_uuid'] ?? $_SESSION['domain_uuid'] ?? '';
  66. if (isset($params['settings'])) {
  67. $this->settings = $params['settings'];
  68. }
  69. //set the database from the settings object if available
  70. if ($this->settings instanceof settings && !isset($this->database)) {
  71. $this->database = $this->settings->database();
  72. }
  73. //ensure we have a valid database object
  74. if (!($this->database instanceof database)) {
  75. $this->database = $params['database'] ?? database::new();
  76. }
  77. //ensure we have a valid settings object
  78. if (!($this->settings) instanceof settings) {
  79. $this->settings = new settings(['database' => $this->database, 'domain_uuid' => $this->domain_uuid]);
  80. }
  81. }
  82. /**
  83. * parse raw emails
  84. */
  85. public function parse($message) {
  86. //includes
  87. require_once('resources/pop3/mime_parser.php');
  88. require_once('resources/pop3/rfc822_addresses.php');
  89. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php")) {
  90. require_once($_SERVER["PROJECT_ROOT"]."/app/emails/email_transcription.php");
  91. }
  92. //parse the email message
  93. $mime = new mime_parser_class;
  94. $mime->decode_bodies = 1;
  95. $parameters = array(
  96. //'File'=>$message_file,
  97. // Read a message from a string instead of a file
  98. 'Data' => $message,
  99. // Save the message body parts to a directory
  100. // 'SaveBody' => '/tmp',
  101. // Do not retrieve or save message body parts
  102. // 'SkipBody' => 1,
  103. );
  104. $success = $mime->Decode($parameters, $decoded);
  105. unset($parameters);
  106. if (!$success) {
  107. echo "MIME message decoding error: ".HtmlSpecialChars($mime->error)."\n";
  108. }
  109. else {
  110. //get the headers
  111. $this->headers = json_decode($decoded[0]["Headers"]["x-headers:"], true);
  112. $this->subject = $decoded[0]["Headers"]["subject:"];
  113. $this->from_name = $decoded[0]["ExtractedAddresses"]["from:"][0]["name"];
  114. $this->from_address = $decoded[0]["ExtractedAddresses"]["from:"][0]["address"];
  115. $this->reply_to = $decoded[0]["Headers"]["reply-to:"];
  116. $this->recipients = $decoded[0]["ExtractedAddresses"]["to:"];
  117. $this->date = $decoded[0]["Headers"]["date:"];
  118. //debug information
  119. //view_array($decoded[0]);
  120. //view_array($this);
  121. //view_array($this->recipients);
  122. //get the body
  123. $this->body = ''; //$parts_array["Parts"][0]["Headers"]["content-type:"];
  124. //get the body
  125. $this->body = '';
  126. $this->content_type = $decoded[0]['Headers']['content-type:'];
  127. if (substr($this->content_type, 0, 15) == "multipart/mixed" || substr($this->content_type, 0, 21) == "multipart/alternative") {
  128. foreach ($decoded[0]["Parts"] as $row) {
  129. $body_content_type = $row["Headers"]["content-type:"];
  130. if (substr($body_content_type, 0, 9) == "text/html") {
  131. $this->body = $row["Body"];
  132. }
  133. if (substr($body_content_type, 0, 10) == "text/plain") {
  134. $body_plain = $row["Body"];
  135. $this->body = $body_plain;
  136. }
  137. }
  138. }
  139. else {
  140. $content_type_array = explode(";", $content_type);
  141. $this->body = $decoded[0]["Body"];
  142. //if ($content_type_array[0] == "text/html" || $content_type_array[0] == "text/plain") {
  143. // $body = $row["Body"];
  144. //}
  145. }
  146. //get the attachments and add to the email
  147. $x = 0;
  148. foreach ($decoded[0]["Parts"] as $parts_array) {
  149. //image/tiff;name="testfax.tif"
  150. //text/plain; charset=ISO-8859-1; format=flowed
  151. $content_type = $parts_array["Parts"][0]["Headers"]["content-type:"];
  152. //base64, 7bit
  153. $content_transfer_encoding = $parts_array["Parts"][0]["Headers"]["content-transfer-encoding:"];
  154. //inline;filename="testfax.tif"
  155. $content_disposition = $parts_array["Parts"][0]["Headers"]["content-disposition"];
  156. //testfax.tif
  157. $file = $parts_array["FileName"];
  158. //inline
  159. $filedisposition = $parts_array["FileDisposition"];
  160. $body_part = $parts_array["BodyPart"];
  161. $body_length = $parts_array["BodyLength"];
  162. if (!empty($file)) {
  163. //get the file information
  164. $file_ext = pathinfo($file, PATHINFO_EXTENSION);
  165. $file_name = substr($file, 0, (strlen($file) - strlen($file_ext))-1 );
  166. $encoding = "base64"; //base64_decode
  167. switch ($file_ext){
  168. case "wav":
  169. $mime_type = "audio/x-wav";
  170. break;
  171. case "mp3":
  172. $mime_type = "audio/x-mp3";
  173. break;
  174. case "pdf":
  175. $mime_type = "application/pdf";
  176. break;
  177. case "tif":
  178. $mime_type = "image/tiff";
  179. break;
  180. case "tiff":
  181. $mime_type = "image/tiff";
  182. break;
  183. default:
  184. $mime_type = "binary/octet-stream";
  185. break;
  186. }
  187. //add attachment(s)
  188. $this->attachments[$x]['type'] = 'string';
  189. $this->attachments[$x]['name'] = $file;
  190. $this->attachments[$x]['value'] = $parts_array["Body"];
  191. //increment the id
  192. $x++;
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * send emails
  199. */
  200. public function send() {
  201. //set the send_method if not already set
  202. if (!isset($this->method)) {
  203. if ($this->settings->get('email_queue','enabled', true)) {
  204. $this->method = 'queue';
  205. }
  206. else {
  207. $this->method = 'direct';
  208. }
  209. }
  210. //add the email to the queue
  211. if ($this->method == 'queue') {
  212. //add the email_queue_uuid
  213. $email_queue_uuid = uuid();
  214. //set the email from address and name
  215. $email_from = $this->from_address;
  216. if (!empty($this->from_name)) {
  217. $email_from = $this->from_name.'<'.$email_from.'>';
  218. }
  219. //prepare the array
  220. $array['email_queue'][0]['email_queue_uuid'] = $email_queue_uuid;
  221. $array['email_queue'][0]['domain_uuid'] = $this->domain_uuid;
  222. $array['email_queue'][0]['hostname'] = gethostname();
  223. $array['email_queue'][0]['email_date'] = 'now()';
  224. $array['email_queue'][0]['email_from'] = $email_from;
  225. $array['email_queue'][0]['email_to'] = $this->recipients;
  226. $array['email_queue'][0]['email_subject'] = $this->subject;
  227. $array['email_queue'][0]['email_body'] = $this->body;
  228. $array['email_queue'][0]['email_status'] = 'waiting';
  229. $array['email_queue'][0]['email_retry_count'] = null;
  230. //$array['email_queue'][0]['email_action_before'] = $email_action_before;
  231. //$array['email_queue'][0]['email_action_after'] = $email_action_after;
  232. //add email attachments
  233. if (is_array($this->attachments) && sizeof($this->attachments) > 0) {
  234. $y = 0;
  235. foreach ($this->attachments as $attachment) {
  236. //set the name of the file, determine extension
  237. if ($attachment['path'] && $attachment['name']) {
  238. if (file_exists($attachment['path'] && $attachment['name'])) {
  239. $attachment['type'] = strtolower(pathinfo($attachment['name'], PATHINFO_EXTENSION));
  240. }
  241. }
  242. else if ($attachment['value']) {
  243. //old method
  244. if (strlen($attachment['value']) < 255 && file_exists($attachment['value'])) {
  245. $attachment['name'] = $attachment['name'] != '' ? $attachment['name'] : basename($attachment['value']);
  246. $attachment['path'] = pathinfo($attachment['value'], PATHINFO_DIRNAME);
  247. $attachment['type'] = strtolower(pathinfo($attachment['value'], PATHINFO_EXTENSION));
  248. }
  249. }
  250. //set the mime type
  251. switch ($attachment['type']) {
  252. case "jpg":
  253. case "jpeg":
  254. $attachment['mime_type'] = 'image/jpeg';
  255. break;
  256. case "gif":
  257. $attachment['mime_type'] = 'image/gif';
  258. break;
  259. case "png":
  260. $attachment['mime_type'] = 'image/png';
  261. break;
  262. case "pdf":
  263. $attachment['mime_type'] = 'application/pdf';
  264. break;
  265. case "tif":
  266. case "tiff":
  267. $attachment['mime_type'] = 'image/tiff';
  268. break;
  269. case "mp3":
  270. $attachment['mime_type'] = 'audio/mpeg';
  271. break;
  272. case "wav":
  273. $attachment['mime_type'] = 'audio/x-wav';
  274. break;
  275. case "opus":
  276. $attachment['mime_type'] = 'audio/opus';
  277. break;
  278. case "ogg":
  279. $attachment['mime_type'] = 'audio/ogg';
  280. break;
  281. default:
  282. $attachment['mime_type'] = 'binary/octet-stream';
  283. }
  284. //add the attachments to the array
  285. $array['email_queue_attachments'][$y]['email_queue_attachment_uuid'] = uuid();
  286. $array['email_queue_attachments'][$y]['email_queue_uuid'] = $email_queue_uuid;
  287. $array['email_queue_attachments'][$y]['domain_uuid'] = $this->domain_uuid;
  288. $array['email_queue_attachments'][$y]['email_attachment_mime_type'] = $attachment['mime_type'];
  289. $array['email_queue_attachments'][$y]['email_attachment_type'] = $attachment['type'];
  290. $array['email_queue_attachments'][$y]['email_attachment_name'] = $attachment['name'];
  291. $array['email_queue_attachments'][$y]['email_attachment_path'] = $attachment['path'];
  292. $array['email_queue_attachments'][$y]['email_attachment_base64'] = $attachment['base64'];
  293. $y++;
  294. }
  295. }
  296. //add temporary permissions
  297. $p = permissions::new();
  298. $p->add("email_queue_add", 'temp');
  299. $p->add("email_queue_attachment_add", 'temp');
  300. //save the dialplan
  301. $this->database->app_name = 'email';
  302. $this->database->app_uuid = 'e24b5dab-3bcc-42e8-99c1-19b0c558c2d7';
  303. $this->database->save($array);
  304. //$dialplan_response = $this->database->message;
  305. unset($array);
  306. //remove temporary permissions
  307. $p->delete("dialplan_add", 'temp');
  308. $p->delete("dialplan_detail_add", 'temp');
  309. //return a human readable response for debugging
  310. if ($this->database->message['message'] == 'OK') {
  311. return "Added to queue";
  312. } else {
  313. //return the SQL server message
  314. return $this->database->message['message'];
  315. }
  316. }
  317. //send the email directly
  318. if ($this->method == 'direct') {
  319. /*
  320. RECIPIENTS NOTE:
  321. Pass in a single email address...
  322. [email protected]
  323. Pass in a comma or semi-colon delimited string of e-mail addresses...
  324. [email protected],[email protected],[email protected]
  325. [email protected];[email protected];[email protected]
  326. Pass in a simple array of email addresses...
  327. Array (
  328. [0] => [email protected]
  329. [1] => [email protected]
  330. [2] => [email protected]
  331. )
  332. Pass in a multi-dimentional array of addresses (delivery, address, name)...
  333. Array (
  334. [0] => Array (
  335. [delivery] => to
  336. [address] => [email protected]
  337. [name] => user 1
  338. )
  339. [1] => Array (
  340. [delivery] => cc
  341. [address] => [email protected]
  342. [name] => user 2
  343. )
  344. [2] => Array (
  345. [delivery] => bcc
  346. [address] => [email protected]
  347. [name] => user 3
  348. )
  349. )
  350. ATTACHMENTS NOTE:
  351. Pass in as many files as necessary in an array in the following format...
  352. Array (
  353. [0] => Array (
  354. [mime_type] => image/jpeg (will be determined by file extension, if empty)
  355. [name] => filename.ext
  356. [path] => /source/folder/ (not used if base64 content)
  357. [base64] => file content as base64 (not used if name and path set)
  358. [cid] => content id of file attachment (only used if referencing attached files in body content)
  359. )
  360. [1] => Array (
  361. ...
  362. )
  363. )
  364. ERROR RESPONSE:
  365. Error messages are stored in the variable passed into $this->error BY REFERENCE
  366. */
  367. try {
  368. //include the phpmailer classes
  369. include_once("resources/phpmailer/class.phpmailer.php");
  370. include_once("resources/phpmailer/class.smtp.php");
  371. //use the email default settings
  372. if (!empty($this->settings->get('email','smtp_hostname'))) {
  373. $smtp['hostname'] = $this->settings->get('email','smtp_hostname');
  374. }
  375. $smtp['host'] = (!empty($this->settings->get('email','smtp_host')) ? $this->settings->get('email','smtp_host'): '127.0.0.1');
  376. if (!empty($this->settings->get('email','smtp_port'))) {
  377. $smtp['port'] = (int)$this->settings->get('email','smtp_port');
  378. }
  379. else {
  380. $smtp['port'] = 0;
  381. }
  382. $smtp['secure'] = $this->settings->get('email','smtp_secure');
  383. $smtp['auth'] = $this->settings->get('email','smtp_auth');
  384. $smtp['username'] = $this->settings->get('email','smtp_username');
  385. $smtp['password'] = $this->settings->get('email','smtp_password');
  386. $smtp['from'] = $this->settings->get('voicemail','smtp_from') ?? $this->settings->get('email','smtp_from');
  387. $smtp['from_name'] = $this->settings->get('voicemail','smtp_from_name') ?? $this->settings->get('email','smtp_from_name');
  388. $smtp['validate_certificate'] = $this->settings->get('email','smtp_validate_certificate');
  389. $smtp['crypto_method'] = $this->settings->get('email','smtp_crypto_method') ?? null;
  390. //override the domain-specific smtp server settings, if any
  391. $sql = "select domain_setting_subcategory, domain_setting_value ";
  392. $sql .= "from v_domain_settings ";
  393. $sql .= "where domain_uuid = :domain_uuid ";
  394. $sql .= "and (domain_setting_category = 'email' or domain_setting_category = 'voicemail') ";
  395. $sql .= "and domain_setting_enabled = 'true' ";
  396. $parameters['domain_uuid'] = $this->domain_uuid;
  397. $result = $this->database->select($sql, $parameters, 'all');
  398. if (is_array($result) && @sizeof($result) != 0) {
  399. foreach ($result as $row) {
  400. if ($row['domain_setting_value'] != '') {
  401. $smtp[str_replace('smtp_','',$row["domain_setting_subcategory"])] = $row['domain_setting_value'];
  402. }
  403. }
  404. }
  405. unset($sql, $parameters, $result, $row);
  406. //value adjustments
  407. $smtp['auth'] = ($smtp['auth'] == "true") ? true : false;
  408. $smtp['password'] = ($smtp['password'] != '') ? $smtp['password'] : null;
  409. $smtp['secure'] = ($smtp['secure'] != "none") ? $smtp['secure'] : null;
  410. $smtp['username'] = ($smtp['username'] != '') ? $smtp['username'] : null;
  411. //create the email object and set general settings
  412. $mail = new PHPMailer();
  413. $mail->IsSMTP();
  414. if (!empty($smtp['hostname'])) {
  415. $mail->Hostname = $smtp['hostname'];
  416. }
  417. $mail->Host = $smtp['host'];
  418. if (is_numeric($smtp['port'])) {
  419. $mail->Port = $smtp['port'];
  420. }
  421. if ($smtp['auth'] == "true") {
  422. $mail->SMTPAuth = true;
  423. $mail->Username = $smtp['username'];
  424. $mail->Password = $smtp['password'];
  425. }
  426. else {
  427. $mail->SMTPAuth = false;
  428. }
  429. $smtp_secure = true;
  430. if ($smtp['secure'] == "") {
  431. $mail->SMTPSecure = 'none';
  432. $mail->SMTPAutoTLS = false;
  433. $smtp_secure = false;
  434. }
  435. elseif ($smtp['secure'] == "none") {
  436. $mail->SMTPSecure = 'none';
  437. $mail->SMTPAutoTLS = false;
  438. $smtp_secure = false;
  439. }
  440. else {
  441. $mail->SMTPSecure = $smtp['secure'];
  442. }
  443. if ($smtp_secure && isset($smtp['validate_certificate']) && $smtp['validate_certificate'] == "false") {
  444. //bypass certificate check e.g. for self-signed certificates
  445. $smtp_options['ssl']['verify_peer'] = false;
  446. $smtp_options['ssl']['verify_peer_name'] = false;
  447. $smtp_options['ssl']['allow_self_signed'] = true;
  448. }
  449. //used to set the SSL version
  450. if ($smtp_secure && isset($smtp['crypto_method'])) {
  451. $smtp_options['ssl']['crypto_method'] = $smtp['crypto_method'];
  452. }
  453. //add SMTP Options if the array exists
  454. if (is_array($smtp_options)) {
  455. $mail->SMTPOptions = $smtp_options;
  456. }
  457. $this->from_address = ($this->from_address != '') ? $this->from_address : $smtp['from'];
  458. $this->from_name = ($this->from_name != '') ? $this->from_name : $smtp['from_name'];
  459. $mail->SetFrom($this->from_address, $this->from_name);
  460. $mail->AddReplyTo($this->from_address, $this->from_name);
  461. $mail->Subject = $this->subject;
  462. $mail->MsgHTML($this->body);
  463. $mail->Priority = $this->priority;
  464. if ($this->read_confirmation) {
  465. $mail->AddCustomHeader('X-Confirm-Reading-To: '.$this->from_address);
  466. $mail->AddCustomHeader('Return-Receipt-To: '.$this->from_address);
  467. $mail->AddCustomHeader('Disposition-Notification-To: '.$this->from_address);
  468. }
  469. if (is_numeric($this->debug_level) && $this->debug_level > 0) {
  470. $mail->SMTPDebug = $this->debug_level;
  471. }
  472. $mail->Timeout = 20; //set the timeout (seconds)
  473. $mail->SMTPKeepAlive = true; //don't close the connection between messages
  474. //add the email recipients
  475. $address_found = false;
  476. if (!is_array($this->recipients)) { // must be a single or delimited recipient address(s)
  477. $this->recipients = str_replace(' ', '', $this->recipients);
  478. $this->recipients = str_replace(',', ';', $this->recipients);
  479. $this->recipients = explode(';', $this->recipients); // convert to array of addresses
  480. }
  481. foreach ($this->recipients as $this->recipient) {
  482. if (is_array($this->recipient)) { // check if each recipient has multiple fields
  483. if ($this->recipient["address"] != '' && valid_email($this->recipient["address"])) { // check if valid address
  484. switch ($this->recipient["delivery"]) {
  485. case "cc" : $mail->AddCC($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]); break;
  486. case "bcc" : $mail->AddBCC($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]); break;
  487. default : $mail->AddAddress($this->recipient["address"], ($this->recipient["name"]) ? $this->recipient["name"] : $this->recipient["address"]);
  488. }
  489. $address_found = true;
  490. }
  491. }
  492. else if ($this->recipient != '' && valid_email($this->recipient)) { // check if recipient value is simply (only) an address
  493. $mail->AddAddress($this->recipient);
  494. $address_found = true;
  495. }
  496. }
  497. if (!$address_found) {
  498. $this->error = "No valid e-mail address provided.";
  499. return false;
  500. }
  501. //add email attachments
  502. if (is_array($this->attachments) && sizeof($this->attachments) > 0) {
  503. foreach ($this->attachments as $attachment) {
  504. //add the attachments
  505. if (file_exists($attachment['path'].'/'.$attachment['name'])) {
  506. $mail->AddAttachment($attachment['path'].'/'.$attachment['name'], $attachment['name'], 'base64', $attachment['mime_type']);
  507. }
  508. else {
  509. if ($attachment['base64']) {
  510. if ($attachment['cid']) {
  511. $mail->addStringEmbeddedImage(base64_decode($attachment['base64']), $attachment['cid'], $attachment['name'], 'base64', $attachment['mime_type']);
  512. }
  513. else {
  514. $mail->AddStringAttachment(base64_decode($attachment['base64']), $attachment['name'], 'base64', $attachment['mime_type']);
  515. }
  516. }
  517. }
  518. }
  519. }
  520. //save output to a buffer
  521. ob_start();
  522. //send the email
  523. $mail_status = $mail->Send();
  524. //get the output buffer
  525. $this->response = ob_get_clean();
  526. //send the email
  527. if (!$mail_status) {
  528. if (isset($mail->ErrorInfo) && !empty($mail->ErrorInfo)) {
  529. $this->error = $mail->ErrorInfo;
  530. }
  531. return false;
  532. }
  533. //cleanup the mail object
  534. $mail->ClearAddresses();
  535. $mail->SmtpClose();
  536. unset($mail);
  537. return true;
  538. }
  539. catch (Exception $e) {
  540. $this->error = $mail->ErrorInfo;
  541. return false;
  542. }
  543. }
  544. }
  545. }
  546. }
  547. /*
  548. $email = new email;
  549. $email->recipients = $recipients;
  550. $email->subject = $email_subject;
  551. $email->body = $email_body;
  552. $email->from_address = $email_from_address;
  553. $email->from_name = $email_from_name;
  554. $email->attachments = $email_attachments;
  555. $response = $mail->error;
  556. $sent = $email->send();
  557. */
  558. ?>