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' => '[email protected]',
 ];
 $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;
 }
 }
}
asked Dec 17, 2019 at 9:58
7
  • Please add your code snippet here whatever you have tried ? and what problem you are getting. Commented Dec 17, 2019 at 10:20
  • Send Email is working properly, I have to add the attachment file with the email Commented Dec 17, 2019 at 10:33
  • have you checked these URLs magento.stackexchange.com/questions/104044/… and dckap.com/blog/how-to-attach-a-pdf-file-to-emails-in-magento-2 Commented Dec 17, 2019 at 10:38
  • $attachment = $_transportBuilder->addAttachment($pdfData,$fileName); $bodyPart = new \Zend\Mime\Message(); $bodyPart->setParts(array($bodyMessage,$attachment)); $transport->getMessage()->setBody($bodyPart); Commented Dec 17, 2019 at 10:39
  • I have tried this one, but not working Commented Dec 17, 2019 at 10:39

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

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.