/**
	 * @var Allows anonymous access to this controller's actions.
	 * @access protected
	 */
    protected $allowAnonymous = false;
    /**
	 * Sends an email based on the posted params.
	 *
	 */
    public function actionSend()
    {
        //Is CpRequest?
        if (craft()->request->isCpRequest() == false) {
            return false;
        }
        $this->requirePostRequest();
        //Vars
        $formData = new Mailer_FormModel();
        $formData->sender_name = craft()->request->getPost('mailer_sender_name');
        $formData->sender_mail = craft()->request->getPost('mailer_sender_mail');
        $formData->subject = craft()->request->getPost('mailer_subject');
        $formData->htmlBody = craft()->request->getPost('mailer_htmlBody');
        $formData->attachment = \CUploadedFile::getInstanceByName('mailer_attachment');
        $formData->sendto_custom = craft()->request->getPost('mailer_sendto_custom');
        $formData->to = craft()->request->getPost('mailer_to');
        $formData->cc = craft()->request->getPost('mailer_cc');
        $formData->bcc = craft()->request->getPost('mailer_bcc');
        $formData->sendto_usergroups = craft()->request->getPost('mailer_sendto_usergroups');
        $formData->usergroups = craft()->request->getPost('mailer_usergroups');
        $formData->sendto_users = craft()->request->getPost('mailer_sendto_users');
        $formData->users = craft()->request->getPost('mailer_users');
        //Model Validation
        if (!$formData->validate()) {
            //Log
            MailerPlugin::log('FormData validation failed: "' . implode('; ', $formData->getErrors()) . '"', LogLevel::Info);
            if (craft()->request->isAjaxRequest()) {
                return $this->returnErrorJson($formData->getErrors());
            } else {
                //SetError
                craft()->userSession->setError(Craft::t('There was a problem with your submission, please check the form and try again!'));
                //Set UserElement
                if (!$formData->hasErrors('users') && !empty($formData->users)) {
                    $users_element_array = array();
                    foreach ($formData->users as $user) {
                        $users_element_array[] = craft()->users->getUserById($user);
                    }
                    $formData->users = $users_element_array;
                }
                //Return formData
                craft()->urlManager->setRouteVariables(array('formData' => $formData));
            }
        } else {
            //Log
            MailerPlugin::log('FormData valid', LogLevel::Info);
            //Service
            $mailer_service = craft()->mailer_main->newMailer_fromForm($formData);
            //Return
            if ($mailer_service) {
                //Success
                if (craft()->request->isAjaxRequest()) {
                    return $this->returnJson(array('success' => true));
                } else {
                    craft()->userSession->setNotice(Craft::t('The Mailer has successfully started'));
Пример #2
0
 /**
  * Runs a task step.
  *
  * @param int $step
  * @return bool
  */
 public function runStep($step)
 {
     //BatchMode?
     if ($this->getSettings()->batchMode) {
         if ($step != 0 && $step % $this->getSettings()->batchMails == 0) {
             sleep($this->getSettings()->batchTime);
         }
     }
     //Get MailerModel
     $email = $this->getSettings()->email;
     //Log
     if ($step == 0) {
         //Log
         $desc = str_replace(craft()->plugins->getPlugin('mailer')->getName() . ': ', '', $this->model->description);
         MailerPlugin::log('MailerTask (' . $desc . '): Task started: Step 0', LogLevel::Info);
         //Create & Save LogRecord
         $record = new Mailer_LogRecord();
         $record->setIsNewRecord(true);
         $record->subject = $email->subject;
         $record->htmlBody = $email->htmlBody;
         $record->status = 'running';
         $record->description = $desc;
         $record->save();
         if ($record->hasErrors()) {
             MailerPlugin::log('MailerTask (' . $record->description . '): Failed to save LogRecord: "' . implode('; ', $record->getErrors()) . '"', LogLevel::Error);
         } else {
             MailerPlugin::log('MailerTask (' . $record->description . '): New LogRecord created', LogLevel::Info);
         }
         //Create LogModel
         $log = new Mailer_LogModel();
         $log->id = $record->id;
         $log->success = 0;
         $log->errors = array();
     } else {
         $log = $this->getSettings()->log;
     }
     //Get recipients for this task-loop
     $recipients = $this->getSettings()->recipients->recipients;
     $recipients = $recipients[$step];
     //Set recipients
     $email->toEmail = $recipients['to'];
     if (!empty($recipients['cc'])) {
         $email->cc = explode(',', $recipients['cc']);
         //Email Model needs array for CC-mails
     } else {
         $email->cc = null;
     }
     if (!empty($recipients['bcc'])) {
         $email->bcc = explode(',', $recipients['bcc']);
         //Email Model needs array for BCC-mails
     } else {
         $email->bcc = null;
     }
     //Send
     try {
         craft()->email->sendEmail($email);
         //Add to Log
         MailerPlugin::log('MailerTask (' . $record->description . '): Mail send to "' . $email->toEmail . '"', LogLevel::Info);
         $log->success++;
     } catch (\Exception $e) {
         MailerPlugin::log($e->getMessage(), LogLevel::Error);
         //Add to Log
         $errors = $log->errors;
         $errors[] = array('message' => $e->getMessage(), 'email' => $email->toEmail);
         $log->errors = $errors;
     }
     //Update Log
     if ($step == $this->getTotalSteps() - 1) {
         //Get LogRecord
         $record = Mailer_LogRecord::model()->findbyPk($log->id);
         //Set attributes from LogModel
         $record->dateFinished = new DateTime();
         $record->errors = $log->errors;
         $record->success = $log->success;
         if (count($record->errors) > 0) {
             $record->status = 'failed';
         } else {
             $record->status = 'finished';
         }
         //Update
         $record->update();
         if ($record->hasErrors()) {
             MailerPlugin::log('MailerTask (' . $record->description . '): Last Step / Failed to update LogRecord: "' . implode('; ', $record->getErrors()) . '"', LogLevel::Error);
         } else {
             MailerPlugin::log('MailerTask (' . $record->description . '): Last Step / LogRecord successfully updated', LogLevel::Info);
         }
     } else {
         //Save to settings
         $this->getSettings()->log = $log;
     }
     //Clean & Return
     unset($log);
     unset($email);
     unset($recipients);
     return true;
 }
Пример #3
0
 /**
  * Creates a recipients-array from User-Ids
  *
  * @param array $user_ids
  * @return array
  */
 public function getUserRecipients($user_ids)
 {
     //Get all Users by ids
     $criteria = craft()->elements->getCriteria(ElementType::User);
     $criteria->id = $user_ids;
     $users = $criteria->find();
     //Create recipients-array
     $recipients = array();
     foreach ($users as $user) {
         $recipients[] = array('to' => $user->email);
     }
     //Check
     if (count($recipients) == 0) {
         MailerPlugin::log('getUserRecipients(): No Users found with IDs: "' . implode(', ', $user_ids) . '"', LogLevel::Error);
         return false;
     } else {
         MailerPlugin::log('getUserRecipients(): Number of valid Recipients: ' . count($recipients), LogLevel::Info);
     }
     return $recipients;
 }