Skip to content

Instantly share code, notes, and snippets.

@zaheerbadi
Created December 9, 2019 09:00
Show Gist options
  • Save zaheerbadi/306f1f69cf97b6542c8e49d12245c1a0 to your computer and use it in GitHub Desktop.
Save zaheerbadi/306f1f69cf97b6542c8e49d12245c1a0 to your computer and use it in GitHub Desktop.
Email preview magento2
<?php
namespace Vendor\Extenssion\Helper;
use Magento\Framework\App\Area;
class Emailpreivew extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Framework\Translate\Inline\StateInterface
*/
protected $inlineTranslation;
/**
* @var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
protected $orderRepository;
protected $shipmentModel;
/**
* @param Magento\Framework\App\Helper\Context $context
* @param Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param Magento\Store\Model\StoreManagerInterface $storeManager
* @param Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
* @param Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Sales\Model\Order\Shipment $shipmentModel,
\Magento\Framework\App\State $state
) {
$this->_scopeConfig = $context;
parent::__construct($context);
$this->_storeManager = $storeManager;
$this->inlineTranslation = $inlineTranslation;
$this->_transportBuilder = $transportBuilder;
$this->orderRepository = $orderRepository;
$this->shipmentModel = $shipmentModel;
$state->setAreaCode('frontend');
}
/**
* @param string $path
* @param int $storeId
* @return mixed
*/
protected function getConfigValue($path, $storeId)
{
return $this->scopeConfig->getValue(
$path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
}
public function getOrder($id)
{
return $this->orderRepository->get($id);
}
public function getShipment($id)
{
return $this->shipmentModel->load($id);
}
/**
* Return store
*
* @return Store
*/
public function getStore()
{
return $this->_storeManager->getStore();
}
public function sendEmailReport(
$template
) {
$this->inlineTranslation->suspend();
$templateId = $this->getConfigValue($template, $this->getStore()->getStoreId());
$sender = ['name' => 'test', 'email' => '[email protected]'];
$templateParams['order'] = $this->getOrder(201);
$templateParams['shipment'] = $this->getShipment(2);
$transport = $this->_transportBuilder->setTemplateIdentifier($templateId)
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $this->getStore()->getId(),
]
)
->setTemplateVars($templateParams)
->setFrom($sender)
->addTo('[email protected]')
->getTransport();
echo '<pre>';print_r($transport->getMessage()->getBody()->generateMessage());exit;
$transport->sendMessage();
$this->inlineTranslation->resume();
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment