1

I had created the dropdown list with fields Lab,Pharmacy,Food and Nurse.When I select the lab it generates the new fields and when I select the pharmacy it generates the new field with upload button.I need to send the mail with these details for lab and pharmacy.

By using below mail function I can send only for lab but when I select pharmacy I could not send any mail.Please help me

form.phtml

    <form action="http://www.labwise.in/devel/serviceform/submit" id="quickservice" method="post"  enctype="multipart/form-data">
   <div class="fieldset">
    <ul class="form-list">
        <li>
        <label for="Servicetype" class="required"><em>*</em><?php echo $this->__('Service Type') ?></label>
            <div class="input-box">
                <select id='servicetype'   onchange="showHidden(this)" name="servicetype" class="input-text required-entry validate-select" style="width: 200px;" />
                        <option value=""><?php echo $this->__('--Please Select--')?></option>
                        <option value="Lab" id='lab'><?php echo $this->__('Lab')?></option>
                        <option value="Pharmacy"><?php echo $this->__('Pharmacy')?></option>
                        <option value="Food"><?php echo $this->__('Food')?></option>
                        <option value="Gym"><?php echo $this->__('Gym')?></option>
                        <option value="Physio"><?php echo $this->__('Physio')?></option>
                        <option value="Nurse"><?php echo $this->__('Nurse')?></option>
                </select>
            </div>
        </li>
    </ul>
    <div id="fileToUpload" style='display: none;'>

        <ul class="form-list">
        <li class="fields">
            <label for="medicinename" ><?php echo $this->__('Medicine Name') ?> </label>
                <div class="input-box">
                    <input type="text" id="medicinename" name="medicinename" value="" class="input-text" title="<?php echo $this->__('Medicine Name') ?>"/>
                </div>
        </li>
        <input type="file" name="fileToUpload" />
                    <p>(upload your prescription )</p>
        <div class="fields">
        <li class="fields">
            <label for="pname" class="required"><em>*</em><?php echo $this->__('Name') ?> </label>
                <div class="input-box">
                    <input type="text" id="pname" name="pname" value="" class="input-text required-entry" title="<?php echo $this->__('Name') ?>"/>
                </div>
        </li>

        <li class="fields">
            <label for="email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                <div class="input-box">
                    <input type="text" name="email" value="<?php echo $this->escapeHtml($this->getUsername()) ?>" id="email" class="input-text required-entry validate-email" title="<?php echo $this->__('Email Address') ?>" />
                </div>
        </li>
        <li class="fields">
        <div class="field">
                <label for="mobile" class="required"><em>*</em><?php echo $this->__('Mobile Number') ?> </label>
                        <div class="input-box">
                            <input type="text" id="mobile" name="mobile" value="" class="input-text required-entry validate-mobileno" title="<?php echo $this->__('Mobile Number') ?>" />
                        </div>
            </div>
        </li>
        <li class="fields">
            <label for="pcity" class="required"><em>*</em><?php echo $this->__('City') ?> </label>
                <div class="input-box">
                    <input type="text" id="pcity" name="pcity" value="" class="input-text required-entry" title="<?php echo $this->__('City') ?>"/>
                </div>
        </li>
        <li class="fields">

                <label for="zip_code" class="required"><em>*</em><?php echo $this->__('Zip Code') ?></label>
                <div class="input-box">
                    <input name="zip_code" id="zip_code" title="<?php echo $this->__('Zip Code') ?>" value="" class="input-text required-entry" type="text" />
                </div>

        </li>

        </ul>
        </div>
    </div>
    <div id="city_name" style='display: none;'>
        <ul class="form-list">
        <li class="fields">
            <label for="testname" class="required"><em>*</em><?php echo $this->__('Testname') ?> </label>
                <div class="input-box">
                    <input type="text" id="testname" name="testname" value="" class="input-text required-entry" title="<?php echo $this->__('Testname') ?>"/>
                </div>
        </li>

        <li class="fields">
            <label for="name" class="required"><em>*</em><?php echo $this->__('Name') ?> </label>
                <div class="input-box">
                    <input type="text" id="name" name="name" value="" class="input-text required-entry" title="<?php echo $this->__('Name') ?>"/>
                </div>
        </li>

        <li class="fields">
            <label for="email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                <div class="input-box">
                    <input type="text" name="email" value="<?php echo $this->escapeHtml($this->getUsername()) ?>" id="email" class="input-text required-entry validate-email" title="<?php echo $this->__('Email Address') ?>" />
                </div>
        </li>
        <li class="fields">
        <div class="field">
                <label for="mobile" class="required"><em>*</em><?php echo $this->__('Mobile Number') ?> </label>
                        <div class="input-box">
                            <input type="text" id="mobile" name="mobile" value="" class="input-text required-entry validate-mobileno" title="<?php echo $this->__('Mobile Number') ?>" />
                        </div>
            </div>
        </li>
        <li class="fields">
            <label for="rcity" class="required"><em>*</em><?php echo $this->__('City') ?> </label>
                <div class="input-box">
                    <input type="text" id="rcity" name="rcity" value="" class="input-text required-entry" title="<?php echo $this->__('City') ?>"/>
                </div>
        </li>
        <li class="fields">

                <label for="zip_code" class="required"><em>*</em><?php echo $this->__('Zip Code') ?></label>
                <div class="input-box">
                    <input name="zip_code" id="zip_code" title="<?php echo $this->__('Zip Code') ?>" value="" class="input-text required-entry" type="text" />
                </div>

        </li>

        </ul>
    </div>
    <div id="food" style='display: none;'>
    <ul class="form-list">

        <li class="fields">
            <label for="fname" class="required"><em>*</em><?php echo $this->__('Name') ?> </label>
                <div class="input-box">
                    <input type="text" id="fname" name="fname" value="" class="input-text required-entry" title="<?php echo $this->__('Name') ?>"/>
                </div>
        </li>

        <li class="fields">
            <label for="email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                <div class="input-box">
                    <input type="text" name="email" value="<?php echo $this->escapeHtml($this->getUsername()) ?>" id="email" class="input-text required-entry validate-email" title="<?php echo $this->__('Email Address') ?>" />
                </div>
        </li>
        <li class="fields">
        <div class="field">
                <label for="mobile" class="required"><em>*</em><?php echo $this->__('Mobile Number') ?> </label>
                        <div class="input-box">
                            <input type="text" id="mobile" name="mobile" value="" class="input-text required-entry validate-mobileno" title="<?php echo $this->__('Mobile Number') ?>" />
                        </div>
            </div>
        </li>
        <li class="fields">
            <label for="fcity" class="required"><em>*</em><?php echo $this->__('City') ?> </label>
                <div class="input-box">
                    <input type="text" id="fcity" name="fcity" value="" class="input-text required-entry" title="<?php echo $this->__('City') ?>"/>
                </div>
        </li>
        <li class="fields">

                <label for="zip_code" class="required"><em>*</em><?php echo $this->__('Zip Code') ?></label>
                <div class="input-box">
                    <input name="zip_code" id="zip_code" title="<?php echo $this->__('Zip Code') ?>" value="" class="input-text required-entry" type="text" />
                </div>

        </li>

        </ul>
    </div>

    </div>


    <div class="button">
        <p class="required"><?php echo $this->__('* Required Fields') ?></p>
        <button id= "submit"  type="submit" class="button" title="<?php echo $this->__('Submit') ?>" name="send" id="send2"><span><span><?php echo $this->__('Submit') ?></span></span></button>
    </div>
    </form>

      <script type="text/javascript">
    //<![CDATA[

