3

I have this observer, and now i send emails via php mail() function, but i want to use the SMTP. Can anyone tell me which steep i need to follow?

public function sendNotification($to,$subject,$products,$shippingAddress) 
    {       
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <[email protected]>' . "\r\n";

        $message = '
        <html>
        <head>
        <title>Email notification</title>
        </head>
        <body>
            <h1>Customer data</h1>
            <p>
                '.__('Name').$shippingAddress['firstname'].' '.$shippingAddress['lastname'].': <br />
                '.__('Phone').$shippingAddress['telephone'].': <br />
                '.__('Email').$shippingAddress['telephone'].': <br />
                '.__('Address').$shippingAddress['street'].' '.$shippingAddress['city'].' '.$shippingAddress['postcode'].' '.': <br />
            </p>
            <h2>Order details</h1>
            <table>
                '.$products.'
            </table>
        </body>
        </html>';
        mail($to,$subject,$message,$headers);
    }

    /* send email notification for both, rent & lend products */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $order_id = $order->getIncrementId();

        $shippingAddress = $order->getShippingAddress(); // shipping address
        $customer = $order->getCustomerId(); // using this id you can get customer name

        $productsRent = '';
        $productsLend = '';
        /* parse all products list from the order */
        foreach($order->getAllItems() as $item){ 
            $ProdustIds = $item->getProductId();
            $attribute_name = $this->getAttributeSetName($ProdustIds);

            if ($attribute_name=='rent') {
                $productName = $item->getName();
                $rent_price = $item->getData('rent_price');
                $rent_time = $item->getData('rent_time');

                $productsRent .= '<tr>
                    <td>'.$productName.'</td>
                    <td>'.$rent_price.' '.$rent_time.'</td>
                </tr>';
            } elseif ($attribute_name=='lend') {
                $productName = $item->getName();

                $productsLend .= '<tr>
                    <td>'.$productName.'</td>
                    <td>Gratis</td>
                    </tr>';
            } 
       }

       // send emails notification rent
       if($productsRent!='') {
            $this->sendNotification("[email protected]","Rent email notification",$productsRent,$shippingAddress);
       }
3

1 Answer 1

0

You can download free smtp extension of Magento 2. And send your custom email.

magento2-gmailsmtpapp

1
  • i am using this extension but i am getting error Command not implemented Jun 17, 2018 at 10:16

Your Answer

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

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