/**
  * Wrapper of CakeEmail object creation and submission.
  * @param $emailData
  * @param string $templateName
  * @param string $to
  * @param string $subjectBlockName
  * @param string $tag
  * @param string $locale
  * @param bool $debug
  * @param array $emailOptions
  * @return string
  * @throws Exception
  */
 public static function sendEmail($emailData, $templateName = '', $to = '', $subjectBlockName = '', $tag = '', $locale = 'en-US', $debug = false, $emailOptions = array())
 {
     if (empty($templateName) || empty($to) || empty($subjectBlockName)) {
         CakeLog::error('CRITICAL EXEPTION: In email library.');
         CakeLog::error('Empty argument passed to email sender.');
         throw new BadMethodCallException('Empty argument passed to email sender.', 1);
         return false;
     }
     // Since getting the data from Drupal happens in-line we need to check
     // for errors by seeing if we only get log_info back and nothing else.
     if (!empty($tag)) {
         $drupalData = array();
         DruniqueAPIUtil::getData(['api_url' => 'email_text.json', 'params' => ['tag' => $tag], 'locale' => $locale], $drupalData);
         if (sizeof($drupalData) <= 1) {
             CakeLog::error('CRITICAL EXEPTION: In email library.');
             CakeLog::error('Unable to get view variables from CMS.');
             throw new Exception('Unable to get view variables from CMS.', 1);
             return false;
         }
         $emailData['email_text'] = $drupalData;
     }
     $email = new CakeEmail('default');
     $subject = DruniqueAPIUtil::content($subjectBlockName, $emailData['email_text']);
     $subject = preg_replace('/[\\pZ\\pC]/u', ' ', $subject);
     if ($debug) {
         $email->transport('Debug');
     }
     // Make sure format is set correctly based on existence of templates.
     $format = 'both';
     $htmlTemplateExists = is_readable(APP . "View/Emails/html/{$templateName}.ctp");
     $textTemplateExists = is_readable(APP . "View/Emails/text/{$templateName}.ctp");
     if (!$htmlTemplateExists && !$textTemplateExists) {
         CakeLog::error('CRITICAL ERROR: In email library.');
         CakeLog::error("Unable to find email template named {$templateName}.ctp");
         return false;
     } elseif (!$htmlTemplateExists && $textTemplateExists) {
         $format = 'text';
     } elseif ($htmlTemplateExists && !$textTemplateExists) {
         $format = 'html';
     }
     $email->template($templateName, 'default')->emailFormat($format)->to($to)->subject(html_entity_decode($subject, ENT_QUOTES))->viewVars($emailData);
     if (isset($emailOptions['bcc'])) {
         $email->bcc($emailOptions['bcc']);
     }
     if (isset($emailOptions['cc'])) {
         $email->cc($emailOptions['cc']);
     }
     try {
         $response = $email->send();
         if ($debug) {
             return $response;
         }
         if ($response) {
             $result = 'Email was sent successfully';
         } else {
             $result = 'Email failed to send without exceptions';
             CakeLog::error('CRITICAL ERROR: Email library failed to send an email without exception.');
             Cakelog::error(print_r($email, true));
         }
     } catch (Exception $e) {
         $result = 'Email failed with exception: ' . print_r($e->getMessage(), true);
         CakeLog::error('CRITICAL ERROR: In email library.');
         CakeLog::error($e->getMessage());
     }
     return $result;
 }