Example #1
0
 /**
  * Returns an instance of Swift_RecipientList, based on an address or an array of addresses.
  * 
  * @see getSwiftAddress()
  * 
  * @param $addresses
  * @return array of Swift_Address | Swift_RecipientList
  */
 protected static function getSwiftAddresses($addresses, $recipient_list = false, $type = 'to')
 {
     // Detect single address
     $address = self::getSwiftAddress($addresses);
     // Single address detected
     if ($address instanceof Swift_Address) {
         $result = array($address);
     } else {
         $result = array();
         foreach ($addresses as $address) {
             $result[] = self::getSwiftAddress($address);
         }
     }
     // transform into a recipient list if asked to
     if ($recipient_list) {
         $addresses = $result;
         $result = new Swift_RecipientList();
         $result->add($addresses, null, $type);
     }
     return $result;
 }
 /**
  * Mass mailer
  *
  * @param void
  * @return null
  */
 function mass_mailer()
 {
     if (!MASS_MAILER_ENABLED) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     $email_data = $this->request->post('email');
     $this->smarty->assign(array('email_data' => $email_data, 'exclude' => array($this->logged_user->getId())));
     if ($this->request->isSubmitted()) {
         $errors = new ValidationErrors();
         $subject = trim(array_var($email_data, 'subject'));
         $body = trim(array_var($email_data, 'body'));
         $recipient_ids = array_var($email_data, 'recipients');
         if (empty($subject)) {
             $errors->addError(lang('Subject is required'), 'subject');
         }
         // if
         if (empty($body)) {
             $errors->addError(lang('Body is required'), 'body');
         }
         // if
         $recipients = array();
         if (is_foreachable($recipient_ids)) {
             $recipients = Users::findByIds(array_unique($recipient_ids));
         }
         // if
         if (!is_foreachable($recipients)) {
             $errors->addError(lang('Please select recipients'), 'recipients');
         }
         // if
         if ($errors->hasErrors()) {
             $this->smarty->assign('errors', $errors);
             $this->render();
         }
         // if
         $mailer =& ApplicationMailer::mailer();
         $message = new Swift_Message($subject, $body, 'text/html', EMAIL_ENCODING, EMAIL_CHARSET);
         $recipients_list = new Swift_RecipientList();
         foreach ($recipients as $recipient) {
             $name = $recipient->getDisplayName();
             $email = $recipient->getEmail();
             if ($name == $email) {
                 $name = '';
             }
             // if
             $recipients_list->add($email, $name);
         }
         // foreach
         $name = $this->logged_user->getDisplayName();
         $email = $this->logged_user->getEmail();
         if ($name == $email) {
             $name = '';
         }
         // if
         if ($mailer->batchSend($message, $recipients_list, new Swift_Address($email, $name))) {
             flash_success('Email has been successfully sent');
         } else {
             flash_error('Failed to send email');
         }
         // if
         $this->redirectTo('admin_tools_mass_mailer');
     }
     // if
 }