/** * Send email with order data * * @return $this */ public function sendNewOrderEmail() { $storeId = $this->getStore()->getId(); if (!$this->_salesData->canSendNewOrderEmail($storeId)) { return $this; } // Get the destination email addresses to send copies to $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO); $copyMethod = $this->_scopeConfig->getValue(self::XML_PATH_EMAIL_COPY_METHOD, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); $paymentBlockHtml = $this->_paymentData->getInfoBlockHtml($this->getPayment(), $storeId); // Retrieve corresponding email template id and customer name if ($this->getCustomerIsGuest()) { $templateId = $this->_scopeConfig->getValue(self::XML_PATH_EMAIL_GUEST_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); $customerName = $this->getBillingAddress()->getName(); } else { $templateId = $this->_scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId); $customerName = $this->getCustomerName(); } $this->_transportBuilder->setTemplateIdentifier($templateId)->setTemplateOptions(array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId))->setTemplateVars(array('order' => $this, 'billing' => $this->getBillingAddress(), 'payment_html' => $paymentBlockHtml, 'store' => $this->getStore()))->setFrom($this->_scopeConfig->getValue(self::XML_PATH_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId))->addTo($this->getCustomerEmail(), $customerName); if ($copyTo && $copyMethod == 'bcc') { // Add bcc to customer email foreach ($copyTo as $email) { $this->_transportBuilder->addBcc($email); } } /** @var \Magento\Framework\Mail\TransportInterface $transport */ $transport = $this->_transportBuilder->getTransport(); $transport->sendMessage(); // Email copies are sent as separated emails if their copy method is 'copy' if ($copyTo && $copyMethod == 'copy') { foreach ($copyTo as $email) { $this->_transportBuilder->setTemplateIdentifier($templateId)->setTemplateOptions(array('area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId))->setTemplateVars(array('order' => $this, 'billing' => $this->getBillingAddress(), 'payment_html' => $paymentBlockHtml, 'store' => $this->getStore()))->setFrom($this->_scopeConfig->getValue(self::XML_PATH_EMAIL_IDENTITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId))->addTo($email)->getTransport()->sendMessage(); } } $this->setEmailSent(true); $this->_getResource()->saveAttribute($this, 'email_sent'); return $this; }
/** * @dataProvider getScopeConfigValue * @return void */ public function testCanSendNewOrderEmail($scopeConfigValue) { $this->setupScopeConfigIsSetFlag(\Magento\Sales\Model\Order\Email\Container\OrderIdentity::XML_PATH_EMAIL_ENABLED, $scopeConfigValue); $this->assertEquals($scopeConfigValue, $this->helper->canSendNewOrderEmail($this->storeMock)); }