if(Validation) {
    Validation.addAllThese([
    ['validate-mobileno','Enter correct mobile number (Eg:9986858483)',
    function(v){
    //var timePat ="^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$";

    var timePat ="^{0,1}9[0-9](\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$";
    // var matchArray = v.match(timePat);
    if(v.length > 0){
    if(v.length !=10){
        return false;
       }else if(v[0]!=9 && v[0]!=8 && v[0]!=7){

        return false;
       }


    return true;

    }else {
    return false;
    }

    }
    ]])};

        var contactForm = new VarienForm('quickservice', true);
      //]]>
     </script>

    <script>
    function showHidden(elem){
       if(elem.value == 'Pharmacy')
       {
          document.getElementById('fileToUpload').style.display = "block";
          document.getElementById('city_name').style.display = "none";
          document.getElementById('food').style.display = "none";
         // document.getElementById('gym').style.display = "none";
         // document.getElementById('physio').style.display = "none";
          //document.getElementById('nurse').style.display = "none";
       }
       else if(elem.value == 'Lab')
       {
          document.getElementById('city_name').style.display = "block";
          document.getElementById('fileToUpload').style.display = "none";
          document.getElementById('food').style.display = "none";
         // document.getElementById('gym').style.display = "none";
         // document.getElementById('physio').style.display = "none";
         // document.getElementById('nurse').style.display = "none";
       }
       else if(elem.value == 'Food')
       {
          document.getElementById('fileToUpload').style.display = "none";
          document.getElementById('city_name').style.display = "none";
          document.getElementById('food').style.display = "block";
         // document.getElementById('gym').style.display = "none";
         //  document.getElementById('physio').style.display = "none";
         // document.getElementById('nurse').style.display = "none";
       }
       else if(elem.value == 'Gym')
       {
           document.getElementById('fileToUpload').style.display = "none";
          document.getElementById('city_name').style.display = "none";
          document.getElementById('food').style.display = "block";
          //document.getElementById('gym').style.display = "block";
        //  document.getElementById('physio').style.display = "none";
         /// document.getElementById('nurse').style.display = "none";
       }
       else if(elem.value == 'Physio')
       {
           document.getElementById('fileToUpload').style.display = "none";
           document.getElementById('city_name').style.display = "none";
           document.getElementById('food').style.display = "block";
          // document.getElementById('gym').style.display = "none";
          // document.getElementById('physio').style.display = "block";
          // document.getElementById('nurse').style.display = "none";
       }
       else if(elem.value == 'Nurse')
       {
           document.getElementById('fileToUpload').style.display = "none";
           document.getElementById('city_name').style.display = "none";
           document.getElementById('food').style.display = "block";
           //document.getElementById('gym').style.display = "none";
          // document.getElementById('physio').style.display = "none";
          // document.getElementById('nurse').style.display = "block";
       }
       else
       {
           document.getElementById('fileToUpload').style.display = "none";
           document.getElementById('city_name').style.display = "none";
           document.getElementById('food').style.display = "none";
           //document.getElementById('gym').style.display = "none";
          // document.getElementById('physio').style.display = "none";
           //document.getElementById('nurse').style.display = "none";
       }
    }
    </script>

