Magento2自定义电子邮件发送?

时间:2018-03-01 18:05:33

标签: magento2

我在magento2页面添加了自定义html表单。在magento2默认联系表单将在那里,它将正常工作。但我的自定义电子邮件不发送。如何使用用户和管理员发送自定义邮件?

2 个答案:

答案 0 :(得分:0)

<强> 1。创建新的html文件

/app/code/VendorName/ModuleName/view/frontend/email/modulename/test.html

<div>

Hello, Test email.

</div>

<强> 2。声明您的电子邮件模板,创建xml文件

/app/code/VendorName/ModuleName/etc/email_templates.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="modulename_test_template" label="Test email" file="modulename/test.html" type="html" module="VendorName_ModuleName" area="frontend"/>
</config>

第3。最后,创建一个发送电子邮件的功能。

namespace VendorName\ModuleName\Controller;

use Magento\Framework\App\RequestInterface;

class Sendemail extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $_request;
    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

public function __construct(
    \Magento\Framework\App\Action\Context $context
    , \Magento\Framework\App\Request\Http $request
    , \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    , \Magento\Store\Model\StoreManagerInterface $storeManager
)
{
    $this->_request = $request;
    $this->_transportBuilder = $transportBuilder;
    $this->_storeManager = $storeManager;
    parent::__construct($context);
}

public function execute()
{
    $store = $this->_storeManager->getStore()->getId();
    $transport = $this->_transportBuilder->setTemplateIdentifier('modulename_test_template')
        ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
        ->setTemplateVars(
            [
                'store' => $this->_storeManager->getStore(),
            ]
        )
        ->setFrom('general')
        // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
        ->addTo('customer@email.com', 'Customer Name')
        ->getTransport();
    $transport->sendMessage();
    return $this;
}

}

答案 1 :(得分:0)


检查流程,创建新的联系表单,然后可以从HTTPREquestInterface获取表单数据,然后参考以上答案发送电子邮件,然后可以使用自定义表单发送电子邮件


public function execute()
{
    $store = $this->_storeManager->getStore()->getId();
    $transport = $this->_transportBuilder->setTemplateIdentifier('modulename_test_template')
        ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
        ->setTemplateVars(
            [
                'store' => $this->_storeManager->getStore(),
            ]
        )
        ->setFrom('general')
        // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
        ->addTo('customer@email.com', 'Customer Name')
        ->getTransport();
    $transport->sendMessage();
    return $this;
}

//customer@email.com---can get it from httpRequest