Last active
October 6, 2023 09:59
-
-
Save alepane21/372ba87dc191cf32aaba99d4ce091c1c to your computer and use it in GitHub Desktop.
Example to send email without template on Magento 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Hevelop\Example\Console\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Magento\Framework\Mail\TransportInterfaceFactory; | |
/** | |
* Class SendMailWithoutTemplate | |
*/ | |
class SendMailWithoutTemplate extends Command | |
{ | |
/** | |
* @var TransportInterfaceFactory | |
*/ | |
protected $mailTransportFactory; | |
/** | |
* @param ResourceConnection $dbConnection | |
* @throws \LogicException | |
*/ | |
public function __construct( | |
TransportInterfaceFactory $mailTransportFactory | |
) | |
{ | |
$this->mailTransportFactory = $mailTransportFactory; | |
parent::__construct(); | |
} | |
/** | |
* Configures the current command. | |
* @throws \InvalidArgumentException | |
*/ | |
protected function configure() | |
{ | |
$this->setName('example:sendmail') | |
->setDescription('Send a simple mail'); | |
parent::configure(); | |
} | |
public function execute() | |
{ | |
$message = new \Magento\Framework\Mail\Message(); | |
$message->setFrom('[email protected]'); | |
$message->addTo('[email protected]'); | |
$message->setSubject('Subject'); | |
$message->setBody('Body'); | |
$transport = $this->mailTransportFactory->create(['message' => $message]); | |
$transport->sendMessage(); | |
} | |
} |
\Magento\Framework\Mail\Message is deprecated since 102.0.4
\Magento\Framework\Mail\EmailMessage should be used instead
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there is error, update to this :
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
public function execute(InputInterface $input, OutputInterface $output)