2

Is there a way how to set multiple recipients on Magento 2 Contacts, right now it only allows to set single email.

http://prntscr.com/l3f158

Thanks, JM

1
  • You have to create a separate field to achieve this functionality as this field is used in many modules so it will require you to customize at many locations. Instead, you can add a separate field for multiple email addresses. Commented Oct 8, 2018 at 6:19

3 Answers 3

2

To achieve, this requirement, there are two section, where you have to work.

  • First, you have created a new field at system configuration where you can put multiple emails.
  • Second, you have to change Magento\Contact\Model\Mail::send behaviour base using around plugin

So, 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.

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.