Magento 1.9 – How to Send Email with PDF Attachment

attachmentemailmagento-1.9

I face one issue with createAttachment method. This method attached pdf but pdf not open and its size is zero kb.

     $html .= "
         <html>
         <head>
         </head>
         <body>
           Test pdf
         </body>
         </html>";

     $attachment = $emailTemp->getMail()->createAttachment($html);
     $attachment->type = 'application/pdf';
     $attachment->filename = 'test.pdf';
     $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
     $attachment->encoding = Zend_Mime::ENCODING_BASE64;

     $emailTemp->send($useremail,$Username,$emailTempVariables);

Please help me ASAP. what is wrong here?
I want to attached pdf with custom HTML in magento.

Best Answer

First you have to create a pdf file, You can create pdf file using below code, suggested code is just for create sample pdf file. you can change it as per your requirement.

$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
//var_dump($page);
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

define('FONT_SIZE'      , 12);
define('MARGIN_LEFT'    , 40);
define('MARGIN_TOP'     , 40);
define('COL_WIDTH'      , 100);
define('COL_LEFT_MARGIN', 10);
define('COL_SEPARATOR'  , '|');
define('ROW_HEIGHT'     , 20);

$row = 1;
$x = MARGIN_LEFT;
$y = MARGIN_TOP;

$page->setFont($font, FONT_SIZE);

$page->drawText('ABCD', $x, $page->getHeight() - $y);
$x += COL_WIDTH;
$x += COL_LEFT_MARGIN;
$x = MARGIN_LEFT;
$y += ROW_HEIGHT;

$pdf->pages[] = $page;
$pdf->save('data.pdf', true);

After generating PDF file, try with your attachment code with proper PDF file which you have created.

Related Topic