Is there a way how to set multiple recipients on Magento 2 Contacts, right now it only allows to set single email.
Thanks, JM
Is there a way how to set multiple recipients on Magento 2 Contacts, right now it only allows to set single email.
Thanks, JM
To achieve, this requirement, there are two section, where you have to work.
Magento\Contact\Model\Mail::send
behaviour base using around pluginSo, first create a module which has a system.xml file at app/code/{Vendor}/{ModuleName}/etc/adminhtml/
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="contact">
<group id="email">
<field id="recipient_emails" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Send Emails To Multiple Email</label>
</field>
</group>
</section>
</system>
</config>
Around Plugin
<?php
namespace {Vendor}\{MOdule}\Plugin;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Area;
class EmailPlugin {
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var ConfigInterface
*/
private $contactsConfig;
/**
* @var TransportBuilder
*/
private $transportBuilder;
/**
* @var StateInterface
*/
private $inlineTranslation;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* Initialize dependencies.
*
* @param ConfigInterface $contactsConfig
* @param TransportBuilder $transportBuilder
* @param StateInterface $inlineTranslation
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
ConfigInterface $contactsConfig,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager = null
) {
$this->contactsConfig = $contactsConfig;
$this->transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
$this->scopeConfig = $scopeConfig;
}
public function aroundSend($replyTo, array $variables)
{
/** @see \Magento\Contact\Controller\Index\Post::validatedParams() */
$replyToName = !empty($variables['data']['name']) ? $variables['data']['name'] : null;
$to = $this->scopeConfig->getValue('contact/email/recipient_emails', ScopeInterface::SCOPE_STORE);
$this->inlineTranslation->suspend();
try {
$transport = $this->transportBuilder
->setTemplateIdentifier($this->contactsConfig->emailTemplate())
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $this->storeManager->getStore()->getId()
]
)
->setTemplateVars($variables)
->setFrom($this->contactsConfig->emailSender())
->addTo($this->contactsConfig->emailRecipient())
->setReplyTo($replyTo, $replyToName)
->getTransport();
$transport->sendMessage();
} finally {
$this->inlineTranslation->resume();
}
}
}
di.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Contact\Model\Email">
<plugin name="multiple-email-receipts" type="{Vendor}\{Module}\Plugin\EmailPlugin" sortOrder="10" disabled="false" />
</type>
</config>
The code is not tested, give basic of the idea.
Create separate config for other email address then you can create after plugin of
emailRecipient()
method located in
vendor/magento/module-contact/Model/Config.php
file
Thanks for all the responses. Just figured it out that I just extend the core magento contact, and instead of commenting out validate-email under etc/adminhtml/system.xml, I just replace it required-entry
Thanks.