0

I have to add the email attachment. I have create a custom module

public function execute()
{
    $post = $this->getRequest()->getPostValue();
    if (!$post) {
        $this->_redirect('/');
        return;
    }

    $this->inlineTranslation->suspend();
    try {
        $recipientMail = $this->getRequest()->getPostValue('email');

        $postObject = new \Magento\Framework\DataObject();
        $postObject->setData($post);

        $error = false;

        $sender = [
          'name' => 'Test',
          'email' => 'test@gmail.com',
        ];

       $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;

       $transport = $this->_transportBuilder
        ->setTemplateIdentifier('send_email_email_template') // this code we have mentioned in the email_templates.xml
        ->setTemplateOptions(
            [
                'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file
                'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
            ]
        )
        ->setTemplateVars(['data' => $postObject])
        ->setFrom($sender)
        ->addTo($recipientMail)
        ->getTransport();

       $transport->sendMessage();
       $this->inlineTranslation->resume();
       $this->messageManager->addSuccess(
        __('Thanks for contacting us. We\'ll respond to you very soon.')
    );
       $this->_redirect('/');
      return;
   } catch (\Exception $e) {
       $this->inlineTranslation->resume();
       $this->messageManager->addError(
        __('We can\'t process your request right now. Sorry, that\'s all we know.' . $e->getMessage())
    );
       $this->_redirect('/');
       return;


   }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.