1

This is my system.xml file

ScheduleContentNotifier\etc\adminhtml\system.xml
<?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>
        <tab id="custommodule" translate="label">           
            <label>Custom Module</label>
        </tab>
        <section id="content" translate="label" type="text" sortOrder="600" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Schedule Content Notifier</label>
            <tab>custommodule</tab>
            <resource>custommodule_ScheduleContentNotifier::notifier</resource>
            <group id="general" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Email</label>
                <field id="email" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Enter Email</label>
                    <comment>Enter comma-separated emails for notifying when scheduled content goes live.</comment>
                </field>            
            </group>
            <group id="update" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Please Choose Your Email Template</label>
                <field id="template" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Email Template</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                    <comment>Email template is chosen based on theme fallback when "Default" option is selected.</comment>
                </field>            
            </group>
        </section>
    </system>
</config>

This is my helper file

ScheduleContentNotifier\Helper\Email.php
<?php
namespace Custommodule\ScheduleContentNotifier\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;
    protected $scopeConfig;

    public function __construct(
        Context $context,
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder,
        ScopeConfigInterface $scopeConfig
    ) {
        parent::__construct($context);
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $context->getLogger();
        $this->scopeConfig = $scopeConfig;
    }

    public function sendEmail()
    {
        try {
            $this->inlineTranslation->suspend();
            $sender = [
                'name' => $this->escaper->escapeHtml('example'),
                'email' => $this->escaper->escapeHtml($this->scopeConfig->getValue('content/general/email')),
            ];
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('content_update_template')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )->setTemplateVars([])
                ->setFrom($sender)
                ->addTo($sender)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->logger->debug($e->getMessage());
        }
    }
}

1 Answer 1

2

In your case, you already have the email field defined in the system.xml file under the content/general group. To fetch the comma-separated email addresses from the configuration, you can modify your helper file as follows:

<?php
namespace Custommodule\ScheduleContentNotifier\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;
    protected $scopeConfig;

    public function __construct(
        Context              $context,
        StateInterface       $inlineTranslation,
        Escaper              $escaper,
        TransportBuilder     $transportBuilder,
        ScopeConfigInterface $scopeConfig
    )
    {
        parent::__construct($context);
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->logger = $context->getLogger();
        $this->scopeConfig = $scopeConfig;
    }
    
    public function sendEmail()
    {
        try {
            $this->inlineTranslation->suspend();

            // Fetch comma-separated emails from system.xml configuration
            $emails = $this->scopeConfig->getValue('content/general/email');

            // Split the emails into an array
            $emailArray = explode(',', $emails);

            $sender = [
                'name' => $this->escaper->escapeHtml('example'),
                'email' => $this->escaper->escapeHtml(trim($emailArray[0])), // Use the first email address in the array
            ];

            $transport = $this->transportBuilder
                ->setTemplateIdentifier('content_update_template')
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )->setTemplateVars([])
                ->setFrom($sender)
                ->addTo($sender)
                ->getTransport();

            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->logger->debug($e->getMessage());
        }
    }
}
  • In the sendEmail() method, we retrieve the comma-separated email addresses from the configuration using $emails = $this->scopeConfig->getValue('content/general/email'). Then we explode the $emails string into an array using $emailArray = explode(',', $emails). Finally, we use the first email address from the array as the sender's email address.

Note: In the code above, I have used $emailArray[0] to retrieve the first email address from the array. You can modify the logic according to your specific requirements if you need to handle multiple email addresses differently.

1
  • thank you @charmipatel Jun 1, 2023 at 10:37

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.