2

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('[email protected]', '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('[email protected]', '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>
             
2
  • please confirm whether u r unable to send the mail? Jun 28, 2017 at 14:27
  • No no, everything works fine, it's just in the body of the email, I want to include the $params mentioned above but can't figure out how :/ Only 1 is visible in the emauil
    – Greg
    Jun 28, 2017 at 14:33

2 Answers 2

2

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('[email protected]', '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/');
}
21
  • Hi, that didn't work. I didn't send any information this time. Only blank email. setFrom didn't send any information too.
    – Greg
    Jun 29, 2017 at 8:27
  • Try to remove the s from getParams from $name, $email, $businessName, $businessAddress, $telephoneNumber like this $name = $this->getRequest()->getParam('name'); sorry i haven't test it look also if you have some errors, and you should have all fields in your form with the same name field !
    – PЯINCƎ
    Jun 29, 2017 at 8:31
  • After removing the s like you mentioned. The $mail->setFrom($email, $name); is now back working, however, there is nothing being sent in the $mail->setBodyText
    – Greg
    Jun 29, 2017 at 8:36
  • ok, try this: $mail->setBodyText($name, $email, $businessName, $businessAddress, $telephoneNumber);
    – PЯINCƎ
    Jun 29, 2017 at 8:37
  • Only $name variable came though to my email.
    – Greg
    Jun 29, 2017 at 8:43
2

you can add like html

$html = 'Your Name'.$params['name'];

$html .= 'Your Email'.$params['email'];

$html .= 'Your Business Name'.$params['business_name'];

$html .= 'Your Business Address'.$params['business_address'];

$html .= 'Your Telephone Number'.$params['telephone_number'];

$mail->setBodyText($html);

2
  • @GrzegorzWisniewski, The answer is missing . before <br /> tag. Please add it and check. Jun 29, 2017 at 9:16
  • try it now @Grzegorz Wisniewski Jun 29, 2017 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.