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 = "nehaseekhwal31@gmail.com";
            $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('*/*/');
    }
}
}
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
neha rani
  • 109
  • 10

1 Answers1

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 = "nehaseekhwal31@gmail.com";
                $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.

Sukumar Gorai
  • 10,823
  • 4
  • 19
  • 46
  • i try this code but my form not submit after use your code. and this is my phtml code [<form action="getUrl('brochure/index/post'); ?>" id="brochureform" class="contact-page-bg" method="post" enctype="multipart/form-data"

    ]


  •     <input type="file" name="uploaded_file">
        </li>
    
    – neha rani May 07 '19 at 04:53
  • This is controller action, I am not sure why form is not submitting. You need to upload the file first. – Sukumar Gorai May 07 '19 at 05:25
  • i want to send [ ] attached file in email only. please provied me code according to this html filed. – neha rani May 07 '19 at 05:27