send mail with attached file

in magento there is no available methods for attach file. once we do it in magento by adding a following code
in  mage/core/model/email/template.php at the end of file.

public function addAttachment(Zend_Pdf $pdf){
        $file = $pdf->render();
        $attachment = $this->getMail()->createAttachment($file);
        $attachment->type = 'application/pdf';
    $attachment->filename = 'yourfile.pdf';
    }
   
but i prefer to use Zend_Mail to send mail with attached file.
for this u need to do following.

try{
            $mail = new Zend_Mail();
            $mail->setFrom("fromemail","fromname");
            $mail->addTo("toemail","toname");
            $mail->setSubject("subject");
            $mail->setBodyHtml(" body text"); // here u also use setBodyText options.
       
            // this is for to set the file format
            $content = file_get_contents("$file_path");
            $at = new Zend_Mime_Part($content);
           
            $at->type        = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
            $at->disposition = Zend_Mime::DISPOSITION_INLINE;
            $at->encoding    = Zend_Mime::ENCODING_8BIT;
            $at->filename    = $filename;
            $mail->addAttachment($at);
            $mail->send();
  
        }catch(Exception $e)
        {
            echo $e->getMassage();
           
        }


and this is ready.. just go for it.

hope this will help.

Comments

  1. just search the attachment email function for magento.
    followed your suggestion, found one bit your code was missing.

    Add
    $content = file_get_contents("$file_path");
    before
    $at = new Zend_Mime_Part($content);

    anything than that the function is working fine.
    Thanks

    ReplyDelete
    Replies
    1. Thanks Chine for your update.! I'd update the same into code..

      Delete
  2. Hi,

    Folowing Url Is Great Help For Send An Attachment With Registartion Email

    http://www.webslike.com/Thread-How-To-Send-An-Attachment-With-Registartion-Email-In-Magento

    ReplyDelete

Post a Comment

Popular posts from this blog

Magento Reindexing problem - take too long time

Magento Navigation menu of CMS pages

Dont want to merge old carts items with current quote