cancel
Showing results for 
Search instead for 
Did you mean: 

Get Store email addresses programmatically

SOLVED

Re: Get Store email addresses programmatically

@tippanna_pawar 

 

Run below commands and try :

 

chmod -R 777 var/ generated/

php bin/magento setup:di:compile

php bin/magento cache:flush

 

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

Re: Get Store email addresses programmatically

Hi @tippanna_pawar ,

Please mention exact magento version of your instance.

Can you try below commands in sequence

rm -rf generated/* var/cache/*

php bin/magento s:d:c

php bin/magento c:f

Chmod 777 -R var/* generated/*

 

Thanks!

Problem Solved! Click Kudos & Accept as Solution!

Re: Get Store email addresses programmatically

issue1.jpg

Re: Get Store email addresses programmatically

i have all above commands but still not solving

Re: Get Store email addresses programmatically

this is my controller code ..@receiver info you can find the code and at the error details i have given please check
<?php

namespace MageVision\Blog27\Controller\Adminhtml\Ohgod;

use Magento\Sales\Model\Order;

class Index extends \Magento\Backend\App\Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context
        //  \Magento\Framework\Message\ManagerInterface $messageManager

    ) {
        $this->resultFactory = $context->getResultFactory();
        //$this->messageManager = $messageManager;
        return parent::__construct($context);
    }
    public function execute()
    {

        //  $this->messageManager->addSuccess(__("Error Message"));
        //  return $this->_messageManager->addError(__("Error Message"));
        //  echo "hi";
        $id = $this->getRequest()->getParam('order_id');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($id);
        $orderState = 'processing';     
        $order->setState($orderState)->setStatus('processing');
        $order->save();
        // Code to send email.
        $templateId = 8;

        /* Receiver Detail  */
        $store = $objectManager = $objectManager->create('Magento\Store\Model\ScopeInterface');
        $semail = $store->getValue('trans_email/ident_sales/name'ScopeInterface:Smiley FrustratedCOPE_STORE);
        $receiverInfo = [
            'name' => $semail,
            'email' => $semail
        ];
        //$name = $customerSession->getCustomer()->getName();
        /* Sender Detail  */
        $email = $order->getCustomerEmail();
        $name = $order->getCustomerFirstname() . " " . $order->getCustomerMiddlename() . " " . $order->getCustomerLastname();
        $senderInfo = [
            'name' => $name,
            'email' => $email
        ];

        $variablevalue1 = 'Tippanna Pawar';
        $variablevalue2 = "12345";
        /* Assign values for your template variables  */
        $emailTempVariables = array();
        $emailTempVariables['myvar1'] = $variablevalue1;
        $emailTempVariables['myvar2'] = $variablevalue2;

        /* We write send mail function in helper because if we want to 
       use same in other action then we can call it directly from helper */

        /* call send mail method from helper or where you define it*/
        $this->_objectManager->get('Tp\Emails\Helper\Email')->yourCustomMailSendMethod(
            $emailTempVariables,
            $senderInfo,
            $receiverInfo,
            $templateId
        );


        //End of email code
        //  $order->setStatus(\Magento\Sales\Model\Order:Smiley FrustratedTATE_PENDING, true)->save();
        $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference again and again');
        // $this->_getSession()->addSuccess($this->__('The order state has been changed.'));
        $this->_redirect('sales/order/view'array('order_id' => $id));
    }

    /**
     * Check Permission.
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('MageVision_Blog27');
    }
}
 
 
 
ERROR:
Fatal error: Uncaught Error: Cannot instantiate interface Magento\Store\Model\ScopeInterface in C:\xampp\htdocs\dnourm2\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php:50 Stack trace: #0 C:\xampp\htdocs\dnourm2\vendor\magento\framework\ObjectManager\ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create('Magento\\Store\\M...', Array) #1 C:\xampp\htdocs\dnourm2\app\code\MageVision\Blog27\Controller\Adminhtml\Ohgod\index.php(34): Magento\Framework\ObjectManager\ObjectManager->create('Magento\\Store\\M...') #2 C:\xampp\htdocs\dnourm2\generated\code\MageVision\Blog27\Controller\Adminhtml\Ohgod\index\Interceptor.php(24): MageVision\Blog27\Controller\Adminhtml\Ohgod\Index->execute() #3 C:\xampp\htdocs\dnourm2\vendor\magento\framework\App\Action\Action.php(107): MageVision\Blog27\Controller\Adminhtml\Ohgod\index\Interceptor->execute() #4 C:\xampp\htdocs\dnourm2\vendor\magento\module-backend\App\AbstractAction.php(235): Magento\Framework\App\Action\Action->dispatch(Object(Mag in C:\xampp\htdocs\dnourm2\vendor\magento\framework\ObjectManager\Factory\Dynamic\Developer.php on line 50

Re: Get Store email addresses programmatically

@tippanna_pawar 

 

You're override $objectManager variable value in :

$store = $objectManager = $objectManager->create('Magento\Store\Model\ScopeInterface');

 

change it by :

$store = $objectManager->create('Magento\Store\Model\ScopeInterface');

 

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !

Re: Get Store email addresses programmatically

i have changed . Still showing same error

Re: Get Store email addresses programmatically

@tippanna_pawar ,

 

Add use Magento\Store\Model\ScopeInterface code before your class instaintaite line of code.

 

Thanks!

Re: Get Store email addresses programmatically

 
<?php

 

namespace MageVision\Blog27\Controller\Adminhtml\Ohgod;

 

use Magento\Sales\Model\Order;

 

class Index extends \Magento\Backend\App\Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context
        //  \Magento\Framework\Message\ManagerInterface $messageManager

 

    ) {
        $this->resultFactory = $context->getResultFactory();
        //$this->messageManager = $messageManager;
        return parent::__construct($context);
    }
    public function execute()
    {

 

        //  $this->messageManager->addSuccess(__("Error Message"));
        //  return $this->_messageManager->addError(__("Error Message"));
        //  echo "hi";
        $id = $this->getRequest()->getParam('order_id');
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($id);
        $orderState = 'processing';     
        $order->setState($orderState)->setStatus('processing');
        $order->save();
        // Code to send email.
        $templateId = 8;

 

        /* Receiver Detail  */
        $store = $objectManager->create('Magento\Store\Model\ScopeInterface');
        $semail = $store->getValue('trans_email/ident_sales/name'ScopeInterface:COPE_STORE);
        $receiverInfo = [
            'name' => $semail,
            'email' => $semail
        ];
        //$name = $customerSession->getCustomer()->getName();
        /* Sender Detail  */
        $email = $order->getCustomerEmail();
        $name = $order->getCustomerFirstname() . " " . $order->getCustomerMiddlename() . " " . $order->getCustomerLastname();
        $senderInfo = [
            'name' => $name,
            'email' => $email
        ];

 

        $variablevalue1 = 'Tippanna Pawar';
        $variablevalue2 = "12345";
        /* Assign values for your template variables  */
        $emailTempVariables = array();
        $emailTempVariables['myvar1'] = $variablevalue1;
        $emailTempVariables['myvar2'] = $variablevalue2;

 

        /* We write send mail function in helper because if we want to 
       use same in other action then we can call it directly from helper */

 

        /* call send mail method from helper or where you define it*/
        $this->_objectManager->get('Tp\Emails\Helper\Email')->yourCustomMailSendMethod(
            $emailTempVariables,
            $senderInfo,
            $receiverInfo,
            $templateId
        );



        //End of email code
        //  $order->setStatus(\Magento\Sales\Model\Order:TATE_PENDING, true)->save();
        $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference again and again');
        // $this->_getSession()->addSuccess($this->__('The order state has been changed.'));
        $this->_redirect('sales/order/view'array('order_id' => $id));
    }

 

    /**
     * Check Permission.
     *
     * @return bool
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('MageVision_Blog27');
    }
}

Re: Get Store email addresses programmatically

@tippanna_pawar 

 

Change your email variable with :

$email = $this->scopeConfig->getValue('trans_email/ident_support/email',\Magento\Store\Model\ScopeInterface:Smiley FrustratedCOPE_STORE);
Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !