/**
  * sends email to defined address
  *
  * @param string $identifier  identifier for email template
  * @param string $to          recipients email address
  * @param array  $variables   array with template variables that can be called in the template
  * @param array  $attachments absolute filename to an attachment file
  *
  * @return bool
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 16.06.2014
  */
 public static function send($identifier, $to, $variables = array(), $attachments = null)
 {
     $mailObj = SilvercartShopEmail::get()->filter('Identifier', $identifier)->first();
     if (!$mailObj) {
         return false;
     }
     $emailText = trim($mailObj->EmailText);
     if (is_null($emailText) || empty($emailText)) {
         return false;
     }
     $emailSubject = trim($mailObj->Subject);
     if (is_null($emailSubject) || empty($emailSubject)) {
         return false;
     }
     if (!is_array($variables)) {
         $variables = array();
     }
     $templateVariables = new ArrayData($variables);
     $emailTextTemplate = new SSViewer_FromString($mailObj->EmailText);
     $emailText = HTTP::absoluteURLs($emailTextTemplate->process($templateVariables));
     $emailSubjectTemplate = new SSViewer_FromString($mailObj->Subject);
     $emailSubject = HTTP::absoluteURLs($emailSubjectTemplate->process($templateVariables));
     $email = new Email(SilvercartConfig::EmailSender(), $to, $emailSubject, $mailObj->EmailText);
     $email->setTemplate('SilvercartShopEmail');
     $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText));
     self::attachFiles($email, $attachments);
     $email->send();
     if (SilvercartConfig::GlobalEmailRecipient() != '') {
         $email = new Email(SilvercartConfig::EmailSender(), SilvercartConfig::GlobalEmailRecipient(), $emailSubject, $mailObj->EmailText);
         $email->setTemplate('SilvercartShopEmail');
         $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText));
         $email->send();
     }
     //Send the email to additional standard receipients from the n:m
     //relation AdditionalReceipients;
     //Email address is validated.
     if ($mailObj->AdditionalReceipients()->exists()) {
         foreach ($mailObj->AdditionalReceipients() as $additionalReceipient) {
             if ($additionalReceipient->getEmailAddressWithName() && Email::validEmailAddress($additionalReceipient->Email)) {
                 $to = $additionalReceipient->getEmailAddressWithName();
             } elseif ($additionalReceipient->getEmailAddress() && Email::validEmailAddress($additionalReceipient->Email)) {
                 $to = $additionalReceipient->getEmailAddress();
             } else {
                 continue;
             }
             $email = new Email(SilvercartConfig::EmailSender(), $to, $emailSubject, $mailObj->EmailText);
             $email->setTemplate('SilvercartShopEmail');
             $email->populateTemplate(array('ShopEmailSubject' => $emailSubject, 'ShopEmailMessage' => $emailText));
             self::attachFiles($email, $attachments);
             $email->send();
         }
     }
 }