0

I have created a custom select in contact form (Contact Us page). Is it possible to send an email to a different email address depending of the selected option? i.e. if i choose 'Area 1' the email is sent to area1@mail.com, if i choose 'Area 2' is sent to area2@mail.com and so on.

Select option

I have seen that the email address to which the email is sent is obtained from system.xml in contact module.

The trace i have made is:

/Magento/Contact/Controller/Index/Post.php

$transport = $this->_transportBuilder
            ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
            ->setTemplateOptions(
                [
                    'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            )
            ->setTemplateVars(['data' => $postObject])
            ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
            ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
            ->setReplyTo($post['email'])
            ->getTransport();

I understand that in the code above is where the transport object is created to send the email. I also know that the email is defined in the next piece of code:

->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))

The value of that constant is 'contact/email/sender_email_identity'.

So, following that path to the system.xml i encountered the field sender_email_identity.

In there, they use a source model to obtain the email.

<source_model>Magento\Config\Model\Config\Source\Email\Identity</source_model>

In the Identity.php file is the toOptionArray( ) function.

/**
 * Retrieve list of options
 *
 * @return array
 */
public function toOptionArray()
{
    if ($this->_options === null) {
        $this->_options = [];
        /** @var $section \Magento\Config\Model\Config\Structure\Element\Section */
        $section = $this->_configStructure->getElement('trans_email');

        /** @var $group \Magento\Config\Model\Config\Structure\Element\Group */
        foreach ($section->getChildren() as $group) {
            $this->_options[] = [
                'value' => preg_replace('#^ident_(.*)$#', '$1', $group->getId()),
                'label' => $group->getLabel(),
            ];
        }
        ksort($this->_options);
    }
    return $this->_options;
} 

Yet, i don't know how to proceed from there.

  • On basis of Dropdown select value you can set Yes. Add Area wise Config Variable in system.xml. Start with static email giving. – Ankit Shah Oct 25 '16 at 7:18
  • @Ankit If i understand correctly the idea would be to replace the <source_model></source_model> tag with my own right? How would i do that, i mean, not to replace in the core file but specify a system.xml of my own that use my custom source_model. – A. Martz Oct 26 '16 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.