Magento发送包含PDF附件的交易电子邮件

时间:2013-06-18 08:30:14

标签: magento

我有以下代码,它似乎是通过magento模型发送交易电子邮件(针对特定模板ID)。

唯一无法让它发挥作用的是在交易电子邮件中附加PDF。

这是我到目前为止的代码:

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 = 'user@domain.com';
$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);

发送电子邮件时,附件文件名称似乎名为: ATT00001.pdf ,当您打开它时,它是一个空白页。

知道我在这里做错了吗?您是否通过Magento模型将PDF附加到交易电子邮件中?

2 个答案:

答案 0 :(得分:7)

在此处找到我的问题的解决方案:https://stackoverflow.com/a/10837774/2332336

以下是向交易电子邮件添加附件的正确代码段:

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)
        );
}

答案 1 :(得分:2)

// 1我在媒体目录中使用了一个请求引用文件夹作为 requestquote 来保存//上传的图像

// 2有一系列自定义变量要传递给交易电子邮件//电子邮件模板是在magento admin中创建的,其模板ID为3

//代码已经在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);