我想在Zend2 Email上添加附件,但我无法理解我对以下代码的处理方式?

时间:2015-02-24 05:39:39

标签: php zend-framework zend-framework2 sendmail

以下是我用于发送电子邮件的代码,但告诉我如何添加附件?以下是我用于发送电子邮件的代码但是告诉我如何添加附件?< / p>

    <?php
namespace Application\Controller;

use Application\Filter\DomainFilter;
use Application\Form\DomainForm;
use Application\Form\SendForm;
use Application\Model\AccountModel;
use Application\Model\CustomersModel;
use Application\Model\DomainModel;
use Application\Model\UploadModel;
use Application\Service\Demo;
use Zend\Crypt\BlockCipher;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

/**
 * Class DomainController
 * @package Application\Controller
 */
class DomainController extends AbstractActionController
{
    /**
     * @var DomainModel
     */
    protected $_DomainModel;

    /**
     * @var DomainForm
     */
    protected $_DomainForm;

    /**
     * @var SendForm
     */
    protected $_SendForm;

    /**
     * @var AccountModel
     */
    protected $_AccountModel;

    /**
     * @var UploadModel
     */
    protected $_UploadModel;

    /**
     * @var CustomersModel
     */
    protected $_CustomersModel;

    /**
     * @var SettingModel
     */
    protected $_SettingModel;

    /**
     * @var Demo
     */
    protected $_Demo;

    /**
     * @param \Application\Service\Demo $Demo
     */
    public function setDemo($Demo)
    {
        $this->_Demo = $Demo;
    }

    /**
     * @return \Application\Service\Demo
     */
    public function getDemo()
    {
        return $this->_Demo;
    }

    /**
     * @param \Application\Model\CustomersModel $CustomersModel
     */
    public function setCustomersModel($CustomersModel)
    {
        $this->_CustomersModel = $CustomersModel;
    }

    /**
     * @return \Application\Model\CustomersModel
     */
    public function getCustomersModel()
    {
        return $this->_CustomersModel;
    }

    /**
     * @param \Application\Model\UploadModel $UploadModel
     */
    public function setUploadModel($UploadModel)
    {
        $this->_UploadModel = $UploadModel;
    }

    /**
     * @return \Application\Model\UploadModel
     */
    public function getUploadModel()
    {
        return $this->_UploadModel;
    }

    /**
     * @param \Application\Form\DomainForm $DomainForm
     */
    public function setDomainForm($DomainForm)
    {
        $this->_DomainForm = $DomainForm;
    }

    /**
     * @return \Application\Form\DomainForm
     */
    public function getDomainForm()
    {
        return $this->_DomainForm;
    }

    /**
     * @param \Application\Model\DomainModel $DomainModel
     */
    public function setDomainModel($DomainModel)
    {
        $this->_DomainModel = $DomainModel;
    }

    /**
     * @return \Application\Model\DomainModel
     */
    public function getDomainModel()
    {
        return $this->_DomainModel;
    }

    /**
     * @param \Application\Model\AccountModel $AccountModel
     */
    public function setAccountModel($AccountModel)
    {
        $this->_AccountModel = $AccountModel;
    }

    /**
     * @return \Application\Model\AccountModel
     */
    public function getAccountModel()
    {
        return $this->_AccountModel;
    }

    /**
     * @return the $_SendForm
     */
    public function getSendForm()
    {
        return $this->_SendForm;
    }

    /**
     * @param field_type $_SendForm
     */
    public function setSendForm($_SendForm)
    {
        $this->_SendForm = $_SendForm;
    }

    /**
     * @param mixed $SettingModel
     */
    public function setSettingModel($SettingModel)
    {
        $this->_SettingModel = $SettingModel;
    }

    /**
     * @return mixed
     */
    public function getSettingModel()
    {
        return $this->_SettingModel;
    }

    /**
     * Domain
     * @return array|ViewModel
     */
    public function indexAction()
    {
        return new ViewModel(array(
            'domain' => $this->getDomainModel()->getList(),
            'message' => $this->flashMessenger()->getMessages()
        ));
    }

