Latheesan
Latheesan

Reputation: 24116

Magento Send transactional email with PDF Attachment

I have the following code, which appears to be sending transactional email (for a specific template id) via magento models.

The only bit I can't get it to work is the attachment of PDF on the transactional email.

This is the code I have so far:

function GetTransactionalSender() {
    return array(
        'name'  => Mage::getStoreConfig('trans_email/ident_support/name'),
        'email' => Mage::getStoreConfig('trans_email/ident_support/email')
    );
}

function SendTransactionalEmail($templateId, $recepientEmail, $recepientName, $vars, $storeId, $pdf_attachment = '') {
    $status = false;
    try {
        $transactionalEmail = Mage::getModel('core/email_template')
            ->setDesignConfig(array('area' => 'frontend', 'store' => $storeId));;
        if (!empty($pdf_attachment) && file_exists($pdf_attachment)) {
            $transactionalEmail
                ->getMail()
                ->createAttachment(file_get_contents($pdf_attachment), 'application/pdf')
                ->filename = basename($pdf_attachment);
        }
        $transactionalEmail
            ->sendTransactional($templateId, GetTransactionalSender(), $recepientEmail, $recepientName, $vars);
        $status = true;
    } catch (Exception $e) { }
    return $status;
}

$template_id = 1;
$to_email = '[email protected]';
$to_name = 'User Name';
$vars = array();
$store_id = 1
$pdf_file = '/full/path/to/my/file.pdf';
$sent = SendTransactionalEmail($template_id, $to_email, $to_name, $vars, $store_id, $pdf_file);

When the email is sent, the attachment file name appears to be named : ATT00001.pdf and when you open it, it's a blank page.

Any idea what I am doing wrong here? Do you attach a PDF onto a transactional email via Magento models?

Upvotes: 2

Views: 15345

Answers (2)

Syed Nazrul Hassan
Syed Nazrul Hassan

Reputation: 31

//1 I used a request quote folder as requestquote in media directory for saving // uploaded images

//2 There is an array of custom variables to be passed to transactional email //email template was created in magento admin and its template id 3

//Code has been tested on Magento 1.9.1.0

$uploadfilename = '';
if( !empty($_FILES["rfloorplanattachment"]["name"])  )
{
    $image_ext = end(explode('.',$_FILES["rfloorplanattachment"]["name"]));
    $allowed_ext =  array('gif','png' ,'jpg','jpeg','pdf','doc','docx','rtf','odt');

    $uploadfilename = md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,100).rand(1,100))).str_replace(" ","_",$_FILES["rfloorplanattachment"]["name"]); 
    $source_upl         = $_FILES["rfloorplanattachment"]["tmp_name"];
    $target_path_upl = Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename;  
    if(in_array($image_ext ,$allowed_ext ) ) {
        @move_uploaded_file($source_upl, $target_path_upl);
    }
}


$senderName = Mage::getStoreConfig('trans_email/ident_general/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$templateId = 3;
$sender = Array('name' => $senderName,'email' => $senderEmail);


$requestquotesvars = array(
            'firmname'     =>  $customer->getFirstname()
        );


$emaiName = 'Request Quote Firms';

$storeId = Mage::app()->getStore()->getId();

$translate = Mage::getSingleton('core/translate');
$transactionalEmail = Mage::getModel('core/email_template');
if(file_exists(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename) )
{
$transactionalEmail->getMail()
                ->createAttachment(
        file_get_contents(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename),
        Zend_Mime::TYPE_OCTETSTREAM,
        Zend_Mime::DISPOSITION_ATTACHMENT,
        Zend_Mime::ENCODING_BASE64,
        basename($uploadfilename)
    );
}
$transactionalEmail->sendTransactional($templateId, $sender, $companymail, $emailName, $requestquotesvars, $storeId);
$translate->setTranslateInline(true);

   unlink(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename);

Upvotes: 2

Latheesan
Latheesan

Reputation: 24116

Found the solution to my issue here: https://stackoverflow.com/a/10837774/2332336

Here's the correct snippet to add attachment to transactional email:

if (!empty($pdf_attachment) && file_exists($pdf_attachment)) {
    $transactionalEmail
        ->getMail()
        ->createAttachment(
            file_get_contents($pdf_attachment),
            Zend_Mime::TYPE_OCTETSTREAM,
            Zend_Mime::DISPOSITION_ATTACHMENT,
            Zend_Mime::ENCODING_BASE64,
            basename($pdf_attachment)
        );
}

Upvotes: 7

Related Questions