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