/**
  * Email a submission.
  *
  * @param AmForms_SubmissionModel $submission
  * @param mixed                   $overrideRecipients [Optional] Override recipients from form settings.
  *
  * @return bool
  */
 public function emailSubmission(AmForms_SubmissionModel $submission, $overrideRecipients = false)
 {
     // Do we even have a form ID?
     if (!$submission->formId) {
         return false;
     }
     // Get form if not already set
     $submission->getForm();
     $form = $submission->form;
     $submission->formName = $form->name;
     if (!$form->notificationEnabled) {
         return false;
     }
     // Get our recipients
     $recipients = ArrayHelper::stringToArray($form->notificationRecipients);
     if ($overrideRecipients !== false) {
         if (is_array($overrideRecipients) && count($overrideRecipients)) {
             $recipients = $overrideRecipients;
         } elseif (is_string($overrideRecipients)) {
             $recipients = ArrayHelper::stringToArray($overrideRecipients);
         }
     }
     $recipients = array_unique($recipients);
     if (!count($recipients)) {
         return false;
     }
     // Other email attributes
     if ($form->notificationReplyToEmail) {
         $replyTo = $this->_translatedObjectPlusEnvironment($form->notificationReplyToEmail, $submission);
         if (!filter_var($replyTo, FILTER_VALIDATE_EMAIL)) {
             $replyTo = null;
         }
     }
     // Start mailing!
     $success = false;
     // Notification email
     $notificationEmail = new EmailModel();
     $notificationEmail->htmlBody = $this->getSubmissionEmailBody($submission);
     $notificationEmail->fromEmail = $this->_translatedObjectPlusEnvironment($form->notificationSenderEmail, $submission);
     $notificationEmail->fromName = $this->_translatedObjectPlusEnvironment($form->notificationSenderName, $submission);
     if (trim($form->notificationSubject) != '') {
         $notificationEmail->subject = $this->_translatedObjectPlusEnvironment($form->notificationSubject, $submission);
     } else {
         $notificationEmail->subject = $this->_translatedObjectPlusEnvironment('{formName} form was submitted', $submission);
     }
     if ($replyTo) {
         $notificationEmail->replyTo = $replyTo;
     }
     // Confirmation email
     $confirmationEmail = new EmailModel();
     $confirmationEmail->htmlBody = $this->getConfirmationEmailBody($submission);
     $confirmationEmail->fromEmail = $this->_translatedObjectPlusEnvironment($form->confirmationSenderEmail, $submission);
     $confirmationEmail->fromName = $this->_translatedObjectPlusEnvironment($form->confirmationSenderName, $submission);
     if (trim($form->confirmationSubject) != '') {
         $confirmationEmail->subject = $this->_translatedObjectPlusEnvironment($form->confirmationSubject, $submission);
     } else {
         $confirmationEmail->subject = $this->_translatedObjectPlusEnvironment('Thanks for your submission.', $submission);
     }
     // Add Bcc?
     $bccEmailAddress = craft()->amForms_settings->getSettingsByHandleAndType('bccEmailAddress', AmFormsModel::SettingGeneral);
     if ($bccEmailAddress && $bccEmailAddress->value) {
         $bccAddresses = ArrayHelper::stringToArray($bccEmailAddress->value);
         $bccAddresses = array_unique($bccAddresses);
         if (count($bccAddresses)) {
             $properBccAddresses = array();
             foreach ($bccAddresses as $bccAddress) {
                 $bccAddress = $this->_translatedObjectPlusEnvironment($bccAddress, $submission);
                 if (filter_var($bccAddress, FILTER_VALIDATE_EMAIL)) {
                     $properBccAddresses[] = array('email' => $bccAddress);
                 }
             }
             if (count($properBccAddresses)) {
                 $notificationEmail->bcc = $properBccAddresses;
                 $confirmationEmail->bcc = $properBccAddresses;
             }
         }
     }
     // Add files to the notification?
     if ($form->notificationFilesEnabled) {
         foreach ($submission->getFieldLayout()->getTabs() as $tab) {
             // Tab fields
             $fields = $tab->getFields();
             foreach ($fields as $layoutField) {
                 // Get actual field
                 $field = $layoutField->getField();
                 // Find assets
                 if ($field->type == 'Assets') {
                     foreach ($submission->{$field->handle}->find() as $asset) {
                         if ($asset->source->type == 'S3') {
                             $file = @file_get_contents($asset->url);
                             // Add asset as attachment
                             if ($file) {
                                 $notificationEmail->addStringAttachment($file, $asset->filename);
                             }
                         } else {
                             $assetPath = craft()->amForms->getPathForAsset($asset);
                             // Add asset as attachment
                             if (IOHelper::fileExists($assetPath . $asset->filename)) {
                                 $notificationEmail->addAttachment($assetPath . $asset->filename);
                             }
                         }
                     }
                 }
             }
         }
     }
     // Fire an 'onBeforeEmailSubmission' event
     $event = new Event($this, array('email' => $notificationEmail, 'submission' => $submission));
     $this->onBeforeEmailSubmission($event);
     // Is the event giving us the go-ahead?
     if ($event->performAction) {
         // Send emails
         foreach ($recipients as $recipient) {
             $notificationEmail->toEmail = $this->_translatedObjectPlusEnvironment($recipient, $submission);
             if (filter_var($notificationEmail->toEmail, FILTER_VALIDATE_EMAIL)) {
                 if (craft()->email->sendEmail($notificationEmail, array('amFormsSubmission' => $submission))) {
                     $success = true;
                 }
             }
         }
         // Send copy?
         if ($form->sendCopy) {
             $sendCopyTo = $submission->{$form->sendCopyTo};
             if (filter_var($sendCopyTo, FILTER_VALIDATE_EMAIL)) {
                 $confirmationEmail->toEmail = $this->_translatedObjectPlusEnvironment($sendCopyTo, $submission);
                 if (craft()->email->sendEmail($confirmationEmail, array('amFormsSubmission' => $submission))) {
                     $success = true;
                 }
             }
         }
         // Fire an 'onEmailSubmission' event
         $this->onEmailSubmission(new Event($this, array('success' => $success, 'email' => $notificationEmail, 'submission' => $submission)));
     }
     return $success;
 }