0

How can i send transactional mail with csv/excel attachment in magento 2? below is my code:

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
class UploadTransportBuilder extends TransportBuilder {
    public function __construct(FactoryInterface $templateFactory,
        MessageInterface $message,
        SenderResolverInterface $senderResolver,
        ObjectManagerInterface $objectManager,
        TransportInterfaceFactory $mailTransportFactory) {
        parent::__construct($templateFactory,
            $message,
            $senderResolver,
            $objectManager,
            $mailTransportFactory);    }
    public function attachFile($file, $name) {
        if (!empty($file) && file_exists($file)) {
            $this->message
            ->createAttachment(
                file_get_contents($file),
                \Zend_Mime::TYPE_OCTETSTREAM,
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                basename($name));
            return true;  }
 return false;
    }
}

and i send mail with attachment

$transportBuilder = $this->_objectManager->create('\Magento\Framework\Mail\Template\UploadTransportBuilder');
        $path='D:\asd.csv';
        //end here
        $templateId=1;
        $storeId=1;
        $templateParams['test']='test test';
        $transport =$transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom(array('email'=>'[email protected]', 'name'=>'anand'))
            ->addTo('[email protected]','anand.ontigeri')
            ->getTransport();
        $transport->attachFile($path,'asd');
        $transport->sendMessage();

Fatal error: Call to undefined method Magento\Framework\Mail\Transport\Interceptor::attachFile() in /var/www/magento2/html/magento2ee/script/Email.php on line 34

1
  • Hi I am also facing same issue ,If you got the solution so can you please share with me. Thanks a lot
    – konika
    Commented Jul 22, 2016 at 6:40

1 Answer 1

1

Replace

$transport =$transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom(array('email'=>'[email protected]', 'name'=>'anand'))
            ->addTo('[email protected]','anand.ontigeri')
            ->getTransport();
        $transport->attachFile($path,'asd');
        $transport->sendMessage();

with

$transport =$transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom(array('email'=>'[email protected]', 'name'=>'anand'))
            ->addTo('[email protected]','anand.ontigeri')
            ->attachFile($path,'asd') //Add attachFile here
            ->getTransport();
        $transport->sendMessage();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.