- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi guys,
how can i add upload file button and send value through email into contact us form. can any one help me on that ?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello @bharath553,
Please replace the code f that file with the below one:
<?php /** * * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ namespace Hashcrypt\Contact\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; use Magento\Contact\Model\ConfigInterface; use Magento\Contact\Model\MailInterface; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Exception\LocalizedException; use Psr\Log\LoggerInterface; use Magento\Framework\DataObject; class Index extends \Magento\Contact\Controller\Index\Post { private $dataPersistor; /** * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page */ protected $context; private $fileUploaderFactory; private $fileSystem; /** * @var \Magento\Framework\Mail\Template\TransportBuilder */ protected $_transportBuilder; /** * @var \Magento\Framework\Translate\Inline\StateInterface */ protected $inlineTranslation; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; protected $storeManager; /** * @var \Magento\Store\Model\StoreManagerInterface */ /** * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct( Context $context, ConfigInterface $contactsConfig, MailInterface $mail, DataPersistorInterface $dataPersistor, LoggerInterface $logger = null, Filesystem $fileSystem, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory ) { $this->fileUploaderFactory = $fileUploaderFactory; $this->fileSystem = $fileSystem; $this->inlineTranslation = $inlineTranslation; $this->_transportBuilder = $transportBuilder; $this->scopeConfig = $scopeConfig; parent::__construct($context, $contactsConfig, $mail,$dataPersistor,$logger); } /** * Post user question * * @return void * @throws \Exception */ public function execute() { $post = $this->getRequest()->getPostValue(); if (!$post) { $this->_redirect('contact'); return; } $this->inlineTranslation->suspend(); try { $postObject = new \Magento\Framework\DataObject(); $postObject->setData($post); $error = false; if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) { $error = true; } if (!\Zend_Validate::is(trim($post['comment']), 'NotEmpty')) { $error = true; } if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) { $error = true; } if (\Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) { $error = true; } $filesData = $this->getRequest()->getFiles('document'); if ($filesData['name']) { try { // init uploader model. $uploader = $this->fileUploaderFactory->create(['fileId' => 'document']); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $uploader->setAllowCreateFolders(true); $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png', 'pdf', 'docx']); $path = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('contact-doc'); $result = $uploader->save($path); $upload_document = 'contact-doc'.$uploader->getUploadedFilename(); $filePath = $result['path'].$result['file']; $fileName = $result['name']; }catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError( __('File format not supported.') ); $this->getDataPersistor()->set('contact', $post); $this->_redirect('contact'); return; } } else { $upload_document = ''; $filePath = ''; $fileName = ''; } if ($error) { throw new \Exception(); } $this->inlineTranslation->suspend(); $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; $transport = $this->_transportBuilder ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope)) ->setTemplateOptions( [ 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ] ) ->setTemplateVars(['data' => $postObject]) ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope)) ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope)) ->addAttachment($filePath, $fileName) ->setReplyTo($post['email']) ->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->messageManager->addSuccess( __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.') ); $this->getDataPersistor()->clear('contact_us'); $this->_redirect('contact'); return; } catch (\Exception $e) { $this->inlineTranslation->resume(); $this->messageManager->addError( __('We can\'t process your request right now. Sorry, that\'s all we know.') ); $this->getDataPersistor()->set('contact_us', $post); $this->_redirect('contact'); return; } } /** * Get Data Persistor * * @return DataPersistorInterface */ private function getDataPersistor() { if ($this->dataPersistor === null) { $this->dataPersistor = ObjectManager::getInstance() ->get(DataPersistorInterface::class); } return $this->dataPersistor; } }
I hope this will help you, if still you have any query let me know.
If it helps you please accept it as solution.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Hi @bharath553
To achieve this functionality - i would suggest you to refer following links :
If you want to add custom field in contact form - https://magento.stackexchange.com/questions/115344/add-custom-field-to-contact-form-magento-2
Add file upload field in contact us form - https://github.com/hashcrypt-devs-rajkot/magento-2-contact-file-upload
Hope it helps !
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Hello @bharath553
First, you need to override the default Magento 2 Contact Us form using the solution at https://stackoverflow.com/questions/32120838/in-magento-2-how-to-override-phtml-or-layout-core-files
Then, you need to add the "browse" button in .phtml file.
Now, follow the solution at https://meetanshi.com/blog/add-attachments-with-email-in-magento-2-3-x/ to attach the files in Email.
The solution given by @Manthan Dave is right but you need to also attach the files in Email for which the given steps will be enough.
Hope it helps.
If you've found my answer useful, please give"Kudos" and "Accept as Solution"
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Thanks for your quick response guys. one more query i.e how can i add dropdown and post it through email (in contact us form) magento 2.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
I started installtion and At the time complilation am faceing with one issue. can you please help me ?
Interception cache generation... 6/7 [========================>---] 85% 1 min 282.0 MiBErrors during compilation:
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Magento\Contact\Model\ConfigInterface. Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/dev/app/code/Hashcrypt/Contact/Controller/Index/Index.php
Total Errors Count: 1
[Magento\Framework\Validator\Exception]
Error during compilation
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Hello @bharath553,
In magento2.3 https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Contact/Controller/Index/Post....
the second argument is of constructor is : ConfigInterface which is not given in https://github.com/hashcrypt-devs-rajkot/magento-2-contact-file-upload/blob/master/Controller/Index/... parent constructor.
So edit the constructor of https://github.com/hashcrypt-devs-rajkot/magento-2-contact-file-upload/blob/master/Controller/Index/... as :
public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Contact\Model\ConfigInterface $contactsConfig, Filesystem $fileSystem, \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory ) { $this->fileUploaderFactory = $fileUploaderFactory; $this->fileSystem = $fileSystem; parent::__construct($context, $contactsConfig, $transportBuilder,$inlineTranslation,$scopeConfig,$storeManager); }
And then execute the commands again.
If it helps you, please accept it as solution and give kudos.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Hi , it has worked for me , but again at the time of compilation am faceing with an issue. can you help me ?
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Magento\Contact\Model\MailInterface.
Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/em1/app/code/Hashcrypt/Contact/Controller/Index/Index.php
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
hey guys after solving all the below error . finall i got extra parameter in parent error. can any one help me ?
error 1 :
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Magento\Contact\Model\ConfigInterface.
Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/dev/app/code/Hashcrypt/Contact/Controller/Index/Index.php
error 2:
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Magento\Contact\Model\MailInterface.
Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/em1/app/code/Hashcrypt/Contact/Controller/Index/Index.php
error 3:
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Magento\Framework\App\Request\DataPersistorInterface.
Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/em1/app/code/Hashcrypt/Contact/Controller/Index/Index.php
error : 4
Hashcrypt\Contact\Controller\Index\Index
Incompatible argument type: Required type: \Psr\Log\LoggerInterface.
Actual type: \Magento\Framework\Mail\Template\TransportBuilder; File:
/home/psabbavarapu/public_html/em1/app/code/Hashcrypt/Contact/Controller/Index/Index.php
error : 5
Extra parameters passed to parent construct: $transportBuilder, $inlineTranslation, $scopeConfig, $storeManager. File: /home/psabbavarapu/public_html/em1/app/code/Hashcrypt/Contact/Controller/Index/Index.php
current constructore:
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Contact\Model\ConfigInterface $contactsConfig,
\Magento\Contact\Model\MailInterface $contactsMail,
\Magento\Framework\App\Request\DataPersistorInterface $contactsPersistor,
\Psr\Log\LoggerInterface $contactsLogger,
Filesystem $fileSystem,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
) {
$this->fileUploaderFactory = $fileUploaderFactory;
$this->fileSystem = $fileSystem;
$this->contactsPersistor = $contactsPersistor;
// $this->logger = $contactsLogger;
parent::__construct($context,$contactsConfig,$contactsMail,$contactsLogger,$transportBuilder,$inlineTranslation,$scopeConfig,$storeManager);
}
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
Hello @bharath553
From your magento root directory remove the contents of generated folder by
rm -rf generated/*
and then compile the code again. If still you face any issue, let me know.
If it helps you, please accept it as solution and give kudos.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: How to add upload file button into contact us form in magenot 2 ?
still am faceing with same issue.