    /**
     * Add Domain
     * @return \Zend\Http\Response|ViewModel
     */
    public function addAction()
    {
        $request = $this->getRequest();
        $form    = $this->getDomainForm();
        $form->setData(array('expired' => date('d/m/Y', strtotime('+1 year'))));
        $Adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $BC      = $this->getServiceLocator()->get('block_cipher');

        $blockCipher = BlockCipher::factory('mcrypt', array('algo' => $BC['algo']));
        $blockCipher->setKey($this->ApiKey()->getApiKey());

        if (true === $request->isPost()) {

            $post = $request->getPost()->toArray();
            $form->setData($post);
            $form->setInputFilter(new DomainFilter($Adapter, 0));

            if (true === $form->isValid()) {

                $post['created'] = date('Y-m-d H:i:s');
                $post['expired'] = date('Y-m-d', $this->FormatDates()->getDateToMkTime($post['expired']));
                $post['pwd_ftp'] = $blockCipher->encrypt($post['pwd_ftp']);

                $this->getDomainModel()->insert($post);
                $this->flashMessenger()->addMessage(array('success', 1));
                return $this->redirect()->toRoute('domain');
            }
        }

        return new ViewModel(array(
            'form' => $form
        ));
    }

    /**
     * Edit Domain
     * @return \Zend\Http\Response|ViewModel
     */
    public function editAction()
    {
        $ID      = (int)$this->params()->fromRoute('id', 0);
        $row     = $this->getDomainModel()->find(array('d.id' => $ID));
        $Adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $BC      = $this->getServiceLocator()->get('block_cipher');

        $blockCipher = BlockCipher::factory('mcrypt', array('algo' => $BC['algo']));
        $blockCipher->setKey($this->ApiKey()->getApiKey());

        if (empty($row)) {
            $this->getResponse()->setStatusCode(404);
            return;
        }

        $request = $this->getRequest();
        $form    = $this->getDomainForm();

        $row['pwd_ftp'] = $blockCipher->decrypt($row['pwd_ftp']);
        $row['expired'] = date('d/m/Y', strtotime($row['expired']));
        $form->setData($row);

        if (true === $request->isPost()) {

            $post = $request->getPost()->toArray();
            $form->setData($post);
            $form->setInputFilter(new DomainFilter($Adapter, $ID));

            if (true === $form->isValid()) {

                $post['expired'] = date('Y-m-d', $this->FormatDates()->getDateToMkTime($post['expired']));
                $post['pwd_ftp'] = $blockCipher->encrypt($post['pwd_ftp']);

                $this->getDomainModel()->update($ID, $post);
                $this->flashMessenger()->addMessage(array('success', 1));
                return $this->redirect()->toRoute('domain');
            }
        }

        return new ViewModel(array(
            'form' => $form
        ));
    }

    /**
     * Delete Domain / Account
     * @return \Zend\Http\Response
     */
    public function deleteAction()
    {
        $ID = (int)$this->params()->fromRoute('id', 0);
        $this->getDomainModel()->delete($ID);
        $this->getAccountModel()->deleteAllByDomain($ID);
        $this->getUploadModel()->deleteAllByDomain($ID);
        $this->flashMessenger()->addMessage(array('success', 1));
        return $this->redirect()->toRoute('domain');
    }

    /**
     * Detaglio Domain
     * @return ViewModel
     */
    public function detailAction()
    {
        $ID     = (int)$this->params()->fromRoute('id', 0);
        $domain = $this->getDomainModel()->find(array('d.id' => $ID));

        if (empty($domain)) {
            $this->getResponse()->setStatusCode(404);
            return;
        }

        return new ViewModel(array(
            'domain' => $domain,
            'account' => $this->getAccountModel()->getList(array('a.id_domain' => $ID)),
            'upload' => $this->getUploadModel()->getList(array('id_domain' => $ID)),
            'type_ftp' => $this->getServiceLocator()->get('type_ftp')
        ));
    }

