Magento Send email easily using Zend_Mail with attached file


/ Published in: PHP
Save to your folder(s)

send mail with attached file
examples: http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Mail/MailTest.php


Copy this code and paste it in your HTML
  1. in magento there is no available methods for attach file. once we do it in magento by adding a following code
  2. in mage/core/model/email/template.php at the end of file.
  3.  
  4. public function addAttachment(Zend_Pdf $pdf){
  5. $file = $pdf->render();
  6. $attachment = $this->getMail()->createAttachment($file);
  7. $attachment->type = 'application/pdf';
  8. $attachment->filename = 'yourfile.pdf';
  9. }
  10.  
  11. but i prefer to use Zend_Mail to send mail with attached file.
  12. for this u need to do following.
  13.  
  14. try{
  15. $mail = new Zend_Mail();
  16. $mail->setFrom("fromemail","fromname");
  17. $mail->addTo("toemail","toname");
  18. $mail->setSubject("subject");
  19. $mail->setBodyHtml(" body text"); // here u also use setBodyText options.
  20.  
  21. // this is for to set the file format
  22. $at = new Zend_Mime_Part($content);
  23.  
  24. $at->type = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
  25. $at->disposition = Zend_Mime::DISPOSITION_INLINE;
  26. $at->encoding = Zend_Mime::ENCODING_8BIT;
  27. $at->filename = $filename;
  28. $mail->addAttachment($at);
  29. $mail->send();
  30.  
  31. }catch(Exception $e)
  32. {
  33. echo $e->getMassage();
  34.  
  35. }

URL: http://mehtagaurav.blogspot.com/2010/08/send-mail-with-attached-file.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.