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'=>'anand@gmail.com', 'name'=>'anand'))
            ->addTo('anand.ontigeri@alshaya.com','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

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

Replace

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

with

$transport =$transportBuilder->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars($templateParams)
            ->setFrom(array('email'=>'anand@gmail.com', 'name'=>'anand'))
            ->addTo('anand.ontigeri@alshaya.com','anand.ontigeri')
            ->attachFile($path,'asd') //Add attachFile here
            ->getTransport();
        $transport->sendMessage();
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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