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

5 REPLIES 5

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. Get Magento insight from
Magento 2 Blogs/Tutorial

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

Re: email attachment from observer

It sounds like you are trying to create an observer in the Shopify platform that sends an email with an attachment when an order is successful. Here are the general steps you can follow to do this:

  1. Create a new observer in your Shopify theme's liquid files. You can do this by adding a new file to the /snippets directory of your theme and using the observe tag to specify the event you want to listen for (in this case, order_success).

  2. In the observer, use the ShopifyAPI:Smiley Surprisedrder class to retrieve the order details for the successful order. You can use the find method to retrieve the order by its ID.

  3. Use the ShopifyAPI:Smiley Surprisedrder object to access the order details and any other information you need for the email (such as the customer's email address). You can also use the line_items property of the ShopifyAPI:Smiley Surprisedrder object to access the items in the order.

  4. Use the ShopifyAPI:Smiley Frustratedhop class to retrieve the shop's email address and other information you need to send the email.

  5. Use the ShopifyAPI::Email class to create and send the email. You can use the create method to create a new email object and specify the recipient, subject, body, and any attachments you want to include. Then, use the deliver method to send the email.

It's important to note that you will need to have the shopify_api gem installed and properly configured in order to use the ShopifyAPI classes in your observer. You may also need to handle any errors or exceptions that might occur during the email sending process.

For more info you can visit- https://appokart.com/