我在自定义模块中有自定义表单,我需要它来发送电子邮件。目前,当我提交表单时,页面重新加载,我没有收到任何消息。控制台没有错误。并且没有发送电子邮件。
以下是我的自定义模块的PHP代码。
供应商/模块/块/ index.php的:
<?php
namespace Vendor\Module\Block\Index;
class Index extends \Magento\Framework\View\Element\Template {
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = []) {
parent::__construct($context, $data);
}
public function getFormAction()
{
return '/customemail/index/post';
}
}
销售者/模块/控制器/索引/ post.php中:
<?php
namespace Vender\Module\Controller\Index;
use Zend\Log\Filter\Timestamp;
class Post extends \Magento\Framework\App\Action\Action
{
const XML_PATH_EMAIL_RECIPIENT_NAME = 'trans_email/ident_general/name';
const XML_PATH_EMAIL_RECIPIENT_EMAIL = 'trans_email/ident_general/email';
protected $_inlineTranslation;
protected $_transportBuilder;
protected $_scopeConfig;
protected $_logLoggerInterface;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Psr\Log\LoggerInterface $loggerInterface,
array $data = []
)
{
$this->_inlineTranslation = $inlineTranslation;
$this->_transportBuilder = $transportBuilder;
$this->_scopeConfig = $scopeConfig;
$this->_logLoggerInterface = $loggerInterface;
$this->messageManager = $context->getMessageManager();
parent::__construct($context);
}
public function execute()
{
$post = $this->getRequest()->getPost();
try
{
// Send Mail
$this->_inlineTranslation->suspend();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$sender = [
'name' => $post['name'],
'email' => $post['email'],
'phone' => $post['phone'],
'numGuests' => $post['numGuests'],
'eventType' => $post['eventType'],
'eventDesc' => $post['eventDesc']
];
$sentToEmail = $this->_scopeConfig ->getValue('trans_email/ident_general/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$sentToName = $this->_scopeConfig ->getValue('trans_email/ident_general/name',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$transport = $this->_transportBuilder
->setTemplateIdentifier('customemail_email_template')
->setTemplateOptions(
[
'area' => 'frontend',
'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
]
)
->setTemplateVars([
'name' => $post['name'],
'email' => $post['email'],
'phone' => $post['phone'],
'numGuests' => $post['numGuests'],
'eventType' => $post['eventType'],
'eventDesc' => $post['eventDesc']
])
->setFrom($sender)
->addTo($sentToEmail,$sentToName)
//->addTo('owner@example.com','owner')
->getTransport();
$transport->sendMessage();
$this->_inlineTranslation->resume();
$this->messageManager->addSuccess('Email sent successfully');
$this->_redirect('/private-events');
} catch(\Exception $e){
$this->messageManager->addError($e->getMessage());
$this->_logLoggerInterface->debug($e->getMessage());
exit;
}
}
}
销售者/模块/视图/前端/模板/ customemail_index_index.phtml:
<form action="<?php echo $block->getFormAction() ?>" name="customemaildata" method="post" id="contactForm-1" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}' class="form">
<div class="row">
<div class="col-lg-6 col-sm-12">
<div class="field event-type required">
<div class="control">
<input placeholder="<?php echo __('Type of Event') ?>" name="eventType" id="eventType" title="<?php echo __('eventType') ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field description">
<div class="control">
<textarea placeholder="Tell us about your event" name="eventDesc" id="eventDesc" title="<?php echo __('eventDesc') ?>" data-validate="{required:false}"></textarea>
</div>
</div>
</div>
<div class="col-lg-6 col-sm-12">
<div class="field name required">
<div class="control">
<input placeholder="<?php echo __('Name') ?>" name="name" id="name" title="<?php echo __('Name') ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field email required">
<div class="control">
<input placeholder="<?php echo __('Email') ?>" name="email" id="email" title="<?php echo __('Email') ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field phone required">
<div class="control">
<input placeholder="<?php echo __('Phone') ?>" name="phone" id="phone" title="<?php echo __('Phone') ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field num-guests required">
<div class="control">
<input placeholder="<?php echo __('Number of Guests') ?>" name="numGuests" id="numGuests" title="<?php echo __('numGuests') ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="actions-toolbar center">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?php echo __('Submit') ?>" class="action submit primary button button--solid-dark button--wide">
<span><?php echo __('Submit') ?></span>
</button>
</div>
</div>
</div>
</div>
</form>
我的模块包含以下文件(我最初没有提供代码,因为我认为问题出在PHP的某处):
Vender
Module
Block
-Index.php
Controller
Index
-Post.php
etc
frontend
-routes.xml
-email_templates.xml
-module.xml
view
frontend
email
-customemail.phtml
layout
-customemail_index_index.xml
templates
-customemail_index_index.phtml
-registration.php
答案 0 :(得分:0)
如果有人遇到此问题,我确实通过以下编辑解决了我的问题。
<强>供应商/模块/块/ index.php的强>
<?php
namespace Vendor\Module\Block\Index;
class Index extends \Magento\Framework\View\Element\Template {
public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = []) {
parent::__construct($context, $data);
}
protected function _prepareLayout()
{
return parent::_prepareLayout();
}
}
<强>销售者/模块/控制器/索引/ post.php中强>
这里的问题是我的发件人数据不正确。我更改/添加了这个:
$senderName = $post['name'];
$senderEmail = $post['email'];
$sender = [
'name' => $senderName,
'email' => $senderEmail,
];
<强>销售者/模块/视图/前端/模板/ customemail_index_index.phtml 强>
在这里,我将表单操作更改为:
<form action="<?php echo $block->getBaseUrl().'/customemail/index/post/';?>" name="customemaildata" method="post" id="privateForm-1" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}' class="form">