2

How can I send email with attachment in magento 2.3.I already tried the way of custom TransportBuilder class from many answer but not working at all.I'm new to magento pls help me.

Shan Atif
1471 silver badge14 bronze badges
asked May 9, 2019 at 4:32
1
  • 1
    can you share your code? Commented May 9, 2019 at 4:37

2 Answers 2

0

Finally I found the solution after two day.Here I found the module for magento 2.3 email attachment for anyone who face the problem like me.

Module Repo https://github.com/extait-com/email-attachment

Tutorial https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/

Thanks Extait

answered May 9, 2019 at 6:57
0

For Override TransportBuilder.php file in Magento 2, you need to create di.xml file.

Using di.xml file you can override any PHP class in Magento 2.

Create di.xml in your module,

app/code/Namespace/Modulename/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">
 <!-- Mail sending with attachment -->
 <preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="Namespace\Modulename\Model\Mail\Template\TransportBuilder" />
</config>

Create TransportBuilder.php file at below location,

app/code/Namespace/Modulename/Model/Mail/Template/TransportBuilder.php

<?php
namespace Namespace\Modulename\Model\Mail\Template;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
 public function addAttachment(
 $body,
 $mimeType = \Zend_Mime::TYPE_OCTETSTREAM,
 $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
 $encoding = \Zend_Mime::ENCODING_BASE64,
 $filename = null
) {
 $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
 return $this;
}
}

Create helper file to send image attachment in mail template,

<?php
namespace Namespace\Modulename\Helper;
use Magento\Customer\Model\Session;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
 const EMAIL_IDENTIFIER_TEMPLATE = 'custommodule/general/emailtemplate';
 protected $_inlineTranslation;
 protected $_storeManager;
 protected $_transportBuilder;
 public function __construct(
 \Magento\Framework\App\Helper\Context $context,
 \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
 \Magento\Store\Model\StoreManagerInterface $storeManager,
 \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder ) 
 {
 $this->_inlineTranslation = $inlineTranslation;
 $this->_storeManager = $storeManager;
 $this->_transportBuilder = $transportBuilder;
 parent::__construct($context);
 }
 public function mailSend() {
 $storeId = $this->_storeManager->getStore()->getStoreId();
 $templateName = $this->scopeConfig->getValue(
 self::EMAIL_IDENTIFIER_TEMPLATE,
 \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
 $storeId
 );
 $senderName = 'Sender name'; //store admin sender name
 $senderEmail = '[email protected]'; // store admin email id
 $recipientEmail = '[email protected]'; // recipient email id
 $recipientName = 'recipient name';
 if (!$senderEmail && !$recipientEmail) {
 return false;
 }
 $this->_inlineTranslation->suspend();
 $this->_transportBuilder
 ->setTemplateIdentifier($templateName)
 ->setTemplateOptions([
 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
 'store' => $storeId,
 ])
 ->setTemplateVars(
 [
 'order' => 'custom order data',
 'store' => $this->_storeManager->getStore(),
 ]
 );
 /* image attachment logic */
 $image = 'abc.jpg'; //actual image name
 $mediaPath = 'image_dynamic_file_path'.$image; //set image path
 $body = file_get_contents($mediaPath);
 $imageName = pathinfo($image,PATHINFO_BASENAME);
 $this->_transportBuilder->addAttachment(
 $body,
 'image/jpeg',
 \Zend_Mime::DISPOSITION_ATTACHMENT,
 \Zend_Mime::ENCODING_BASE64,
 $imageName
 );
 $this->_transportBuilder
 ->setFrom([
 'name' => $senderName,
 'email' => $senderEmail,
 ])
 ->addTo($recipientEmail, $recipientName);
 /* @var \Magento\Framework\Mail\Transport $transport */
 $transport = $this->_transportBuilder->getTransport();
 try {
 $transport->sendMessage();
 } catch (\Exception $e) {
 $this->context->getLogger()->alert($e->getMessage());
 }
 $this->_inlineTranslation->resume();
 }
 }

Note: In above example i send image attachment

I hope it works! Thanks.

answered May 9, 2019 at 4:44
3
  • thanks for your answer.I already tried that way not working in magento 2.3. magento.stackexchange.com/questions/252506/… Commented May 9, 2019 at 5:31
  • What error are you getting? Commented May 9, 2019 at 5:52
  • 1
    call undefined method createAttachment() error. Commented May 9, 2019 at 5:56

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.