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
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.
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
hi,
went through that tutorial but I still get the same error...
thanks
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:
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).
In the observer, use the ShopifyAPI:rder class to retrieve the order details for the successful order. You can use the find method to retrieve the order by its ID.
Use the ShopifyAPI:rder 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:rder object to access the items in the order.
Use the ShopifyAPI:hop class to retrieve the shop's email address and other information you need to send the email.
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/