Magento – how to Send an EMail with attachement in custom module

magento-1.8php-5.4phpstorm

I'm new to magento I created custom module for customer quote request, In that after filling quote request we need to send an email. So that I wrote fallowing code.

I add the Add Quote button in below form.

design->frontentd->Test->default->template->catalog->product->list.phtml

<p><button type="button" title="<?php echo $this->__('Add to Quote') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Quote') ?></span></span></button></p>

when customer click I navigate the design->frontend->Test->default->template->checkout->cart->test.phtml file, this design code below.

<form action="<?php echo $this->getUrl('checkout/quote/sendMail') ?>" method="post">

    <div style="border-color: red;">
        <div class="page-title">
            <h1>Request For Quote</h1>
        <div class="fieldset">
            <h2 class="legend">RFQ Requirements</h2>
           <table>
               <tr>
                   <td>
                        Company Name :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="company" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Contact Name :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="contact_name" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Email :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="email" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Phone Number :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="phone" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Project Title :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="project_title" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Quote Needed By :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="date" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Project Budget Status :
                   </td>
                   <td>
                       <input style="font-size:14px;" name="budget_status" value="" class="input-text" type="text" size="29"/>
                   </td>
               </tr>
               <tr>
                   <td>
                        Brief Overview :
                   </td>
                   <td>
                       <textarea name="overview" id="overview" title="Brief Overview" class="input-text required-entry" style="font-size:14px;" name="overview" cols="55" rows="10" id="project_overview"></textarea>
                   </td>
               </tr>
               <tr>
                   <td colspan="2" align="right">
                       <p class="required">* Required Fields</p>
                       <button type="submit" title="Submit" class="button"><span><span>Send Request</span></span></button>
                   </td>
               </tr>
           </table>
        </div>
    </div>
  </div>
</form>

In app\code\core\Mage\Checkout\controllers I created QuoteController.php this code is below.

<?php

class Mage_Checkout_QuoteController extends Mage_Core_Controller_Front_Action
{
    function indexAction()
    {
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
        $this->getLayout()->getBlock('head')->setTitle($this->__('Request for Quote'));
        $this->renderLayout();
        Mage::log("Request for quote");
    }

    public function sendMailAction()
    {
        //send Mail
        Mage::log("Send Mail");
       $to="dotnet@gmail.com";
        $com_name = $_POST['company'];
        $contact_name = $_POST['contact_name'];
        $pro_title = $_POST['project_title'];
        $phone_tel = $_POST['phone'];
        $email = $_POST['email'];
        $date = $_POST['date'];
        $bud_status = $_POST['budget_status'];
        $comment = $_POST['overview'];

               $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

               $headers = "From:$email\r\n" .
                   "MIME-Version: 1.0\r\n" .
                   "Content-Type: multipart/mixed;\r\n" .
                   " boundary=\"{$mime_boundary}\"";

               $subjects = "Request For Quote " . "From : $email";

               $subject="Details From : $subjects";

               $message="Company Name: $com_name\r\n".
                   "Contact Name:  $contact_name\r\n".
                   "Mobile No.: $phone_tel\r\n".
                   "Email: $email\r\n".
                   "Project Title: $pro_title\r\n " .
                   "Estimated Date: $date\r\n " .
                   "Budget Status: $bud_status\r\n " .
                   "Comments : $comment\r\n";


               $message = "This is a multi-part message in MIME format.\r\n\r\n" .
                   "--{$mime_boundary}\r\n" .
                   "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" .
                   "Content-Transfer-Encoding: 7bit\r\n\r\n" .
                   $message . "\r\n\r\n";

               $message.="--{$mime_boundary}--\n";

               @mail($to, $subject, $message, $headers);

        Mage::log("Send Mail End");

    }

}

when customer hit the Send Request button I call the sendMailAction() action but the mail is not sending.

even though I configure the mail settings in admin panel also but it's not sending mail.

can anyone tell me where I went wrong & tell me the how to attach if any files is there?

Best Answer

Hello you can use the below code for attachment.

you can use zend and this code is tested.

Source : https://stackoverflow.com/questions/20904249/how-to-attach-a-csv-file-to-php-email-code-running-from-the-magento-root/20944539#20944539

require 'app/Mage.php';
umask( 0 );
Mage :: app( "default" );
$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setBodyHtml($html_body);
$mail->setFrom('support@example.com', 'Example');
$mail->addTo('dev.amitbera@gmail.com', 'Amit');
$mail->setSubject('Sending email using Zend Framework');
$dir = Mage::getBaseDir();
$path = $dir.DS.'var'.DS.'docs'.DS.'test.csv';
$file = $mail->createAttachment(file_get_contents($path));
$file ->type        = 'text/csv';
$file ->disposition = Zend_Mime::DISPOSITION_INLINE;
$file ->encoding    = Zend_Mime::ENCODING_BASE64;
$file ->filename    = 'test.csv';
$mail->send();



<?php
    $mail = new Zend_Mail();
    $mail->setType(Zend_Mime::MULTIPART_RELATED);
    $mail->setBodyHtml($html_body);
    $mail->setFrom('support@example.com', 'Example');
    $mail->addTo('dev.amitbera@gmail.com', 'Amit');
    $mail->setSubject('Sending email using Zend Framework');
    $dir = Mage::getBaseDir();
    $path = $dir.DS.'var'.DS.'docs'.DS.'test.csv';
    $file = $mail->createAttachment(file_get_contents($path));
    $file ->type        = 'text/csv';
    $file ->disposition = Zend_Mime::DISPOSITION_INLINE;
    $file ->encoding    = Zend_Mime::ENCODING_BASE64;
    $file ->filename    = 'test.csv';
    $mail->send();
?>
Related Topic