Is it possible to send email to customer when he place an order

this is my event.xml

<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
   <event name='checkout_onepage_controller_success_action'>

and this is my observer


namespace Pulsestorm\HelloWorldMVVM\Observer;

use Magento\Framework\Event\ObserverInterface;

class HelloWorld implements ObserverInterface
     * @var ObjectManagerInterface
    protected $_objectManager;
    protected $_orderRepositoryInterface;

     * @param \Magento\Framework\ObjectManagerInterface $objectManager
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepositoryInterface
    ) {
        $this->_objectManager = $objectManager;
        $this->_orderRepositoryInterface = $orderRepositoryInterface;

     * customer register event handler
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(\Magento\Framework\Event\Observer $observer)

    $orderIds = $observer->getEvent()->getOrderIds();
    $order = $this->_orderRepositoryInterface->get($orderIds[0]);
    $items =$order->getAllVisibleItems();
    $productIds = array();
    foreach($items as $item) {
        $productIds[]= $item->getProductId();

    //custoemr id
    $customerEmail = $order->getCustomerEmail();



In $customerEmail i got customer mail id after that i have no idea how to send email using SMTP


I hope you already configure smtp. if not please refer this link for send mail using your smtp.

key code of sending mail

use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
class ClassName implements ObserverInterface
protected $storeManager;
protected $_transportBuilder;
protected $inlineTranslation;
public function __construct(StoreManagerInterface $smi,TransportBuilder $tb,StateInterface $si)
public function yourFunctionName()
       $templateOptions = array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->storeManager->getStore()->getId());
        $templateVars = array(
            'store' => $this->storeManager->getStore(),
            'customer_name' => $cname,
            'message'   => 'We processed your order ID '.$orederid.'. We will contact you soon in mail for the acknowledgement if you not receive mail within 4 hours please get help from support@xxx.com'
        $from = array('email' => "xxxxx@xxx.xxx", 'name' => 'AnyName');
        $to = array('xxxxx@xxx.xxx');
        $transport = $this->_transportBuilder->setTemplateIdentifier('order_template')

please note it setTemplateIdentifier('order_template') so you need to create order_template. for create email template refer this link

  • Exception #0 (Magento\Framework\Exception\MailException): 5.7.0 Must issue a STARTTLS command first. s63sm41970461pfb.52 - gsmtp – Ramkishan Suthar Sep 15 '16 at 6:56
  • plz help this error is coming after using your code – Ramkishan Suthar Sep 15 '16 at 6:57
  • I thought it is a mail configuration issue not code so refer web.liferay.com/community/forums/-/message_boards/message/… . here only the problem or the entire system does not send mail? let test with subcribe mail in footer form and check did you receive any mail from your store? if not, definitely you are not configure smtp properly – Bilal Usean Sep 15 '16 at 7:08
  • thank you @Bilal for your help now my code is running very well except template is not showing on mail (blank email) . I will check my code again – Ramkishan Suthar Sep 15 '16 at 7:18
  • plz tell why blank email is sent. i have created a template with template id "order_template" in email_templates.xml in etc folder – Ramkishan Suthar Sep 15 '16 at 7:28

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.