0

We have created custom form module. We don't want to save the attachment, but send it via mail. I have tried below code :

<?php
class Vendor_Module_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
    $this->loadLayout();
    $this->renderLayout();
}

public function postAction()
{
    $post = $this->getRequest()->getPost();

    if ($post){
        try {
            $name = $post["name"];
            $email = $post["email"];
            $address_line_1 = $post["address_line_1"]; 
            $phone_number = $post["phone_number"]; 
            $adhar_number = $post["adhar_number"]; 
            $pan_number = $post["pan_number"]; 
            /* $image=$_FILES["uploaded_file"]["name"]; */              
                    /* $target_path = "image/";  
                    $folder = $target_path.basename( $_FILES['fileToUpload']['name']);   
                    move_uploaded_file($_files["uploaded_file"]["tmp_name"],$folder);  */   

            $lone_type = $post["lone_type"]; 



            $to = "[email protected]";
            $subject = "Inquiry Form For Lone";
            $body .= "<p>The following customer have requested for lone.</p>";
            $body .= "<p>Name : " . $name . "<p>";
            $body .= "<p>Email  : " . $email . "<p>";
            $body .= "<p>Full Address : " . $address_line_1 . "<p>";
            $body .= "<p>Phone Card Number : " . $phone_number . "<p>";
            $body .= "<p>Adhar Card Number : " . $adhar_number . "<p>";
            $body .= "<p>Pan Card Number : " . $pan_number . "<p>";         
            $body .= "<p>Lone Type : " . $lone_type . "<p>";
            $body .= "<br/><p>Kind regards,</p><p>$name</p>";

            $from = $email;

            $mail = Mage::getModel('core/email');
            $mail->setToName($name);
            $mail->setToEmail($to);
            $mail->setBody($body);
            $mail->setSubject($subject);
            $mail->setFromEmail($from);
            $mail->setType('html');// YOu can use Html or text as Mail format

            $mail->send();

            Mage::getSingleton('core/session')->addSuccess('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.');
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('core/session')->addError('Unable to submit your request. Please, try again later');
            $this->_redirect('*/*/');
            return;
        }
    } else {
        $this->_redirect('*/*/');
    }
}
}
1

1 Answer 1

0

Try the below code:

<?php
class Vendor_Module_IndexController extends Mage_Core_Controller_Front_Action { 
    public function indexAction()
    { 
        $this->loadLayout(); $this->renderLayout(); 
    }

    public function postAction()
    {
        $post = $this->getRequest()->getPost();

        if ($post){
            try {
                $name = $post["name"];
                $email = $post["email"];
                $address_line_1 = $post["address_line_1"]; 
                $phone_number = $post["phone_number"]; 
                $adhar_number = $post["adhar_number"]; 
                $pan_number = $post["pan_number"]; 
                /* $image=$_FILES["uploaded_file"]["name"]; */              
                        /* $target_path = "image/";  
                        $folder = $target_path.basename( $_FILES['fileToUpload']['name']);   
                        move_uploaded_file($_files["uploaded_file"]["tmp_name"],$folder);  */   

                $lone_type = $post["lone_type"]; 



                $to = "[email protected]";
                $subject = "Inquiry Form For Lone";
                $body .= "<p>The following customer have requested for lone.</p>";
                $body .= "<p>Name : " . $name . "<p>";
                $body .= "<p>Email  : " . $email . "<p>";
                $body .= "<p>Full Address : " . $address_line_1 . "<p>";
                $body .= "<p>Phone Card Number : " . $phone_number . "<p>";
                $body .= "<p>Adhar Card Number : " . $adhar_number . "<p>";
                $body .= "<p>Pan Card Number : " . $pan_number . "<p>";         
                $body .= "<p>Lone Type : " . $lone_type . "<p>";
                $body .= "<br/><p>Kind regards,</p><p>$name</p>";

                $from = $email;

                $mail = Mage::getModel('core/email');
                $mail->setToName($name);
                $mail->setToEmail($to);
                $mail->setBody($body);
                $mail->setSubject($subject);
                $mail->setFromEmail($from);
                $mail->setType('html');// YOu can use Html or text as Mail format

                // Attachment Code
                $file       = file_path;
                $attachment = file_get_contents($file);
                $mail->createAttachment(
                    $attachment,
                    Zend_Mime::TYPE_OCTETSTREAM,
                    Zend_Mime::DISPOSITION_ATTACHMENT,
                    Zend_Mime::ENCODING_BASE64,
                    'attachment.pdf'
                );
                // Attachment Code

                $mail->send();

                Mage::getSingleton('core/session')->addSuccess('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.');
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('core/session')->addError('Unable to submit your request. Please, try again later');
                $this->_redirect('*/*/');
                return;
            }
        } else {
            $this->_redirect('*/*/');
        }
    }
}

First save it in particular folder before sending email and after email sending unlink that file from the desired folder location.

3
  • i try this code but my form not submit after use your code. and this is my phtml code [<form action="<?php echo $this->getUrl('brochure/index/post'); ?>" id="brochureform" class="contact-page-bg" method="post" enctype="multipart/form-data" >] <li class="col-md-6 col-sm-6 col-xs-12 form-group"><label>Attached File</label><br> <input type="file" name="uploaded_file"> </li>
    – neha rani
    May 7, 2019 at 4:53
  • This is controller action, I am not sure why form is not submitting. You need to upload the file first. May 7, 2019 at 5:25
  • i want to send [<input type="file" name="uploaded_file"> ] attached file in email only. please provied me code according to this html filed.
    – neha rani
    May 7, 2019 at 5: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.