cancel
Showing results for 
Search instead for 
Did you mean: 

Get Store email addresses programmatically

SOLVED

Get Store email addresses programmatically

Hi,

In my custom controller want to get store email addresses programmatically for email sender info. Please suggest ASAP

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get Store email addresses programmatically

@tippanna_pawar 

 

Resolved this error by this piece of code :

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface');
$email = $scopeConfig->getValue('trans_email/ident_support/email',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

View solution in original post

21 REPLIES 21

Re: Get Store email addresses programmatically

Hi @tippanna_pawar 

Try the below stack solution.

https://magento.stackexchange.com/a/125449

I hope it will help you!

Re: Get Store email addresses programmatically

Hi @tippanna_pawar 

Try below code :

<?php
namespace Custom\Module\Controller\Index;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $request;
    protected $scopeConfig;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Request\Http $request,
    ScopeConfigInterface $scopeConfig
){
    parent::__construct($context);
    $this->scopeConfig = $scopeConfig;
    $this->request = $request;
}

public function execute()
{
    $email = $this->scopeConfig->getValue('trans_email/ident_support/email',ScopeInterface::SCOPE_STORE);
    $name  = $this->scopeConfig->getValue('trans_email/ident_support/name',ScopeInterface::SCOPE_STORE);

    echo $email;echo "<br/>";
    echo $name;echo "<br/>";

}

It may help you to get store email id.
If issue resolve, please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Get Store email addresses programmatically

Hi Bhanu,

 

Tried above code ,Throws an error as per the above post.

Re: Get Store email addresses programmatically

Hi @tippanna_pawar ,

 

To get the store email address programmatically, please follow @Vimal Kumar suggestion:

return $this->_scopeConfig->getValue(
            'trans_email/ident_sales/name', 
\Magento\Store\Model\ScopeInterface
::SCOPE_STORE );

 Try replacing the highlighted string with others String if you want other Email  address as well:

 

General :- ident_general

Sales:- ident_sales

Customer Support:- ident_support

Custom Email1:- ident_custom1

Custom Email2:- ident_custom2

 

Screenshot from 2020-07-02 16-00-18.png

 

Hope it helps you.

Problem Solved! Click Kudos & Accept as Solution!

Re: Get Store email addresses programmatically

Vimal kumar post is on block but when i use code in controller it is not working.Please give me controller code for this issue. Even bhanu post is not working...

Re: Get Store email addresses programmatically

Hi @tippanna_pawar 

Please try this :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager = $objectManager->create('Magento\Store\Model\ScopeInterface');
$email = $objectManager->getValue('trans_email/ident_sales/name',ScopeInterface::SCOPE_STORE);
echo $email;

It show only email if saved with configuration.

If issue resolved, please click on 'Kudos' & Accept as Solution!

 

Problem solved? Click Accept as Solution!

Re: Get Store email addresses programmatically

Hi @tippanna_pawar,

Can you please share the screenshot of the issue which you are facing. After adding code in your controller please run below command.

php bin/magento s:d:c 

 

Thanks!

Problem Solved! Click Kudos & Accept as Solution!

Re: Get Store email addresses programmatically

@tippanna_pawar 

 

Assuming you've already run these commands after pasting code :

 

php bin/magento setup:di:compile

php bin/magento c:f

 

also share the related error.

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Get Store email addresses programmatically

I used code as per the instruction but thown 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