|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Lof\Webp2\Controller\Test; |
| 5 | + |
| 6 | +use Magento\Framework\App\Action\Action; |
| 7 | +use Magento\Framework\App\Action\Context; |
| 8 | +use Magento\Framework\App\Area; |
| 9 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 10 | +use Magento\Framework\App\DeploymentConfig; |
| 11 | +use Magento\Framework\App\RequestInterface; |
| 12 | +use Magento\Framework\Controller\Result\Json; |
| 13 | +use Magento\Framework\Controller\Result\JsonFactory; |
| 14 | +use Magento\Framework\Controller\ResultInterface; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\Exception\MailException; |
| 17 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 18 | +use Magento\Framework\Mail\Template\TransportBuilder; |
| 19 | +use Magento\Framework\View\Result\LayoutFactory; |
| 20 | +use Magento\Framework\View\Result\Page; |
| 21 | +use Magento\Store\Model\StoreManagerInterface; |
| 22 | + |
| 23 | +class Mail extends Action |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var TransportBuilder |
| 27 | + */ |
| 28 | + private $transportBuilder; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var StoreManagerInterface |
| 32 | + */ |
| 33 | + private $storeManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var LayoutFactory |
| 37 | + */ |
| 38 | + private $layoutFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var RequestInterface |
| 42 | + */ |
| 43 | + private $request; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var JsonFactory |
| 47 | + */ |
| 48 | + private $jsonFactory; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ScopeConfigInterface |
| 52 | + */ |
| 53 | + private $scopeConfig; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var DeploymentConfig |
| 57 | + */ |
| 58 | + private $config; |
| 59 | + |
| 60 | + /** |
| 61 | + * Mail constructor. |
| 62 | + * @param TransportBuilder $transportBuilder |
| 63 | + * @param StoreManagerInterface $storeManager |
| 64 | + * @param LayoutFactory $layoutFactory |
| 65 | + * @param RequestInterface $request |
| 66 | + * @param JsonFactory $jsonFactory |
| 67 | + * @param ScopeConfigInterface $scopeConfig |
| 68 | + * @param DeploymentConfig $config |
| 69 | + * @param Context $context |
| 70 | + */ |
| 71 | + public function __construct( |
| 72 | + TransportBuilder $transportBuilder, |
| 73 | + StoreManagerInterface $storeManager, |
| 74 | + LayoutFactory $layoutFactory, |
| 75 | + RequestInterface $request, |
| 76 | + JsonFactory $jsonFactory, |
| 77 | + ScopeConfigInterface $scopeConfig, |
| 78 | + DeploymentConfig $config, |
| 79 | + Context $context |
| 80 | + ) { |
| 81 | + parent::__construct($context); |
| 82 | + $this->transportBuilder = $transportBuilder; |
| 83 | + $this->storeManager = $storeManager; |
| 84 | + $this->layoutFactory = $layoutFactory; |
| 85 | + $this->request = $request; |
| 86 | + $this->jsonFactory = $jsonFactory; |
| 87 | + $this->scopeConfig = $scopeConfig; |
| 88 | + $this->config = $config; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return ResultInterface |
| 93 | + * @throws LocalizedException |
| 94 | + * @throws MailException |
| 95 | + * @throws NoSuchEntityException |
| 96 | + */ |
| 97 | + public function execute(): ResultInterface |
| 98 | + { |
| 99 | + $token = (string)$this->request->getParam('token'); |
| 100 | + $cryptKey = (string)$this->config->get('crypt/key'); |
| 101 | + if ($token === $cryptKey) { |
| 102 | + return $this->sendResult(['msg' => 'Invalid token']); |
| 103 | + } |
| 104 | + |
| 105 | + $emailTemplate = (string)$this->request->getParam('email'); |
| 106 | + |
| 107 | + if (empty($emailTemplate)) { |
| 108 | + $emailTemplate = 'sales_email_invoice_template'; |
| 109 | + } |
| 110 | + |
| 111 | + $receiverInfo = [ |
| 112 | + 'name' => $this->scopeConfig->getValue('trans_email_ident/general/name'), |
| 113 | + 'email' => $this->scopeConfig->getValue('trans_email_ident/general/email'), |
| 114 | + ]; |
| 115 | + |
| 116 | + $senderInfo = [ |
| 117 | + 'name' => $this->scopeConfig->getValue('trans_email_ident/general/name'), |
| 118 | + 'email' => $this->scopeConfig->getValue('trans_email_ident/general/email'), |
| 119 | + ]; |
| 120 | + |
| 121 | + $data = [ |
| 122 | + 'template' => $emailTemplate, |
| 123 | + 'sender' => $senderInfo, |
| 124 | + 'receiver' => $receiverInfo |
| 125 | + ]; |
| 126 | + |
| 127 | + $variables = []; |
| 128 | + $this->transportBuilder->setTemplateIdentifier($emailTemplate) |
| 129 | + ->setTemplateOptions( |
| 130 | + [ |
| 131 | + 'area' => Area::AREA_FRONTEND, |
| 132 | + 'store' => $this->storeManager->getStore()->getId(), |
| 133 | + ] |
| 134 | + ) |
| 135 | + ->setTemplateVars($variables) |
| 136 | + ->setFrom($senderInfo) |
| 137 | + ->addTo($receiverInfo['email'], $receiverInfo['name']); |
| 138 | + |
| 139 | + $transport = $this->transportBuilder->getTransport(); |
| 140 | + $transport->sendMessage(); |
| 141 | + $data['msg'] = 'Email sent'; |
| 142 | + |
| 143 | + return $this->sendResult($data); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @param mixed[] $data |
| 148 | + * @return ResultInterface |
| 149 | + */ |
| 150 | + private function sendResult(array $data): ResultInterface |
| 151 | + { |
| 152 | + $jsonResult = $this->jsonFactory->create(); |
| 153 | + $jsonResult->setData($data); |
| 154 | + return $jsonResult; |
| 155 | + } |
| 156 | +} |
0 commit comments