0

I have try to send email with PDF attachment but not work in magento 2.3.1. I am looking for some reference link:

https://magento.stackexchange.com/questions/104044/magento-2-send-email-with-attachment

excellencemagentoblog

and git link for mageplaza/magento-2-email-attachments

Please help me, if you have faces above issue and find the right solution.

2 Answers 2

0

Please check below link which will help you: By pragmatically: https://webkul.com/blog/attach-pdf-file-magento-2-email/

By extension: https://www.mageplaza.com/magento-2-email-attachments/

answered Oct 4, 2019 at 7:44
1
  • I have already use it but it is not work. Commented Oct 4, 2019 at 8:41
0

I managed to get it working in my extension

https://github.com/DominicWatts/ContactAttachment

I based this off another tutorial. Sorry I can't find link.

 public function send($replyTo, array $variables)
 {
 $filePath = null;
 $fileName = null;
 $uploaded = false;
 try {
 $fileCheck = $this->fileUploaderFactory->create(['fileId' => 'attachment']);
 $file = $fileCheck->validateFile();
 $attachment = $file['name'] ?? null;
 } catch (\Exception $e) {
 $attachment = null;
 }
 if ($attachment) {
 $upload = $this->fileUploaderFactory->create(['fileId' => 'attachment']);
 $upload->setAllowRenameFiles(true);
 $upload->setFilesDispersion(true);
 $upload->setAllowCreateFolders(true);
 $upload->setAllowedExtensions(['txt', 'csv', 'jpg', 'jpeg', 'gif', 'png', 'pdf', 'doc', 'docx']);
 $path = $this->fileSystem
 ->getDirectoryRead(DirectoryList::MEDIA)
 ->getAbsolutePath(self::FOLDER_LOCATION);
 $result = $upload->save($path);
 $uploaded = self::FOLDER_LOCATION . $upload->getUploadedFilename();
 $filePath = $result['path'] . $result['file'];
 $fileName = $result['name'];
 }
 /** @see \Magento\Contact\Controller\Index\Post::validatedParams() */
 $replyToName = !empty($variables['data']['name']) ? $variables['data']['name'] : null;
 $this->inlineTranslation->suspend();
 $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
 $transport = $this->transportBuilder
 ->setTemplateIdentifier($this->contactsConfig->emailTemplate())
 ->setTemplateOptions(
 [
 'area' => Area::AREA_FRONTEND,
 'store' => $this->storeManager->getStore()->getId(),
 ]
 )
 ->setTemplateVars($variables)
 ->setFrom($this->contactsConfig->emailSender())
 ->addTo($this->contactsConfig->emailRecipient())
 ->setReplyTo($replyTo, $replyToName)
 ->getTransport();
 if ($uploaded && !empty($filePath) && $this->file->fileExists($filePath)) {
 $mimeType = mime_content_type($filePath);
 $transport = $this->transportBuilder
 ->setTemplateIdentifier($this->contactsConfig->emailTemplate())
 ->setTemplateOptions(
 [
 'area' => Area::AREA_FRONTEND,
 'store' => $this->storeManager->getStore()->getId(),
 ]
 )
 ->addAttachment($this->file->read($filePath), $fileName, $mimeType)
 ->setTemplateVars($variables)
 ->setFrom($this->contactsConfig->emailSender())
 ->addTo($this->contactsConfig->emailRecipient())
 ->setReplyTo($replyTo, $replyToName)
 ->getTransport();
 }
 $transport->sendMessage();
 $this->inlineTranslation->resume();
 }
answered Oct 5, 2019 at 12:35

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.