Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DominicWatts/727852c4039f3213ab14a76215eeb202 to your computer and use it in GitHub Desktop.
Save DominicWatts/727852c4039f3213ab14a76215eeb202 to your computer and use it in GitHub Desktop.
Magento 2 : send email without transport builder or template command line #magento2
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\Mail\Message;

[...]

    protected $mailTransportFactory;

    public function __construct(
        TransportInterfaceFactory $mailTransportFactory
    ) {
        $this->mailTransportFactory = $mailTransportFactory;
    }

[...]

    public function sendEmail()
    {
        $message = new Message();
        $message->setFrom('[email protected]');
        $message->addTo('[email protected]');
        $message->setSubject('Subject');
        $message->setBody('Body');
        $transport = $this->mailTransportFactory->create(['message' => $message]);
        try {
            $transport->sendMessage();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment