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);
}