/nacridan/lib/swift/Swift/Connection/SMTP.php

https://gitlab.com/nacridan/Nacridan · PHP · 254 lines · 94 code · 27 blank · 133 comment · 17 complexity · e0e772ce3d432a9396fa04f61d72eebd MD5 · raw file

  1. <?php
  2. /**
  3. * This is the SMTP handler for Swift Mailer, a PHP Mailer class.
  4. *
  5. * @package Swift
  6. * @version >= 2.0.0
  7. * @author Chris Corbyn
  8. * @date 30th July 2006
  9. * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
  10. *
  11. * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
  12. * @filesource
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to
  26. *
  27. * The Free Software Foundation, Inc.,
  28. * 51 Franklin Street,
  29. * Fifth Floor,
  30. * Boston,
  31. * MA 02110-1301 USA
  32. *
  33. * "Chris Corbyn" <chris@w3style.co.uk>
  34. *
  35. */
  36. if (! defined('SWIFT_OPEN'))
  37. define('SWIFT_OPEN', 0);
  38. if (! defined('SWIFT_SSL'))
  39. define('SWIFT_SSL', 1);
  40. if (! defined('SWIFT_TLS'))
  41. define('SWIFT_TLS', 2);
  42. if (! defined('SWIFT_DEFAULT_PORT'))
  43. define('SWIFT_DEFAULT_PORT', 25);
  44. if (! defined('SWIFT_SECURE_PORT'))
  45. define('SWIFT_SECURE_PORT', 465);
  46. if (! defined('SWIFT_AUTO_DETECT'))
  47. define('SWIFT_AUTO_DETECT', - 2);
  48. /**
  49. * SMTP Connection Class.
  50. * Connects to a remote MTA and stores the connections internally
  51. *
  52. * @package Swift
  53. */
  54. class Swift_Connection_SMTP implements Swift_IConnection
  55. {
  56. /**
  57. * Any errors we see
  58. *
  59. * @var string error
  60. */
  61. public $error;
  62. /**
  63. * Just a boolean value for when we're connected
  64. *
  65. * @var bool connected
  66. */
  67. public $connected = false;
  68. /**
  69. * SMTP Connection socket
  70. *
  71. * @var resource
  72. */
  73. public $socket;
  74. /**
  75. * SMTP Read part of I/O for Swift
  76. *
  77. * @var resource (reference)
  78. */
  79. public $readHook;
  80. /**
  81. * SMTP Write part of I/O for Swift
  82. *
  83. * @var resource (reference)
  84. */
  85. public $writeHook;
  86. /**
  87. * Number of seconds to try to connect for (non-settable)
  88. *
  89. * @var int timeout
  90. */
  91. public $connectTimeout = 30;
  92. /**
  93. * SMTP server
  94. *
  95. * @var resource
  96. */
  97. public $server;
  98. /**
  99. * SMTP Port (default 25)
  100. *
  101. * @var int number
  102. */
  103. public $port = SWIFT_DEFAULT_PORT;
  104. /**
  105. * Use SSL Encryption
  106. *
  107. * @var bool
  108. */
  109. public $ssl;
  110. /**
  111. * Constructor
  112. *
  113. * @param
  114. * string SMTP server
  115. * @param
  116. * int SMTP Port, optional
  117. * @param
  118. * bool SSL
  119. */
  120. public function __construct($server, $port = false, $transport = SWIFT_OPEN)
  121. {
  122. if ($server == SWIFT_AUTO_DETECT)
  123. $server = @ini_get('SMTP');
  124. if ($port == SWIFT_AUTO_DETECT)
  125. $port = @ini_get('smtp_port');
  126. $this->server = $server;
  127. if ($port)
  128. $this->port = $port;
  129. $this->ssl = $transport;
  130. if ($transport && ! $port) {
  131. $this->port = SWIFT_SECURE_PORT;
  132. }
  133. }
  134. /**
  135. * Establishes a connection with the MTA
  136. * The SwiftInstance Object calls this
  137. *
  138. * @return bool
  139. */
  140. public function start()
  141. {
  142. return $this->connect();
  143. }
  144. /**
  145. * Establishes a connection with the MTA
  146. *
  147. * @return bool @private
  148. */
  149. protected function connect()
  150. {
  151. $server = $this->server;
  152. switch ($this->ssl) {
  153. case SWIFT_SSL:
  154. $protocol = 'ssl://';
  155. break;
  156. case SWIFT_TLS:
  157. $protocol = 'tls://';
  158. break;
  159. case SWIFT_OPEN:
  160. default:
  161. $protocol = '';
  162. }
  163. $server = $protocol . $server;
  164. $this->socket = @fsockopen($server, $this->port, $errno, $errstr, $this->connectTimeout);
  165. $this->readHook = & $this->socket;
  166. $this->writeHook = & $this->socket;
  167. if (! $this->socket) {
  168. $this->error = $errstr;
  169. return $this->connected = false;
  170. } else
  171. return $this->connected = true;
  172. }
  173. /**
  174. * Closes the connection with the MTA
  175. * Called by the SwiftInstance object
  176. *
  177. * @return void
  178. */
  179. public function stop()
  180. {
  181. $this->disconnect();
  182. }
  183. /**
  184. * Closes the connection with the MTA
  185. *
  186. * @return void
  187. */
  188. protected function disconnect()
  189. {
  190. if ($this->connected && $this->socket) {
  191. fclose($this->socket);
  192. $this->readHook = false;
  193. $this->writeHook = false;
  194. $this->socket = false;
  195. $this->connected = false;
  196. }
  197. }
  198. /**
  199. * Returns TRUE if the socket is connected
  200. *
  201. * @return bool connected
  202. */
  203. public function isConnected()
  204. {
  205. return $this->connected;
  206. }
  207. /**
  208. * Change the default timeout from 30 seconds
  209. *
  210. * @param
  211. * int timeout secs
  212. * @return void
  213. */
  214. public function setConnectTimeout($seconds)
  215. {
  216. $this->connectTimeout = (int) $seconds;
  217. }
  218. /**
  219. * Destructor.
  220. * Closes any open connections
  221. */
  222. public function __destruct()
  223. {
  224. $this->disconnect();
  225. }
  226. }
  227. ?>