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.
-
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.phpNagaraju Kasa– Nagaraju Kasa2019年12月04日 03:07:56 +00:00Commented Dec 4, 2019 at 3:07
3 Answers 3
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();
-
Thanks for the answer. Working fine.Hiren Shah– Hiren Shah2019年06月28日 07:23:46 +00:00Commented Jun 28, 2019 at 7:23
-
Thanks for mark as a right answer. :)Ravi Soni– Ravi Soni2019年06月28日 07:29:11 +00:00Commented Jun 28, 2019 at 7:29
-
@RaviSoni how to send email to multiple users can u advise?Nagaraju Kasa– Nagaraju Kasa2019年11月21日 03:54:11 +00:00Commented Nov 21, 2019 at 3:54
-
Check now I have added array for variable $toRavi Soni– Ravi Soni2019年11月21日 05:50:19 +00:00Commented Nov 21, 2019 at 5:50
-
Thanks, For upvote :)Ravi Soni– Ravi Soni2019年11月21日 10:28:10 +00:00Commented Nov 21, 2019 at 10:28
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();
}
}
-
Working good... :)Jimit Bhavsar– Jimit Bhavsar2023年02月10日 02:26:50 +00:00Commented Feb 10, 2023 at 2:26
-
1Need to be changed in the constructor messageInterfaceFacory -> messageInterfaceFactory because it produces an error.Duddy Woody– Duddy Woody2023年08月04日 08:37:33 +00:00Commented Aug 4, 2023 at 8:37
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();
-
Thanks for the answer. Working fine.Hiren Shah– Hiren Shah2019年06月28日 07:23:54 +00:00Commented 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.phpNagaraju Kasa– Nagaraju Kasa2019年12月04日 03:07:25 +00:00Commented Dec 4, 2019 at 3:07
-
Check zend-email installed or not? follow below link stackoverflow.com/questions/26140870/…Arunprabakaran M– Arunprabakaran M2019年12月05日 07:23:58 +00:00Commented Dec 5, 2019 at 7:23
Explore related questions
See similar questions with these tags.