If, you want to send another contact mail copy to another email address as a BCC? Then follow the below steps.
Step 1: Create a registration file like Magemonkeys/ContactCc/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkey_ContactCc', __DIR__ ); |
Step 2: Create di file like Magemonkeys/ContactCc/etc/di.xml
1 2 3 4 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Contact\Controller\Index\Post" type="Magemonkey\ContactCc\Rewrite\Magento\Contact\Controller\Index\Post"/> </config> |
Step 3: Create module file like Magemonkeys/ContactCc/etc/module.xml
1 2 3 4 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkey_ContactCc" setup_version="1.0.2"/> </config> |
Step 4: Create a system file like Magemonkeys/ContactCc/etc/adminhtml/system.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0"?> <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" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1"> <tab>general</tab> <group id="email" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Email Options</label> <field id="copy_to" translate="label comment" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Send Contact Us Email Copy To</label> <comment>Comma-separated</comment> </field> </group> </section> </system> </config> |
Step 5: Create helper file like Magemonkeys/ContactCc/Helper/Email.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php namespace Magemonkey\ContactCc\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; /** * Class Email * @package Magemonkey\ContactCc\Helper */ class Email extends AbstractHelper { const XML_PATH_EMAIL_COPY_METHOD = 'contact/email/copy_method'; const XML_PATH_EMAIL_COPY_TO = 'contact/email/copy_to'; /** * @var StoreManagerInterface */ private $storeManager; /** * @var ScopeConfigInterface */ protected $scopeConfig; /** * Email constructor. * @param Context $context * @param StoreManagerInterface $storeManager * @param ScopeConfigInterface $scopeConfig */ public function __construct( Context $context, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig ) { $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; parent::__construct($context); } /** * @return array|bool * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getEmailCopyTo() { $data = $this->getConfigValue(self::XML_PATH_EMAIL_COPY_TO, $this->storeManager->getStore()->getId()); if (!empty($data)) { return explode(',', $data); } return false; } /** * @return mixed * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getCopyMethod() { return $this->getConfigValue(self::XML_PATH_EMAIL_COPY_METHOD, $this->storeManager->getStore()->getId()); } /** * @param $xmlPath * @param null $storeId * @return mixed */ public function getConfigValue($xmlPath, $storeId = null) { return $this->scopeConfig->getValue( $xmlPath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId ); } } |
Step 6: Create PHP file like Magemonkey/ContactCc/Rewrite/Magento/Contact/Controller/Index/Post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
<?php namespace Magemonkey\ContactCc\Rewrite\Magento\Contact\Controller\Index; use Magento\Framework\App\Area; use Magento\Contact\Model\ConfigInterface; use Magento\Framework\App\Action\Context; use Magento\Framework\DataObject; use Magento\Framework\App\Request\DataPersistorInterface; use Magemonkey\ContactCc\Helper\Email; use Psr\Log\LoggerInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Contact\Model\MailInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\Translate\Inline\StateInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\Mail\Template\TransportBuilder; /** * Class Post * @package Magemonkey\ContactCc\Rewrite\Magento\Contact\Controller\Index */ class Post extends \Magento\Contact\Controller\Index\Post { /** * @var Context */ private $context; /** * @var ConfigInterface */ private $contactsConfig; /** * @var MailInterface */ private $mail; /** * @var DataPersistorInterface */ private $dataPersistor; /** * @var LoggerInterface */ private $logger; /** * @var Email */ private $helper; /** * @var StateInterface */ private $inlineTranslation; /** * @var TransportBuilder */ private $transportBuilder; /** * @var StoreManagerInterface */ private $storeManager; /** * Post constructor. * @param Context $context * @param ConfigInterface $contactsConfig * @param MailInterface $mail * @param DataPersistorInterface $dataPersistor * @param Email $helper * @param LoggerInterface|null $logger * @param TransportBuilder $transportBuilder * @param StateInterface $inlineTranslation * @param StoreManagerInterface|null $storeManager */ public function __construct( Context $context, ConfigInterface $contactsConfig, MailInterface $mail, DataPersistorInterface $dataPersistor, Email $helper, LoggerInterface $logger = null, TransportBuilder $transportBuilder, StateInterface $inlineTranslation, StoreManagerInterface $storeManager = null ) { $this->context = $context; $this->contactsConfig = $contactsConfig; $this->mail = $mail; $this->dataPersistor = $dataPersistor; $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); $this->helper = $helper; $this->transportBuilder = $transportBuilder; $this->inlineTranslation = $inlineTranslation; $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class); parent::__construct($context, $contactsConfig, $mail, $dataPersistor, $logger); } /** * Contact store * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { if (!$this->getRequest()->isPost()) { return $this->resultRedirectFactory->create()->setPath('*/*/'); } try { $this->sendEmail($this->validatedParams()); $this->messageManager->addSuccessMessage( __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.') ); $this->dataPersistor->clear('contact_us'); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); $this->dataPersistor->set('contact_us', $this->getRequest()->getParams()); } catch (\Exception $e) { $this->logger->critical($e); $this->messageManager->addErrorMessage( __('An error occurred while processing your form. Please try again later.') ); $this->dataPersistor->set('contact_us', $this->getRequest()->getParams()); } return $this->resultRedirectFactory->create()->setPath('contact/index'); } /** * Send email * @param array $post * @throws \Magento\Framework\Exception\MailException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function sendEmail($post) { $this->send( $post['email'], ['data' => new DataObject($post)] ); } /** * Send email * @param $replyTo * @param array $variables * @throws \Magento\Framework\Exception\MailException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function send($replyTo, array $variables) { $replyToName = !empty($variables['data']['name']) ? $variables['data']['name'] : null; $this->inlineTranslation->suspend(); try { $this->configureEmailTemplate($replyTo, $replyToName, $variables); $this->transportBuilder->addTo($this->contactsConfig->emailRecipient()); $copyTo = $this->helper->getEmailCopyTo(); if (!empty($copyTo) && $this->helper->getCopyMethod() == 'bcc') { foreach ($copyTo as $email) { $this->transportBuilder->addBcc($email); } } $transport = $this->transportBuilder->getTransport(); $transport->sendMessage(); $this->sendCopyTo($replyTo, $replyToName, $variables); } catch (\Exception $e) { $this->logger->error($e->getMessage()); } finally { $this->inlineTranslation->resume(); } } /** * Prepare and send copy email message * @param string $replyTo * @param string $replyToName * @param array $variables * @return void */ public function sendCopyTo($replyTo, $replyToName, $variables) { $copyTo = $this->helper->getEmailCopyTo(); if (!empty($copyTo) && $this->helper->getCopyMethod() == 'copy') { foreach ($copyTo as $email) { $this->configureEmailTemplate($replyTo, $replyToName, $variables); $this->transportBuilder->addTo($email); $transport = $this->transportBuilder->getTransport(); $transport->sendMessage(); } } } /** * Configure email template * @param string $replyTo * @param string $replyToName * @param array $variables * @return void */ protected function configureEmailTemplate($replyTo, $replyToName, $variables) { $this->transportBuilder->setTemplateIdentifier($this->contactsConfig->emailTemplate()); $this->transportBuilder->setTemplateOptions([ 'area' => Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId() ]); $this->transportBuilder->setTemplateVars($variables); $this->transportBuilder->setFrom($this->contactsConfig->emailSender()); $this->transportBuilder->setReplyTo($replyTo, $replyToName); } /** * Validate form params * @return array * @throws \Exception */ private function validatedParams() { $request = $this->getRequest(); if (trim($request->getParam('name')) === '') { throw new LocalizedException(__('Name is missing')); } if (trim($request->getParam('comment')) === '') { throw new LocalizedException(__('Comment is missing')); } if (false === \strpos($request->getParam('email'), '@')) { throw new LocalizedException(__('Invalid email address')); } if (trim($request->getParam('hideit')) !== '') { throw new LocalizedException(__('Error')); } return $request->getParams(); } } |
Step 7: Then after run the below commands.
1 2 3 |
php bin/magento setup:upgrade php bin/magento setup:static-content:deploy php bin/magento cache:flush |
That’s it…
Now clean cache and check in Admin > STORES > Settings > Configuration > GENERAL > Contacts. Your new email address field option appear here.
[crayon-63d3e8e83a33f581205750/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.