0

Method setFrom is deprecated
Declared in Magento\Framework\Mail\Template\TransportBuilder
Is there any alternative? enter image description here

2 Answers 2

1

Yes, the alternative is setFromByScope. In fact setFrom is now an alias of it.

For instance, the test file vendor/magento/magento2-ee-base/dev/tests/integration/testsuite/Magento/Rma/Controller/Returns/SubmitTest.php makes use of the scope related method.

1
  • Great, Thank you! Commented Sep 5, 2022 at 7:20
0

With new implementation, magento 2.4 is recommended setFromByScope as an alternative.

Change:

  • ->setFrom($this->contactsConfig->emailSender())

To

  • ->setFromByScope($this->contactsConfig->emailSender())

<?php
declare(strict_types=1);

namespace Module\Test\Model;
use Magento\Framework\Mail\Template\TransportBuilder;

/**
 * @var TransportBuilder
*/
private $transportBuilder;

class Main implements MailInterface
{
    /**
     * Initialize dependencies.
     * @param TransportBuilder $transportBuilder
     */
    public function __construct(
        TransportBuilder $transportBuilder
    ) {
        $this->transportBuilder = $transportBuilder;
    }

    public function send($replyTo, array $variables)
    {
       ...
        try {
            $transportBuilder = $this->transportBuilder
               ...
                ->setFromByScope($this->contactsConfig->emailSender())
               ...
            $transportBuilder->sendMessage();
        } finally {
            ...
        }
    }
 }
1

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.