class SiteWebPublicMailerProcessor extends modProc

May 10, 2015
class SiteWebPublicMailerProcessor extends modProcessor{

    public $body = '';

    public function initialize(){

        $this->setDefaultProperties(
            array(
                'from_email'    =>'no-replay@'.$_SERVER['HTTP_HOST']
                ,'from_name'    =>$this->modx->getOption('site_name')
                ,'body'         =>'hello world!'

            )
        );
/*
* в моем случае существует ресурс с настройками (Твшки)
* оттуда тянем email для отправки
*/
        if ($settings = $this->modx->getObject('modResource',2)){
            $this->setProperty('to_email', $settings->getTVValue('email'));
        }

        if (!$this->getProperty('to_email')){
            $this->addFieldError('to_email', 'Ошибка получения email адреса.');
        }

        if ($this->hasErrors()){
            return 'Возникли ошибки при отправке сообщения.';
        }

        return parent::initialize();
    }

    public function process(){
        $this->body = $this->prepareMailBody();
        if ($this->SendMail()){
            return $this->success('Сообщение успешно отправлено.');
        } else {
            return $this->failure('Сообщение не отправлено.');
        }
    }

    public function prepareMailBody(){
        return '';
    }

    public function SendMail(){
        $this->modx->getService('mail', 'mail.modPHPMailer');
        $this->modx->mail->set(modMail::MAIL_BODY,$this->body);
        $this->modx->mail->set(modMail::MAIL_FROM,$this->getProperty('from_email'));
        $this->modx->mail->set(modMail::MAIL_FROM_NAME,$this->getProperty('from_name'));
        $this->modx->mail->set(modMail::MAIL_SUBJECT,$this->getProperty('subject'));
        $this->modx->mail->address('to', $this->getProperty('to_email'));
        $this->modx->mail->setHTML(true);
        $status = $this->modx->mail->send();
        $this->modx->mail->reset();
        return $status;
    }

}
return 'SiteWebPublicMailerProcessor';