|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Fruitcake\EmailAdvancedConfig\Model; |
| 9 | + |
| 10 | +use Fruitcake\EmailAdvancedConfig\Helper\Data; |
| 11 | +use Laminas\Mail\Transport\Smtp; |
| 12 | +use Laminas\Mail\Transport\SmtpOptions; |
| 13 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 14 | +use Magento\Framework\Exception\MailException; |
| 15 | +use Magento\Framework\Mail\MessageInterface; |
| 16 | +use Magento\Framework\Mail\TransportInterface; |
| 17 | +use Magento\Framework\Phrase; |
| 18 | +use Magento\Store\Model\ScopeInterface; |
| 19 | +use Laminas\Mail\Message; |
| 20 | +use Laminas\Mail\Transport\Sendmail; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class that responsible for filling some message data before transporting it. |
| 24 | + * @see \Laminas\Mail\Transport\Smtp is used for transport |
| 25 | + * @see \Magento\Email\Model\Transport for the original Magento implementation. This overrides the construct |
| 26 | + */ |
| 27 | +class SmtpTransport implements TransportInterface |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Configuration path to source of Return-Path and whether it should be set at all |
| 31 | + * @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values |
| 32 | + */ |
| 33 | + const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Configuration path for custom Return-Path email |
| 37 | + */ |
| 38 | + const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email'; |
| 39 | + |
| 40 | + /** |
| 41 | + * Whether return path should be set or no. |
| 42 | + * |
| 43 | + * Possible values are: |
| 44 | + * 0 - no |
| 45 | + * 1 - yes (set value as FROM address) |
| 46 | + * 2 - use custom value |
| 47 | + * |
| 48 | + * @var int |
| 49 | + */ |
| 50 | + private $isSetReturnPath; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var string|null |
| 54 | + */ |
| 55 | + private $returnPathValue; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var Sendmail |
| 59 | + */ |
| 60 | + private $laminasTransport; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var MessageInterface |
| 64 | + */ |
| 65 | + private $message; |
| 66 | + |
| 67 | + /** @var Data */ |
| 68 | + private $helper; |
| 69 | + |
| 70 | + /** |
| 71 | + * @param MessageInterface $message Email message object |
| 72 | + * @param ScopeConfigInterface $scopeConfig Core store config |
| 73 | + * @param null|string|array|\Traversable $parameters Config options for sendmail parameters |
| 74 | + */ |
| 75 | + public function __construct( |
| 76 | + MessageInterface $message, |
| 77 | + ScopeConfigInterface $scopeConfig, |
| 78 | + Data $helper, |
| 79 | + $parameters = null |
| 80 | + ) { |
| 81 | + $this->isSetReturnPath = (int) $scopeConfig->getValue( |
| 82 | + self::XML_PATH_SENDING_SET_RETURN_PATH, |
| 83 | + ScopeInterface::SCOPE_STORE |
| 84 | + ); |
| 85 | + $this->returnPathValue = $scopeConfig->getValue( |
| 86 | + self::XML_PATH_SENDING_RETURN_PATH_EMAIL, |
| 87 | + ScopeInterface::SCOPE_STORE |
| 88 | + ); |
| 89 | + $this->helper = $helper; |
| 90 | + |
| 91 | + $this->message = $message; |
| 92 | + |
| 93 | + // When not enabled, fall back to Sendmail transport |
| 94 | + if (! $this->helper->isEnabled()) { |
| 95 | + throw new \RuntimeException('Should be enabled'); |
| 96 | + $this->laminasTransport = new Sendmail($parameters); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + $options = [ |
| 101 | + 'name' => 'localhost', |
| 102 | + 'host' => $this->helper->getConfig('smtp/host'), |
| 103 | + 'port' => $this->helper->getConfig('smtp/port'), |
| 104 | + 'connection_class' => $this->helper->getConfig('smtp/auth'), |
| 105 | + 'connection_config' => [ |
| 106 | + 'username' => $this->helper->getConfig('smtp/username'), |
| 107 | + 'password' => $this->helper->getConfig('smtp/password'), |
| 108 | + ] |
| 109 | + ]; |
| 110 | + |
| 111 | + $ssl = $this->helper->getConfig('smtp/ssl'); |
| 112 | + |
| 113 | + if ($ssl && $ssl !== 'none') { |
| 114 | + $connectionConfig['ssl'] = $ssl; |
| 115 | + } |
| 116 | + |
| 117 | + $this->laminasTransport = new Smtp(new SmtpOptions($options)); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @inheritdoc |
| 122 | + */ |
| 123 | + public function sendMessage() |
| 124 | + { |
| 125 | + try { |
| 126 | + $laminasMessage = Message::fromString($this->message->getRawMessage())->setEncoding('utf-8'); |
| 127 | + if (2 === $this->isSetReturnPath && $this->returnPathValue) { |
| 128 | + $laminasMessage->setSender($this->returnPathValue); |
| 129 | + } elseif (1 === $this->isSetReturnPath && $laminasMessage->getFrom()->count()) { |
| 130 | + $fromAddressList = $laminasMessage->getFrom(); |
| 131 | + $fromAddressList->rewind(); |
| 132 | + $laminasMessage->setSender($fromAddressList->current()->getEmail()); |
| 133 | + } |
| 134 | + |
| 135 | + $this->laminasTransport->send($laminasMessage); |
| 136 | + } catch (\Exception $e) { |
| 137 | + throw new MailException(new Phrase($e->getMessage()), $e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @inheritdoc |
| 143 | + */ |
| 144 | + public function getMessage() |
| 145 | + { |
| 146 | + return $this->message; |
| 147 | + } |
| 148 | +} |
0 commit comments