ConnectionBase.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Swift Mailer Connection Base Class
  4. * All connection handlers extend this abstract class
  5. * Please read the LICENSE file
  6. * @author Chris Corbyn <chris@w3style.co.uk>
  7. * @package Swift_Connection
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/ClassLoader.php";
  11. Swift_ClassLoader::load("Swift_LogContainer");
  12. Swift_ClassLoader::load("Swift_Connection");
  13. Swift_ClassLoader::load("Swift_ConnectionException");
  14. /**
  15. * Swift Connection Base Class
  16. * @package Swift_Connection
  17. * @author Chris Corbyn <chris@w3style.co.uk>
  18. */
  19. abstract class Swift_ConnectionBase implements Swift_Connection
  20. {
  21. /**
  22. * Any extensions the server might support
  23. * @var array
  24. */
  25. protected $extensions = array();
  26. /**
  27. * True if the connection is ESMTP.
  28. * @var boolean
  29. */
  30. protected $isESMTP = false;
  31. /**
  32. * Set an extension which the connection reports to support
  33. * @param string Extension name
  34. * @param array Attributes of the extension
  35. */
  36. public function setExtension($name, $options=array())
  37. {
  38. $this->extensions[$name] = $options;
  39. $log = Swift_LogContainer::getLog();
  40. if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
  41. {
  42. $log->add("SMTP extension '" . $name . "' reported with attributes [" . implode(", ", $options) . "].");
  43. }
  44. }
  45. /**
  46. * Check if a given extension has been set as available
  47. * @param string The name of the extension
  48. * @return boolean
  49. */
  50. public function hasExtension($name)
  51. {
  52. return array_key_exists($name, $this->extensions);
  53. }
  54. /**
  55. * Execute any needed logic after connecting and handshaking
  56. */
  57. public function postConnect(Swift $instance) {}
  58. /**
  59. * Get the list of attributes supported by the given extension
  60. * @param string The name of the connection
  61. * @return array The list of attributes
  62. * @throws Swift_ConnectionException If the extension cannot be found
  63. */
  64. public function getAttributes($extension)
  65. {
  66. if ($this->hasExtension($extension))
  67. {
  68. return $this->extensions[$extension];
  69. }
  70. else
  71. {
  72. throw new Swift_ConnectionException(
  73. "Unable to locate any attributes for the extension '" . $extension . "' since the extension cannot be found. " .
  74. "Consider using hasExtension() to check.");
  75. }
  76. }
  77. /**
  78. * Returns TRUE if the connection needs a EHLO greeting.
  79. * @return boolean
  80. */
  81. public function getRequiresEHLO()
  82. {
  83. return $this->isESMTP;
  84. }
  85. /**
  86. * Set TRUE if the connection needs a EHLO greeting.
  87. * @param boolean
  88. */
  89. public function setRequiresEHLO($set)
  90. {
  91. $this->isESMTP = (bool) $set;
  92. $log = Swift_LogContainer::getLog();
  93. if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
  94. {
  95. $log->add("Forcing ESMTP mode. HELO is EHLO.");
  96. }
  97. }
  98. }