cancel
Showing results for 
Search instead for 
Did you mean: 

email attachment from observer

email attachment from observer

Hello, using 2.1.7. I wrote an observer to catch order success, which is working. Now I need to send an email with attachment, trying this:

$transport = $this->transportBuilder
            ->setTemplateIdentifier('order_notify')
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            );
        $receiverInfo = [
            'name' => 'pippo',
            'email' => 'xxx@domain.it'
        ];
        $senderInfo = [
            'name' => 'giorg',
            'email' => 'ppp@domain.com',
        ];

        $transport->setTemplateVars(['data' => 'nonprende'])
            ->setFrom($senderInfo)
            ->addTo($receiverInfo);
        $transport = $transport->getTransport();
        $transport->sendMessage();

but how to add attachment?

thanks

4 REPLIES

Re: email attachment from observer

You need to create one custom function for attachment of file,

app/code/Rbj/Test/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<preference for="Magento\Framework\Mail\Template\TransportBuilder"
    type="Rbj\Test\Mail\Template\TransportBuilder" />
</config>

Now,

app/code/Rbj/Test/Mail/Template/TransportBuilder.php file,

<?php
namespace Rbj\Test\Mail\Template;

class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
    public function addAttachment(
        $body,
        \Zend_Mime::TYPE_OCTETSTREAM,
        \Zend_Mime::DISPOSITION_ATTACHMENT,
        \Zend_Mime::ENCODING_BASE64,
        $filename = 'attachment.pdf'
    ) {
        $this->message->createAttachment($body, $mimeType, $disposition, 
            $encoding, $filename);
        return $this;
    }
}

Now in your observer,

$pdfFilepath = 'pdf file location';
$transport = $this->transportBuilder
    ->setTemplateIdentifier('order_notify')
    ->setTemplateOptions(
        [
            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
        ]
    );
$receiverInfo = [
    'name' => 'pippo',
    'email' => 'xxx@domain.it'
];
$senderInfo = [
    'name' => 'giorg',
    'email' => 'ppp@domain.com',
];

$transport->setTemplateVars(['data' => 'nonprende'])
    ->setFrom($senderInfo)
    ->addTo($receiverInfo);
$transport->addAttachment(file_get_contents($pdfFilepath)); 
$transport = $transport->getTransport();
$transport->sendMessage();

if issue solved, Click Kudos/Accept as solutions.

If Issue Solved, Click Kudos/Accept As solutions.

Re: email attachment from observer

Hello Rakesh,

thanks for your answer. I did, but I get:

 

Fatal error: Uncaught Error: Call to undefined method MagePal\GmailSmtpApp\Model\Transport\Interceptor::addAttachment()

 

I forgot to mention there is this plugin installed: MagePal_GmailSmtpApp configured to use customer's SMTP, does this change anything?

Thanks

Re: email attachment from observer

hello please go through the website link. 

https://webkul.com/blog/attach-pdf-file-magento-2-email/

 

Re: email attachment from observer

hi,

went through that tutorial but I still get the same error...

thanks