8

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('[email protected]');
$mail->setBody($html);
$mail->setSubject('Test email : '.$fieldss['o']);
$mail->setfromemail('[email protected]');
$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('[email protected]', 'Hiren Shah');
//$transport->settoemail('[email protected]');
//$transport->setBody($html);
$transport->setSubject('Test email : '.$fieldss['o']);
$transport->setFrom('[email protected]');
$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 Commented Dec 4, 2019 at 3:07

3 Answers 3

10

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

    $to = ['[email protected]', '[email protected]'];
    $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
    Commented Jun 28, 2019 at 7:23
  • Thanks for mark as a right answer. :)
    – Ravi Soni
    Commented Jun 28, 2019 at 7:29
  • @RaviSoni how to send email to multiple users can u advise? Commented Nov 21, 2019 at 3:54
  • Check now I have added array for variable $to
    – Ravi Soni
    Commented Nov 21, 2019 at 5:50
  • thanks @RaviSoni Commented Nov 21, 2019 at 9:15
9

Send email programmatically without email template, code is tested in Magento 2.4

/**
 * @var \Magento\Framework\Mail\MessageInterfaceFactory
 */
protected $messageInterfaceFactory;

/**
 * @var \Magento\Framework\Mail\TransportInterfaceFactory
 */
protected $mailTransportFactory;

/**
 * @param \Magento\Framework\Mail\MessageInterfaceFactory $messageInterfaceFactory
 * @param \Magento\Framework\Mail\TransportInterfaceFactory $mailTransportFactory
 */
public function __construct(
    \Magento\Framework\Mail\MessageInterfaceFactory $messageInterfaceFactory,
    \Magento\Framework\Mail\TransportInterfaceFactory $mailTransportFactory
) {
    $this->messageInterfaceFacory = $messageInterfaceFactory;
    $this->mailTransportFactory = $mailTransportFactory;
}

public function execute()
{
    $from = '[email protected]';
    $to = '[email protected]';
    $subject = "email subject";
    $emailHtml = "<h1>Test Email</h1>";

    try {
        $message = $this->messageInterfaceFactory->create();
        $message->setFromAddress($from);
        $message->addTo($to);
        $message->setSubject($subject);
        $message->setBodyHtml($emailHtml);
        $transport = $this->mailTransportFactory->create(['message' => $message]);
        $transport->sendMessage();

    } catch (\Exception $e) {
        echo "error=>".$e->getMessage();
    }
}
2
  • Working good... :) Commented Feb 10, 2023 at 2:26
  • 1
    Need to be changed in the constructor messageInterfaceFacory -> messageInterfaceFactory because it produces an error. Commented Aug 4, 2023 at 8:37
4

You can send mail using Zend_Mail() function.

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

// 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();
3
  • Thanks for the answer. Working fine.
    – Hiren Shah
    Commented 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 Commented Dec 4, 2019 at 3:07
  • Check zend-email installed or not? follow below link stackoverflow.com/questions/26140870/… Commented Dec 5, 2019 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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