Skip to content

Instantly share code, notes, and snippets.

@alepane21
Last active October 6, 2023 09:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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('mail.recipient@example.com');
$message->addTo('mail.tosent@example.com');
$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