6

I want to send custom email programmatically without any template.

In magento 1, I used below script to do same thing.

$mail = Mage::getModel('core/email');
$mail->setToName('xxx');
$mail->settoemail('xxx@yyy.com');
$mail->setBody($html);
$mail->setSubject('Test email : '.$fieldss['o']);
$mail->setfromemail('from@yopmail.com');
$mail->setType('html');// YOu can use Html or text as Mail format
$mail->setBodyHTML($html);  // your content or message
try{ 
    $mail->send();
}catch (Exception $e) {
    echo $e->getMessage();exit;
}

Here, In magento 2 I used below code but it gives error.

$objectManager = $this->getObjectManager();
$transport = $objectManager->get('Magento\Framework\Mail\Template\TransportBuilder');
$transport->addTo('xxx@yyy.com', 'Hiren Shah');
//$transport->settoemail('hiren.shah@healthtrader.com');
//$transport->setBody($html);
$transport->setSubject('Test email : '.$fieldss['o']);
$transport->setFrom('from@yopmail.com');
$transport->setType('html');// YOu can use Html or text as Mail format
$transport->setBodyHTML($html);  // your content or message
$transport->getTransport();
try { 
    $transport->sendMessage();
} catch (Exception $e) {
    echo $e->getMessage();exit;
}

But this gives me error Call to undefined method Magento\Framework\Mail\Template\TransportBuilder\Interceptor::setSubject()

How to send custom email without template? Please give me code sample.

1
  • can you advise me on this I have tried this on Magento 2 but getting an error Fatal error: Fatal error: Uncaught Error: Class 'Zend_Mail' not found in /srv/public_html/pub/test.php Dec 4, 2019 at 3:07

2 Answers 2

9

Magento 2: The simplest way I found to send a plain text - use Zend1 mail

    $to = ['email1@test.com', 'email2@test.com'];
    $email = new \Zend_Mail();
    $email->setSubject("Feedback email");
    $email->setBodyText($body);
    $email->setFrom($from, $nameFrom);
    $email->addTo($to, $nameTo);
    $email->send();
10
  • Thanks for the answer. Working fine.
    – Hiren Shah
    Jun 28, 2019 at 7:23
  • Thanks for mark as a right answer. :)
    – Ravi Soni
    Jun 28, 2019 at 7:29
  • @RaviSoni how to send email to multiple users can u advise? Nov 21, 2019 at 3:54
  • Check now I have added array for variable $to
    – Ravi Soni
    Nov 21, 2019 at 5:50
  • thanks @RaviSoni Nov 21, 2019 at 9:15
4

You can send mail using Zend_Mail() function.

$firstname = "xxx";
$lastname = "yyyyyy";

// Send Mail functionality starts from here 
$from = "from_address@example.com";
$nameFrom = "From Name";
$to = "to_address@example.com";
$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();
3
  • Thanks for the answer. Working fine.
    – Hiren Shah
    Jun 28, 2019 at 7:23
  • I have tried this on Magento but getting an error Fatal error: Fatal error: Uncaught Error: Class 'Zend_Mail' not found in /srv/public_html/pub/test.php Dec 4, 2019 at 3:07
  • Check zend-email installed or not? follow below link stackoverflow.com/questions/26140870/… Dec 5, 2019 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.