Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DomPixie/06ac638be7e2680f1b04621ab0ed3b9b to your computer and use it in GitHub Desktop.
Save DomPixie/06ac638be7e2680f1b04621ab0ed3b9b to your computer and use it in GitHub Desktop.
Magento 2 send email without transport builder or template command line
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('mail.recipient@example.com');
        $message->addTo('mail.tosent@example.com');
        $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