mail.phtml

 <?php
//Admin Mail
      function sendMailAction(){
       //Mage::getModel('sales/order')->load(24999);
     $servicetype=$_POST['servicetype'];
    $email=$_POST['email'];
      $testname=$_POST['testname'];
     $name=$_POST['name'];
     $mobile=$_POST['mobile'];
    $city=$_POST['rcity'];


   $html=
 ' <p>Service Type:'.$servicetype.'</p>
    <p>Testname:'.$testname.' </p>
     <p>Name: '.$name.'</p>
      <p>Email : '.$email.'</p>
      <p>Mobile Number: '.$mobile.'</p>
     <p>City: '.$city.'</p>'


     ;
     $mail = Mage::getModel('core/email');
     $mail->setToName('admin');
    $mail->setToEmail($email);
   $mail->setBody($html);
    $mail->setSubject('Quick Service');
    $mail->setFromEmail('admin@labwise.in');
      $mail->setFromName("Labwise");
  //$mail->addBcc("admin@labwise.in");
    $mail->setType('html');// YOu can use Html or text as Mail format

   try {
   $mail->send();
     //Mage::getSingleton('core/session')->addSuccess('Your Order is successfully Completed');
    //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
    //return '<h2>Your Order has been submitted. Thank you for using labwise.   </h2>';
    //$this->_redirect('');
     }
    catch (Exception $e) {
   //Mage::getSingleton('core/session')->addError('Unable to send.');
   //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
    //return '</h2>Unable to submit.</h2>'; 
    //$this->_redirect('');
         }
         }
      //Customer Mail

         function sendMailcustomerAction(){
         //Mage::getModel('sales/order')->load(24999);
          $email=$_POST['email'];
           $testname=$_POST['testname'];
           $name=$_POST['name'];
           $mobile=$_POST['mobile'];
            $city=$POST_['city'];

             $html=
            'Dear '.$name.',

               <p>We have received your order for '.$testname.'. You will be attended shortly.</p>

              <p>Best Regards,</p>
               <p>Team Labwise.</p>'

       ;
        $mail = Mage::getModel('core/email');
        $mail->setToName($name);
        $mail->setToEmail($email);
           $mail->setBody($html);
        $mail->setSubject('Quick Service');
         $mail->setFromEmail('admin@labwise.in');
        $mail->setFromName("Labwise");
        //$mail->addBcc("contact@labwise.in");
        $mail->setType('html');// YOu can use Html or text as Mail format

   try {
    $mail->send();
       //Mage::getSingleton('core/session')->addSuccess('Your Order is    successfully Completed');
       //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
         return '<h2>Your Request has been submitted.Thank you for using labwise.   </h2>';
        //$this->_redirect('');
        }
       catch (Exception $e) {
      //Mage::getSingleton('core/session')->addError('Unable to send.');
    //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
     return '</h2>Unable to submit.</h2>';
    //$this->_redirect('');
       }
        }

  //pharmacy

   $connectionresource = Mage::getSingleton('core/resource');
  $readconnection = $connectionresource->getConnection('core_read');  
   $query="SELECT *
 FROM mg_serviceprovider  where islab='1' or ispharmacy='1'";
  $alldata =$readconnection->fetchAll($query);

 foreach($alldata as $data)
   {
     if($_POST['servicetype']=='Pharmacy') { 
         sendMailpharmacyandlabAction($data['email'],$data['name']);
  } else {
  sendMailserviceAction($data['email'],$data['name']);
  }
  }
 function sendMailpharmacyandlabAction($cemail,$pname){
 //Mage::getModel('sales/order')->load(24999);
 $cemail=$_POST['email'];

       if( !empty($_FILES["fileToUpload"]["name"])  and $servicetype=='Pharmacy')
       {

        $image_ext = end(explode('.',$_FILES["fileToUpload"]["name"]));
        $allowed_ext =  array('gif','png' ,'jpg','jpeg','pdf','doc','docx','rtf','odt');

          $uploadfilename = md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,100).rand(1,100))).str_replace(" ","_",$_FILES["fileToUpload"]["name"]); 
          $source_upl         = $_FILES["fileToUpload"]["tmp_name"];
          $target_path_upl =  Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename;  
    if(in_array($image_ext ,$allowed_ext ) ) {
        @move_uploaded_file($source_upl, $target_path_upl);
    }
    }

   $html=
  '<p>Hi,</p>'

    ;

       $mail = Mage::getModel('core/email');
        $mail->setToName($pname);
      $mail->setToEmail($cemail);
    $mail->setBody($html);
    $mail->setSubject('Blood Donor');
   $mail->setFromEmail('admin@labwise.in');
    $mail->setFromName("Labwise");
       if(file_exists(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename) )
      {
      $mail->createAttachment(
          file_get_contents(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename),
        Zend_Mime::TYPE_OCTETSTREAM,
        Zend_Mime::DISPOSITION_ATTACHMENT,
        Zend_Mime::ENCODING_BASE64,
        basename($uploadfilename)
    );
    }
    //$mail->addBcc("admin@labwise.in");
    $mail->setType('html');// YOu can use Html or text as Mail format

   try {
     $mail->send();


  unlink(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename); 
    //Mage::getSingleton('core/session')->addSuccess('Your Order is successfully Completed');
     //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
   return '<h2>Your Order has been submitted. Thank you for using labwise.   </h2>';
     //$this->_redirect('');
    }
  catch (Exception $e) {
  //Mage::getSingleton('core/session')->addError('Unable to send.');
  //Mage::app()->getResponse()->setRedirect(Mage::getBaseUrl());
  return '</h2>Unable to submit.</h2>'; 
 //$this->_redirect('');
   }
   }
  echo sendMailpharmacyandlabAction();
  echo sendMailserviceAction();
  echo sendMailcustomerAction();
  echo sendMailAction();

   ?>
0

There is no method createAttachment in Mage_Core_Model_Email. Try to use Zend_Mail instead. As addition, my advice is to refactor your code following Magento extension standard. Most of your php code should be put to controller and block; you need to add screening while using variables from $_POST array to avoid XSS vulnerabilities.

  • Can you please suggest me.How to put to controller and block – Annaram Ravinder Dec 8 '15 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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