I have followed this post about creating a form that sends data to a specific email. The module works, but the form isn't sending data to the given email.
For simplicity I am going to paste the code given in the above post and write my comments afterwards
vendorname/modulename/controller/Index/index.php
<?php
/**
*
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\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;
class Index extends Action
{
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;
/**
* @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(
\Magento\Framework\App\Action\Context $context,
Filesystem $fileSystem,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Modia\Cform\Helper\Data $helper,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
parent::__construct($context,$transportBuilder,$inlineTranslation, $scopeConfig );
$this->fileUploaderFactory = $fileUploaderFactory;
$this->fileSystem = $fileSystem;
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->helper = $helper;
$this->scopeConfig = $scopeConfig;
}
public function execute()
{
$post = $this->getRequest()->getPostValue();
$filesData = $this->getRequest()->getFiles('upload_document');
if ($filesData['name']) {
$uploader = $this->fileUploaderFactory->create(['fileId' => 'upload_document']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
$path = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('test-doc');
$result = $uploader->save($path);
$upload_document = 'test-doc'.$uploader->getUploadedFilename();
$filePath = $result['path'].$result['file'];
$fileName = $result['name'];
} else {
$upload_document = '';
$filePath = '';
$fileName = '';
}
$txt='<table>';
if($post['fname']){
$txt.='<tr><td><strong>Client Name</strong>:'.$post['fname'].'</td></tr>';
}
if($post['address']){
$txt.='<tr><td><strong>Address</strong>:'.$post['address'].'</td></tr>';
}
if($post['city']){
$txt.='<tr><td><strong>City</strong>:'.$post['city'].'</td></tr>';
}
if($post['state']){
$txt.='<tr><td><strong>State/Province</strong>:'.$post['state'].'</td></tr>';
}
if($post['zipcode']){
$txt.='<tr><td><strong>Zip Code</strong>:'.$post['zipcode'].'</td></tr>';
}
if($post['phone']){
$txt.='<tr><td><strong>Phone</strong>:'.$post['phone'].'</td></tr>';
}
if($post['email']){
$txt.='<tr><td><strong>Email</strong>:'.$post['email'].'</td></tr>';
}
if(!empty($post['project_type'])){
$projecttypearray = implode(",",$post['project_type']);
$txt.='<tr><td><strong>Project Type</strong>:'.$projecttypearray.'</td></tr>';
}
if($post['comment']){
$txt.='<tr><td><strong>Comment</strong>:'.$post['comment'].'</td></tr>';
}
$txt.='</table>';
//echo $txt;
$customerName='Demo Form';
$message=$txt;
$userSubject= 'Demo From ';
$fromEmail= '[email protected]';
$fromName = 'Test Demo Form';
$templateVars = [
'store' => 1,
'customer_name' => $customerName,
'subject' => $userSubject,
'message' => $message
];
$from = ['email' => $fromEmail, 'name' => $fromName];
$this->inlineTranslation->suspend();
$to = '[email protected]';
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$templateOptions = [
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => 1
];
$transport = $this->_transportBuilder->setTemplateIdentifier(5, $storeScope)
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->addAttachment($filePath, $fileName)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
$this->messageManager->addSuccess(__('Form successfully submitted'));
$this->_redirect('form');
}
}
vendorname/modulename/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="\Vendorname\Modulename\Magento\Mail\Template\TransportBuilder" />
</config>
Vendorname/Modulename/Magento/Mail/Template/TransportBuilder.php
<?php
namespace Vendorname\Modulename\Magento\Mail\Template;
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
public function addAttachment($file, $name)
{
if (!empty($file) && file_exists($file)) {
$this->message
->createAttachment(
file_get_contents($file),
\Zend_Mime::TYPE_OCTETSTREAM,
\Zend_Mime::DISPOSITION_ATTACHMENT,
\Zend_Mime::ENCODING_BASE64,
basename($name)
);
}
return $this;
}
}
Vendorname/Modulename/view/frontend/templates/test.phtml
<div class="container">
<form class="form-horizontal" method="post" enctype="multipart/form-data" action="url/cform/index/index">
<div class="form-group">
<div class="row">
<div class="col-sm-12">
<label for="">Name</label>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<input class="form-control" type="text" id="" name="fname" placeholder="first name">
</div>
<div class="col-sm-6">
<input class="form-control" type="text" id="" name="lname" placeholder="last name">
</div>
</div>
</div>
<div class="form-group">
<label for="">Address</label>
<textarea class="col-sm-12 form-control" rows="3" placeholder="" name="address"></textarea>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-5">
<label for="">City</label>
<input class="form-control" type="text" name="city" id="" placeholder="">
</div>
<div class="col-sm-4">
<label for="">State/Province</label>
<input class="form-control" type="text" name="state" id="" placeholder="">
</div>
<div class="col-sm-3">
<label for="">Zip Code</label>
<input class="form-control" type="text" name="zipcode" id="" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-8">
<label for="">Phone</label>
<div class="row">
<div class="col-sm-5">
<input class="form-control" type="text" id="" name="phone" placeholder="">
</div>
<div class="col-sm-2 phone-or">
<span>OR</span>
</div>
<div class="col-sm-5">
<input class="form-control" type="text" id="" placeholder="">
</div>
</div>
</div>
<div class="col-sm-4">
<label for="">Email</label>
<input class="form-control" type="email" id="email" name="email" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-2">
<label for="">Project Type :</label>
</div>
<div class="col-sm-10">
<label class="checkbox-inline">
<input type="checkbox" id="" value="One1" name="project_type[]"> <span>Residential</span>
</label>
<label class="checkbox-inline">
<input type="checkbox" id="" value="One2" name="project_type[]"> <span>Commercial</span>
</label>
</div>
</div>
</div>
<div class="col-md-4 col-xs-12">
<input accept="image/*" name="upload_document" type="file" value="" />
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-2">
<label for="">Comment :</label>
</div>
<div class="col-sm-10">
<textarea class="form-control comment" rows="3" name="comment" placeholder="additional information about your project"></textarea>
</div>
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-primary">SUBMIT</button>
</div>
</form>
</div>
Now call in cms page
{{block class="Magento\Framework\View\Element\Template" template="Vendorname_Modulename::test.phtml" }}
I think the error resides in the action field of the template file. I replaced vendorname/modulename with my own in all the files, but the action url should point to the controller file, right?
So it should be something like vendorname/modulename/index/index or modulename/index/index , right? Well neither of the above work, so I would love any kind of help.
PS.
I also suspect the way I am using the cms-code for the block. Maybe there should be a mention of a different url? I don't know, I have been fighting with this for a few days now.
Thank you for you time!