Transport.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Email\Model;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Exception\MailException;
  9. use Magento\Framework\Mail\MessageInterface;
  10. use Magento\Framework\Mail\TransportInterface;
  11. use Magento\Framework\Phrase;
  12. use Magento\Store\Model\ScopeInterface;
  13. use Zend\Mail\Message;
  14. use Zend\Mail\Transport\Sendmail;
  15. /**
  16. * Class that responsible for filling some message data before transporting it.
  17. * @see \Zend\Mail\Transport\Sendmail is used for transport
  18. */
  19. class Transport implements TransportInterface
  20. {
  21. /**
  22. * Configuration path to source of Return-Path and whether it should be set at all
  23. * @see \Magento\Config\Model\Config\Source\Yesnocustom to possible values
  24. */
  25. const XML_PATH_SENDING_SET_RETURN_PATH = 'system/smtp/set_return_path';
  26. /**
  27. * Configuration path for custom Return-Path email
  28. */
  29. const XML_PATH_SENDING_RETURN_PATH_EMAIL = 'system/smtp/return_path_email';
  30. /**
  31. * Whether return path should be set or no.
  32. *
  33. * Possible values are:
  34. * 0 - no
  35. * 1 - yes (set value as FROM address)
  36. * 2 - use custom value
  37. *
  38. * @var int
  39. */
  40. private $isSetReturnPath;
  41. /**
  42. * @var string|null
  43. */
  44. private $returnPathValue;
  45. /**
  46. * @var Sendmail
  47. */
  48. private $zendTransport;
  49. /**
  50. * @var MessageInterface
  51. */
  52. private $message;
  53. /**
  54. * @param MessageInterface $message Email message object
  55. * @param ScopeConfigInterface $scopeConfig Core store config
  56. * @param null|string|array|\Traversable $parameters Config options for sendmail parameters
  57. */
  58. public function __construct(
  59. MessageInterface $message,
  60. ScopeConfigInterface $scopeConfig,
  61. $parameters = null
  62. ) {
  63. $this->isSetReturnPath = (int) $scopeConfig->getValue(
  64. self::XML_PATH_SENDING_SET_RETURN_PATH,
  65. ScopeInterface::SCOPE_STORE
  66. );
  67. $this->returnPathValue = $scopeConfig->getValue(
  68. self::XML_PATH_SENDING_RETURN_PATH_EMAIL,
  69. ScopeInterface::SCOPE_STORE
  70. );
  71. $this->zendTransport = new Sendmail($parameters);
  72. $this->message = $message;
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function sendMessage()
  78. {
  79. try {
  80. $zendMessage = Message::fromString($this->message->getRawMessage())->setEncoding('utf-8');
  81. if (2 === $this->isSetReturnPath && $this->returnPathValue) {
  82. $zendMessage->setSender($this->returnPathValue);
  83. } elseif (1 === $this->isSetReturnPath && $zendMessage->getFrom()->count()) {
  84. $fromAddressList = $zendMessage->getFrom();
  85. $fromAddressList->rewind();
  86. $zendMessage->setSender($fromAddressList->current()->getEmail());
  87. }
  88. $this->zendTransport->send($zendMessage);
  89. } catch (\Exception $e) {
  90. throw new MailException(new Phrase($e->getMessage()), $e);
  91. }
  92. }
  93. /**
  94. * @inheritdoc
  95. */
  96. public function getMessage()
  97. {
  98. return $this->message;
  99. }
  100. }