    /**
     * Send Domain
     * @return \Zend\Http\Response|ViewModel
     */
    public function sendAction()
    {
        $ID      = (int)$this->params()->fromRoute('id', 0);
        $domain  = $this->getDomainModel()->find(array('d.id' => $ID));
        $request = $this->getRequest();

        $BC = $this->getServiceLocator()->get('block_cipher');

        $blockCipher = BlockCipher::factory('mcrypt', array('algo' => $BC['algo']));
        $blockCipher->setKey($this->ApiKey()->getApiKey());

        if (empty($domain)) {
            $this->getResponse()->setStatusCode(404);
            return;
        }

        if (true === $request->isPost()) {

            $post        = $request->getPost()->toArray();
            $array_email = array();

            if (isset($post['info_domain'])) {
                $array_email['info_domain'] = array(
                    'fullname' => $domain['fullname'],
                    'url' => $domain['url'],
                    'auth_code' => $domain['auth_code'],
                    'name_provider' => $domain['name_provider'],
                    'name_hosting' => $domain['name_hosting'],
                    'name_server' => $domain['name_server']
                );
            }

            if (isset($post['hosting_info'])) {
                $pwd_ftp            = !empty($domain['pwd_ftp']) ? $blockCipher->decrypt($domain['pwd_ftp']) : '';
                $array_email['hosting_info'] = array(
                    'fullname' => $domain['fullname'],
                    'url' => $domain['url'],
                    'pwd_ftp' => $pwd_ftp,
                    'port_ftp' => $domain['port_ftp'],
                    'name_hosting' => $domain['name_hosting'],
                    'name_server' => $domain['name_server']
                );
            }
            if (isset($post['ftp'])) {
                $pwd_ftp            = !empty($domain['pwd_ftp']) ? $blockCipher->decrypt($domain['pwd_ftp']) : '';
                $array_email['ftp'] = array(
                    'host_ftp' => $domain['host_ftp'],
                    'user_ftp' => $domain['user_ftp'],
                    'pwd_ftp' => $pwd_ftp,
                    'port_ftp' => $domain['port_ftp'],
                );
            }
 if (isset($post['email_info'])) {
                $array_email['email_info'] = array(
                    'fullname' => $domain['fullname'],
                    'url' => $domain['url']
                );
            }

            if (isset($post['account']) && is_array($post['account'])) {
                $cnt = 0;
                foreach ($post['account'] as $key => $value) {
                    $info_account = $this->getAccountModel()->find(array(
                        'a.id' => $value
                    ));

                    $pwd                          = !empty($info_account['pwd']) ? $blockCipher->decrypt($info_account['pwd']) : '';
                    $array_email['account'][$cnt] = array(
                        'name_type_account' => $info_account['name_type_account'],
                        'usermail' => $info_account['usermail'],
                        'pwd' => $pwd,
                        'info' => $info_account['info'],
                    );
                    $cnt++;
                }
            }

            if (isset($post['note'])) {
                $array_email['note'] = $post['note'];
            }

            $sm        = $this->getServiceLocator()->get('ServiceManager');
            $translate = $sm->get('ViewHelperManager')->get('translate');
            $setting   = $this->getSettingModel()->find(array(
                'id' => $this->WebStudioAuthentication()->getIdentity()
            ));

            $view = new ViewModel(array(
                'data' => $array_email,
            ));
            $view->setTerminal(true);
            $view->setTemplate(sprintf('Application/view/email/send_data_%s', $setting['language']));

            if ($this->getDemo()->getSendEmail() === true) {
                $this->mailerZF2()->send(array(
                    'to' => $post['email'],
                    'cc' => $post['usermail'],
                    'subject' => $translate('label_86', null, $setting['language']),
                ), $view);
            }

            $this->flashMessenger()->addMessage(array('success', 1));
            return $this->redirect()->toRoute('domain/default', array('action' => 'send', 'id' => $ID));
        }

        return new ViewModel(array(
            'demo' => $this->getDemo()->getShowMessage(),
            'domain' => $domain,
            'datalist' => $this->getCustomersModel()->getList(),
            'form' => $this->getSendForm(),
            'account' => $this->getAccountModel()->getList(array('a.id_domain' => $ID)),
            'message' => $this->flashMessenger()->getMessages()
        ));
    }
}

这是发送表格代码

<?php
namespace Application\Form;

use Zend\Form\Form;

/**
 * Class SendForm
 * @package Application\Form
 */
class SendForm extends Form
{
    /**
     * @param string $name
     */
    public function __construct($name = '')
    {
        parent::__construct($name);
        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'bottom-margin');
        $this->setAttribute('autocomplete', 'off');
        $this->setAttribute('id', 'validateForm');

        $this->add(array(
            'name' => 'email',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'class' => 'form-control',
                'required' => 'required',
                'list' => 'customers',
            ),
        ));

        $this->add(array(
            'name' => 'note',
            'type' => 'Zend\Form\Element\Textarea',
            'attributes' => array(
                'class' => 'form-control',
            )
        ));
    }
}

0 个答案:

没有答案