send a custom email by the controller in Magento 2

send custom email by controller in magento 2

send custom email by controller in magento 2

Add the following code in your controller file:
$firstname = "firstnametext";
$lastname = "lastnametext";

// Send Mail functionality starts from here 
$from = "[email protected]";
$nameFrom = "From Name";
$to = "[email protected]";
$nameTo = "To Name";
$body = "
<div>
    <b>".$firstname."</b>
    <i>".$lastname."</i>
</div>";

$email = new \Zend_Mail();
$email->setSubject("Email Subject"); 
$email->setBodyHtml($body);     // use it to send html data
//$email->setBodyText($body);   // use it to send simple text data
$email->setFrom($from, $nameFrom);
$email->addTo($to, $nameTo);
$email->send();

send custom email by controller in magento 2