Magento 1.9 – Custom Module Form with Zend_Mail and Additional Params

contact-formemailmagento-1.9zend-framework

I created a simple custom module and have a problem with $parmas which I can't figure out for hours now.

Here is the IndexController.php responsible for it:

        public function sendemailAction()
        {
            //Fetch submited params
            $params = $this->getRequest()->getParams();
     
            $mail = new Zend_Mail();
            $mail->setBodyText($params['name']);
            $mail->setFrom($params['email'], $params['name']);
            $mail->addTo('greg@domain.co.uk', 'Some Recipient');
            $mail->setSubject('Test Wholesale_SimpleContact Module');
            try {
                $mail->send();
            }        
            catch(Exception $ex) {
                Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Wholesale_SimpleContact.');
     
            }
     
            //Redirect back to index action of (this) wholesale-simplecontact controller
            $this->_redirect('wholesale-simplecontact/');
        }

In the $mail->setBodyText($params['name']); I need more $params to be included. These are: name email business_name business_address telephone_number.

I tried many different things such as:

        $mail->setBodyText($params['name'], $params['email'], $params['business_name'], $params['business_address'], $params['telephone_number']);

But no luck. They all work individually so they are set correctly. I just can't get them to be sent together.

UPDATED CONTROLLER:

        public function sendemailAction() {
            $params = $this->getRequest()->getParams();
            $name = $this->getRequest()->getParam('name');
            $email = $this->getRequest()->getParam('email');
            $businessName = $this->getRequest()->getParam('business_name');
            $businessAddress = $this->getRequest()->getParam('business_address');
            $telephoneNumber = $this->getRequest()->getParam('telephone_number');
            $allParams = $name;
            $allParams .= $email;
            $allParams .= $businessName;
            $allParams .= $businessAddress;
            $allParams .= $telephoneNumber;
            $body = $name.' '.$email.' '.$businessName.' '.$businessAddress.' '.$telephoneNumber
            $mail = new Zend_Mail();
            $mail->setBodyText($body)
            $mail->setFrom($email, $name);
            $mail->addTo('greg@domain.co.uk', 'Some Recipient');
            $mail->setSubject('Test Wholesale_SimpleContact Module');
            try {
                $mail->send();
            }     
            catch(Exception $ex) {
                Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Wholesale_SimpleContact.');
            }
            //Redirect back to index action of (this) wholesale-simplecontact controller
            $this->_redirect('wholesale-simplecontact/');
        }

phtml file:

            <div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
            <div class="page-title">
                <h1><?php echo Mage::helper('contacts')->__('Wholesale Contact') ?></h1>
            </div>

            <form id="contactForm" name="simple_contact_form" action="<?php echo $this->getUrl('wholesale-simplecontact/') ?>index/sendemail" method="post">

                          <div class="fieldset">
                            <fieldset class="group-select">
                                <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
                                <ul>
                                <li class="fields">
                                        <div class="field">
                                            <label for="name">Name <span class="required"><em>*</em></span></label><br />
                         
                                            <input name="name" id="name" title="Name" value="" class="required-entry input-text" type="text" />
                                        </div>
                         
                                        <div class="input-box">
                                            <label for="email">Email <span class="required"><em>*</em></span></label><br />
                                            <input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email" type="text" />
                                        </div>
                                        
                                        <div class="input-box">
                                            <label for="business_name">Business Name <span class="required"><em>*</em></span></label><br />
                                            <input name="business_name" id="business_name" title="Business Name" value="" class="required-entry input-text" type="text" />
                                        </div>
                                        
                                        <div class="input-box">
                                            <label for="business_address">Business Address <span class="required"><em>*</em></span></label><br />
                                            <input name="business_address" id="business_address" title="Business Address" value="" class="required-entry input-text" type="text" />
                                        </div>
                                        
                                        <div class="input-box">
                                            <label for="telephone_number">Telephone Number <span class="required"><em>*</em></span></label><br />
                                            <input name="telephone_number" id="telephone_number" title="Telephone Number" value="" class="required-entry input-text" type="text" />
                                        </div>
                         
                                        <div class="clear"></div>
                         
                                        <div class="input-box">
                                            <label for="comment">Comment</label><br />
                         
                                            <textarea name="comment" id="comment" title="Comment" class="required-entry input-text" style="height:100px;" cols="50" rows="3"></textarea>
                                        </div>
                                </li>
                                        
                                </ul>
                            </fieldset>
                            </div>
                
                
                <div class="button-set">
                    <p class="required">* Required Fields</p>
                    <button style="float: right; margin-bottom: 10px;" class="button" type="submit"><span>Submit <br></span></button>
                    <p></p>
                </div>
            </form>
             

Best Answer

Try this:

<!-- I haven't tested it but tell me about a result-->
public function sendemailAction() {
    $params = $this->getRequest()->getParams();
    $name = $this->getRequest()->getParam('name');
    $email = $this->getRequest()->getParam('email');
    $businessName = $this->getRequest()->getParam('business_name');
    $businessAddress = $this->getRequest()->getParam('business_address');
    $telephoneNumber = $this->getRequest()->getParam('telephone_number');
    $body = $name.' '.$email.' '.$businessName.' '.$businessAddress.' '.$telephoneNumber;
    $mail = new Zend_Mail();
    $mail->setBodyText($body);
    $mail->setFrom($email, $name);
    $mail->addTo('greg@domain.co.uk', 'Some Recipient');
    $mail->setSubject('Test Wholesale_SimpleContact Module');
    try {
        $mail->send();
    }     
    catch(Exception $ex) {
        Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Wholesale_SimpleContact.');
    }
    //Redirect back to index action of (this) wholesale-simplecontact controller
    $this->_redirect('wholesale-simplecontact/');
}
Related Topic