Skip to content

Instantly share code, notes, and snippets.

@alepane21
Last active October 6, 2023 09:59
Show Gist options
  • Save alepane21/372ba87dc191cf32aaba99d4ce091c1c to your computer and use it in GitHub Desktop.
Save alepane21/372ba87dc191cf32aaba99d4ce091c1c to your computer and use it in GitHub Desktop.
Example to send email without template on Magento 2
<?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();
}
}
@mohfahrul
Copy link

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)

@avelikduswt
Copy link

\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