|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Email\Model; |
| 7 | + |
| 8 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 9 | +use Magento\Framework\Exception\MailException; |
| 10 | +use Magento\Framework\Mail\MessageInterface; |
| 11 | +use Magento\Framework\Mail\TransportInterface; |
| 12 | +use Magento\Store\Model\ScopeInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class that responsible for filling some message data before transporting it. |
| 16 | + * This class checks whether this email should be sent in the first place, based on System Configurations. |
| 17 | + * @see Zend_Mail_Transport_Sendmail is used for transport |
| 18 | + */ |
| 19 | +class Transport implements TransportInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Config path to mail sending setting that shows if email communications are disabled |
| 23 | + */ |
| 24 | + const XML_PATH_SYSTEM_SMTP_DISABLE = 'system/smtp/disable'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Configuration path to source of Return-Path and whether it should be set at all |
| 28 | + * @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values |
| 29 | + */ |
| 30 | + const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Configuration path for custom Return-Path email |
| 34 | + */ |
| 35 | + const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Object for sending eMails |
| 39 | + * |
| 40 | + * @var \Zend_Mail_Transport_Sendmail |
| 41 | + */ |
| 42 | + private $transport; |
| 43 | + |
| 44 | + /** |
| 45 | + * Email message object that should be instance of \Zend_Mail |
| 46 | + * |
| 47 | + * @var MessageInterface |
| 48 | + */ |
| 49 | + private $message; |
| 50 | + |
| 51 | + /** |
| 52 | + * Core store config |
| 53 | + * |
| 54 | + * @var ScopeConfigInterface |
| 55 | + */ |
| 56 | + private $scopeConfig; |
| 57 | + |
| 58 | + /** |
| 59 | + * @param \Zend_Mail_Transport_Sendmail $transport |
| 60 | + * @param MessageInterface $message Email message object |
| 61 | + * @param ScopeConfigInterface $scopeConfig Core store config |
| 62 | + * @param string|array|\Zend_Config|null $parameters Config options for sendmail parameters |
| 63 | + * |
| 64 | + * @throws \InvalidArgumentException when $message is not an instance of \Zend_Mail |
| 65 | + */ |
| 66 | + public function __construct( |
| 67 | + \Zend_Mail_Transport_Sendmail $transport, |
| 68 | + MessageInterface $message, |
| 69 | + ScopeConfigInterface $scopeConfig |
| 70 | + ) { |
| 71 | + if (!$message instanceof \Zend_Mail) { |
| 72 | + throw new \InvalidArgumentException('The message should be an instance of \Zend_Mail'); |
| 73 | + } |
| 74 | + $this->transport = $transport; |
| 75 | + $this->message = $message; |
| 76 | + $this->scopeConfig = $scopeConfig; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Sets Return-Path to email if necessary, and sends email if it is allowed by System Configurations |
| 81 | + * |
| 82 | + * @return void |
| 83 | + * @throws MailException |
| 84 | + */ |
| 85 | + public function sendMessage() |
| 86 | + { |
| 87 | + try { |
| 88 | + if (!$this->scopeConfig->isSetFlag(self::XML_PATH_SYSTEM_SMTP_DISABLE, ScopeInterface::SCOPE_STORE)) { |
| 89 | + /* configuration of whether return path should be set or no. Possible values are: |
| 90 | + * 0 - no |
| 91 | + * 1 - yes (set value as FROM address) |
| 92 | + * 2 - use custom value |
| 93 | + * @see Magento\Config\Model\Config\Source\Yesnocustom |
| 94 | + */ |
| 95 | + $isSetReturnPath = $this->scopeConfig->getValue( |
| 96 | + self::XML_PATH_SENDING_SET_RETURN_PATH, |
| 97 | + ScopeInterface::SCOPE_STORE |
| 98 | + ); |
| 99 | + $returnPathValue = $this->scopeConfig->getValue( |
| 100 | + self::XML_PATH_SENDING_RETURN_PATH_EMAIL, |
| 101 | + ScopeInterface::SCOPE_STORE |
| 102 | + ); |
| 103 | + |
| 104 | + if ($isSetReturnPath == '1') { |
| 105 | + $this->message->setReturnPath($this->message->getFrom()); |
| 106 | + } elseif ($isSetReturnPath == '2' && $returnPathValue !== null) { |
| 107 | + $this->message->setReturnPath($returnPathValue); |
| 108 | + } |
| 109 | + $this->transport->send($this->message); |
| 110 | + } |
| 111 | + } catch (\Exception $e) { |
| 112 | + throw new MailException(__($e->getMessage()), $e); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @inheritdoc |
| 118 | + */ |
| 119 | + public function getMessage() |
| 120 | + { |
| 121 | + return $this->message; |
| 122 | + } |
| 123 | +} |
0 commit comments