Method setFrom is deprecated
Declared in Magento\Framework\Mail\Template\TransportBuilder
Is there any alternative?
2 Answers
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.
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 {
...
}
}
}