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

seb
3,5723 gold badges26 silver badges57 bronze badges
asked Feb 23, 2016 at 12:56
1
  • Hi I am also facing same issue ,If you got the solution so can you please share with me. Thanks a lot 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();
answered Jul 22, 2016 at 13:23
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.