Magento 2 客户注册邮件给管理员



在过去的几周里,我一直在玩Magento 2,并设法让一些事情发挥作用。我现在唯一在挣扎的事情就是在客户注册后向管理员发送邮件。

我正在寻找的是这样的: 如果(邮件已发送给客户确认注册({ 向管理员发送邮件}

希望这就足够了。

谢谢你的时间。

在Magento 2中注册客户后向管理员发送电子邮件的步骤

在 \app\code\Namespace_Modulename\etc\frontend\ 中创建事件.xml文件并添加以下代码。在这里,我们需要定义一个事件 在客户注册后发送邮件.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_register_success">
<observer name="sendmail_toadmin" instance="Namespacemodule_name ObserverSendMailToAdmin"/>
</event>
</config>

在 \app\code\Namespace_Modulename\Observer 中创建 SendMailToAdmin.php 文件。此观察器类用于在客户成功注册后发送邮件。

namespace Namespace_ModulenameCustomObserver; 
use MagentoFrameworkEventObserverInterface; 
class SendMailToAdmin implements ObserverInterface
{
const XML_PATH_EMAIL_RECIPIENT = 'trans_email/ident_general/email';
protected $_transportBuilder;
protected $inlineTranslation;
protected $scopeConfig;
protected $storeManager;
protected $_escaper;
public function __construct(
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $escaper
) {
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->_escaper = $escaper;
}
public function execute(MagentoFrameworkEventObserver $observer)
{

$customer = $observer->getData('customer');
$this->inlineTranslation->suspend();
try 
{
$error = false;
$sender = [
'name' => $this->_escaper->escapeHtml($customer->getFirstName()),
'email' => $this->_escaper->escapeHtml($customer->getEmail()),
];
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($sender);
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE; 
$transport = 
$this->_transportBuilder
->setTemplateIdentifier('1') // Send the ID of Email template which is created in Admin panel
->setTemplateOptions(
['area' => MagentoFrameworkAppArea::AREA_FRONTEND, // using frontend area to get the template file
'store' => MagentoStoreModelStore::DEFAULT_STORE_ID,]
)
->setTemplateVars(['data' => $postObject])
->setFrom($sender)
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->getTransport();
$transport->sendMessage(); ;
$this->inlineTranslation->resume();

} 
catch (Exception $e) 
{
MagentoFrameworkAppObjectManager::getInstance()->get('PsrLogLoggerInterface')->debug($e->getMessage());
}
}

}

最新更新