Please can any one help me on this, I would like to disable email notofication for customers, which have a flag non_email_customers set to true. I Have Try this way. i have created a plugin for: Magento\Framework\Mailsend\TransportInterface::sendMessage()

but I don't know how to get customer object there. Getting it from customer session is not the best solution I suppose beacuse of mail from adminhtml area. Any ideas how to resolve this issue?

1 Answer 1


You can get a list of all the customers with this code -

public function __construct(
    \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
) {
    $this->_customerFactory = $customerFactory;

 * Get customer collection
public function getCustomerCollection()
    return $this->_customerFactory->create();

Use this as -

$customerCollection = $this->getCustomerCollection();

foreach ($customerCollection as $customer) {
    echo $customer->getEmail(); //Or any other method

Since non_email_customers sounds like a user defined field, you should be able to access it as $customer->getDataByKey('non_email_customers');

To get a list of all the methods available on the $customer object, use get_class_methods($customer)

Hope this helps!

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.