Magento / Adminhtml - saveAction()不向客户组中的用户发送电子邮件

时间:2015-06-08 16:29:40

标签: php magento

我正在为Magento 1.9.0.1开发自定义扩展程序。

这是我的Adminhtml表单:

<?php

class VivasIndustries_SmsNotification_Block_Adminhtml_Sms_Sendmass_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
    {

        public function _prepareLayout() 
           {
              $ExtensionPath = Mage::getModuleDir('js', 'VivasIndustries_SmsNotification'); 
              $head = $this->getLayout()->getBlock('head');
              $head->addJs('jquery.js');
              $head->addJs('vivas.js');

              return parent::_prepareLayout();
           }

        protected function _prepareForm()
            {
            $form = new Varien_Data_Form(array(
                                    'id' => 'edit_form',
                                    'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                                    'method' => 'post',
                                 ));

                $fieldset = $form->addFieldset('edit_form', array('legend'=>Mage::helper('smsnotification')->__('SMS Information')));

                $CustomerGroups = Mage::getResourceModel('customer/group_collection')->toOptionArray();

                $smsprice_value = Mage::getStoreConfig('vivas/smsprice/smsprice_value');
                $smsprice_tag = Mage::getStoreConfig('vivas/smsprice/smsprice_tag');

                $customerArray=array();
                foreach($CustomerGroups as $each){

                     $count=Mage::getResourceModel('customer/customer_collection')
                                ->addAttributeToFilter('group_id',$each['value'])->getSize();
                $SMSPrice = $count * $smsprice_value;               
                     $customerArray[]=array('value'=> $each['value'],'label'=> $each['label'].' - ('.$count.' Members) - ('.$SMSPrice.' '.$smsprice_tag.')');

                }

                $CustomerGroups = array_merge(array('' => ''), $customerArray);

                $fieldset->addField('customergroups', 'select',
                        array(
                            'name'      => 'customergroups',
                            'label'     => Mage::helper('smsnotification')->__('Customer Group'),
                            'class'     => 'required-entry',
                            'after_element_html' => '<br><small>If customer group is not selected the SMS will be sended<br> to all store members!</small>',
                            'values'    => $CustomerGroups
                        )
                    );


                $fieldset->addField('smstext', 'textarea', array(
                          'label'     => Mage::helper('smsnotification')->__('SMS Text'),
                          'class'     => 'required-entry',
                          'required'  => true,
                          'name'      => 'smstext',
                          'onclick' => "",
                          'onkeyup' => "CheckLetterSize(this)",
                          'after_element_html' => '<br><b style="color:brown;"><span id="charNum"></span><span id="charNum1"></span></b><br><small>SMS text must <b>NOT</b> be longer then 160 characters!</small>',
                          'tabindex' => 1
                        ));






                if ( Mage::getSingleton('adminhtml/session')->getsmsnotificationData() )
                    {
                        $form->setValues(Mage::getSingleton('adminhtml/session')->getsmsnotificationData());
                        Mage::getSingleton('adminhtml/session')->setsmsnotificationData(null);
                    } elseif ( Mage::registry('smsnotification_data') ) {
                        $form->setValues(Mage::registry('smsnotification_data')->getData());
                    }
                // Add these two lines


                $form->setUseContainer(true);
                $this->setForm($form);

                ////

                return parent::_prepareForm();
            }
    }

这是我的SendmassController.php:

<?php

class VivasIndustries_SmsNotification_Adminhtml_SendmassController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $smsnotificationId     = $this->getRequest()->getParam('id');
        $smsnotificationModel  = Mage::getModel('smsnotification/smsnotification')->load($smsnotificationId);

        if ($smsnotificationModel->getId() || $smsnotificationId == 0) {

            Mage::register('smsnotification_data', $smsnotificationModel);

            $this->loadLayout();
            $this->_setActiveMenu('vivassms');

            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);

            $this->_addContent($this->getLayout()->createBlock('smsnotification/adminhtml_sms_sendmass_edit'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('smsnotification')->__('SMS Rule does not exist'));
            $this->_redirect('*/*/');
        }
    }



    public function saveAction()
    {
        if ( $this->getRequest()->getPost() ) {
            try {
                $postData = $this->getRequest()->getPost();


                $smstext = $postData['smstext'];

                $groupId = $this->getRequest()->getPost('customergroups', '');
                if (!empty($groupId)) {
                    //Get customers from a group
                    $customers = Mage::getModel('customer/customer')
                                        ->getCollection()
                                        ->addAttributeToSelect('*')
                                        ->addFieldToFilter('group_id', $groupId);

                $customers = Mage::getModel('customer/customer')
                            ->getCollection()
                            ->addAttributeToSelect('*')
                            ->addFieldToFilter('group_id', $groupId);

                $email=array();
                foreach($customers as $customer)
                {
                    $email[]=$customer->getEmail();
                    $subject = "SMS Nntification test email";
                    $message = $smstext;


                        $headers = 'From: vivas@store.bg' . "\r\n" .
                                    'MIME-Version: 1.0' . "\r\n" .
                                    'Content-type: text/html; charset=UTF-8' . "\r\n" .                         
                                    'Reply-To: vivas@store.bg' . "\r\n" .
                                    'X-Mailer: PHP/' . phpversion();

                    $subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";               

                    mail($email, $subject, $message, $headers);
                }

                } else {
                    //Get all customers
                    $customers = Mage::getModel('customer/customer')
                                        ->getCollection()
                                        ->addAttributeToSelect('*');

                    $email=array();
                    foreach($customers as $customer)
                    {
                        $email[]=$customer->getEmail();
                        $subject = "SMS Nntification test email";
                        $message = $smstext;


                            $headers = 'From: vivas@store.bg' . "\r\n" .
                                        'MIME-Version: 1.0' . "\r\n" .
                                        'Content-type: text/html; charset=UTF-8' . "\r\n" .                         
                                        'Reply-To: vivas@store.bg' . "\r\n" .
                                        'X-Mailer: PHP/' . phpversion();

                        $subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";               

                        mail($email, $subject, $message, $headers);
                    }                   
                }



                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('SMS Rule was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setsmsnotificationData(false);

                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setsmsnotificationData($this->getRequest()->getPost());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }

        $this->_redirect('*/*/');
    }

    public function deleteAction()
    {
        if( $this->getRequest()->getParam('id') > 0 ) {
            try {
                $smsnotificationModel = Mage::getModel('smsnotification/smsnotification');

                $smsnotificationModel->setId($this->getRequest()->getParam('id'))
                    ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('SMS Rule was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }
    /**
     * Product grid for AJAX request.
     * Sort and filter result for example.
     */
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
               $this->getLayout()->createBlock('smsnotification/adminhtml_smsnotification_grid')->toHtml()
        );
    }
}

当我提交表单时,它不会发送任何电子邮件。你能在代码中的任何地方看到问题吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您必须包含发件人电子邮件,并使用以下代码从magento管理员电子邮件地址获取

<?php 

$fromemail=Mage::getStoreConfig('trans_email/ident_support/email');
mail($fromemail,$email, $subject, $message, $headers);

?>