PageRenderTime 77ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/swiftmailer/swiftmailer/tests/unit/Swift/Transport/AbstractSmtpTest.php

https://github.com/antonPostWeb/pzone1704
PHP | 924 lines | 668 code | 75 blank | 181 comment | 1 complexity | f2719b3edb2a8efb995f3fe900b0c35d MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, CC-BY-3.0, MIT, BSD-3-Clause
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Transport/EsmtpHandler.php';
  4. require_once 'Swift/Mime/Message.php';
  5. require_once 'Swift/Transport/IoBuffer.php';
  6. abstract class Swift_Transport_AbstractSmtpTest
  7. extends Swift_Tests_SwiftUnitTestCase
  8. {
  9. /** Abstract test method */
  10. abstract protected function _getTransport($buf);
  11. public function testStartAccepts220ServiceGreeting()
  12. {
  13. /* -- RFC 2821, 4.2.
  14. Greeting = "220 " Domain [ SP text ] CRLF
  15. -- RFC 2822, 4.3.2.
  16. CONNECTION ESTABLISHMENT
  17. S: 220
  18. E: 554
  19. */
  20. $buf = $this->_getBuffer();
  21. $smtp = $this->_getTransport($buf);
  22. $s = $this->_sequence('SMTP-convo');
  23. $this->_checking(Expectations::create()
  24. -> one($buf)->initialize() -> inSequence($s)
  25. -> one($buf)->readLine(0) -> inSequence($s) -> returns("220 some.server.tld bleh\r\n")
  26. );
  27. $this->_finishBuffer($buf);
  28. try {
  29. $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
  30. $smtp->start();
  31. $this->assertTrue($smtp->isStarted(), '%s: start() should have started connection');
  32. } catch (Exception $e) {
  33. $this->fail('220 is a valid SMTP greeting and should be accepted');
  34. }
  35. }
  36. public function testBadGreetingCausesException()
  37. {
  38. $buf = $this->_getBuffer();
  39. $smtp = $this->_getTransport($buf);
  40. $s = $this->_sequence('SMTP-convo');
  41. $this->_checking(Expectations::create()
  42. -> one($buf)->initialize() -> inSequence($s)
  43. -> one($buf)->readLine(0) -> inSequence($s) -> returns("554 I'm busy\r\n")
  44. );
  45. $this->_finishBuffer($buf);
  46. try {
  47. $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
  48. $smtp->start();
  49. $this->fail('554 greeting indicates an error and should cause an exception');
  50. } catch (Exception $e) {
  51. $this->assertFalse($smtp->isStarted(), '%s: start() should have failed');
  52. }
  53. }
  54. public function testStartSendsHeloToInitiate()
  55. {
  56. /* -- RFC 2821, 3.2.
  57. 3.2 Client Initiation
  58. Once the server has sent the welcoming message and the client has
  59. received it, the client normally sends the EHLO command to the
  60. server, indicating the client's identity. In addition to opening the
  61. session, use of EHLO indicates that the client is able to process
  62. service extensions and requests that the server provide a list of the
  63. extensions it supports. Older SMTP systems which are unable to
  64. support service extensions and contemporary clients which do not
  65. require service extensions in the mail session being initiated, MAY
  66. use HELO instead of EHLO. Servers MUST NOT return the extended
  67. EHLO-style response to a HELO command. For a particular connection
  68. attempt, if the server returns a "command not recognized" response to
  69. EHLO, the client SHOULD be able to fall back and send HELO.
  70. In the EHLO command the host sending the command identifies itself;
  71. the command may be interpreted as saying "Hello, I am <domain>" (and,
  72. in the case of EHLO, "and I support service extension requests").
  73. -- RFC 2281, 4.1.1.1.
  74. ehlo = "EHLO" SP Domain CRLF
  75. helo = "HELO" SP Domain CRLF
  76. -- RFC 2821, 4.3.2.
  77. EHLO or HELO
  78. S: 250
  79. E: 504, 550
  80. */
  81. $buf = $this->_getBuffer();
  82. $smtp = $this->_getTransport($buf);
  83. $s = $this->_sequence('SMTP-convo');
  84. $this->_checking(Expectations::create()
  85. -> one($buf)->initialize() -> inSequence($s)
  86. -> one($buf)->readLine(0) -> inSequence($s) -> returns("220 some.server.tld bleh\r\n")
  87. -> one($buf)->write(pattern('~^HELO .*?\r\n$~D')) -> inSequence($s) -> returns(1)
  88. -> one($buf)->readLine(1) -> inSequence($s) -> returns('250 ServerName' . "\r\n")
  89. );
  90. $this->_finishBuffer($buf);
  91. try {
  92. $smtp->start();
  93. } catch (Exception $e) {
  94. $this->fail('Starting SMTP should send HELO and accept 250 response');
  95. }
  96. }
  97. public function testInvalidHeloResponseCausesException()
  98. {
  99. $buf = $this->_getBuffer();
  100. $smtp = $this->_getTransport($buf);
  101. $s = $this->_sequence('SMTP-convo');
  102. $this->_checking(Expectations::create()
  103. -> one($buf)->initialize() -> inSequence($s)
  104. -> one($buf)->readLine(0) -> inSequence($s) -> returns("220 some.server.tld bleh\r\n")
  105. -> one($buf)->write(pattern('~^HELO .*?\r\n$~D')) -> inSequence($s) -> returns(1)
  106. -> one($buf)->readLine(1) -> inSequence($s) -> returns('504 WTF' . "\r\n")
  107. );
  108. $this->_finishBuffer($buf);
  109. try {
  110. $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
  111. $smtp->start();
  112. $this->fail('Non 250 HELO response should raise Exception');
  113. } catch (Exception $e) {
  114. $this->assertFalse($smtp->isStarted(), '%s: SMTP start() should have failed');
  115. }
  116. }
  117. public function testDomainNameIsPlacedInHelo()
  118. {
  119. /* -- RFC 2821, 4.1.4.
  120. The SMTP client MUST, if possible, ensure that the domain parameter
  121. to the EHLO command is a valid principal host name (not a CNAME or MX
  122. name) for its host. If this is not possible (e.g., when the client's
  123. address is dynamically assigned and the client does not have an
  124. obvious name), an address literal SHOULD be substituted for the
  125. domain name and supplemental information provided that will assist in
  126. identifying the client.
  127. */
  128. $buf = $this->_getBuffer();
  129. $smtp = $this->_getTransport($buf);
  130. $s = $this->_sequence('SMTP-convo');
  131. $this->_checking(Expectations::create()
  132. -> one($buf)->initialize() -> inSequence($s)
  133. -> one($buf)->readLine(0) -> inSequence($s) -> returns("220 some.server.tld bleh\r\n")
  134. -> one($buf)->write("HELO mydomain.com\r\n") -> inSequence($s) -> returns(1)
  135. -> one($buf)->readLine(1) -> inSequence($s) -> returns('250 ServerName' . "\r\n")
  136. );
  137. $this->_finishBuffer($buf);
  138. $smtp->setLocalDomain('mydomain.com');
  139. $smtp->start();
  140. }
  141. public function testSuccessfulMailCommand()
  142. {
  143. /* -- RFC 2821, 3.3.
  144. There are three steps to SMTP mail transactions. The transaction
  145. starts with a MAIL command which gives the sender identification.
  146. .....
  147. The first step in the procedure is the MAIL command.
  148. MAIL FROM:<reverse-path> [SP <mail-parameters> ] <CRLF>
  149. -- RFC 2821, 4.1.1.2.
  150. Syntax:
  151. "MAIL FROM:" ("<>" / Reverse-Path)
  152. [SP Mail-parameters] CRLF
  153. -- RFC 2821, 4.1.2.
  154. Reverse-path = Path
  155. Forward-path = Path
  156. Path = "<" [ A-d-l ":" ] Mailbox ">"
  157. A-d-l = At-domain *( "," A-d-l )
  158. ; Note that this form, the so-called "source route",
  159. ; MUST BE accepted, SHOULD NOT be generated, and SHOULD be
  160. ; ignored.
  161. At-domain = "@" domain
  162. -- RFC 2821, 4.3.2.
  163. MAIL
  164. S: 250
  165. E: 552, 451, 452, 550, 553, 503
  166. */
  167. $buf = $this->_getBuffer();
  168. $smtp = $this->_getTransport($buf);
  169. $message = $this->_createMessage();
  170. $this->_checking(Expectations::create()
  171. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  172. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  173. -> allowing($message)
  174. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(1)
  175. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  176. );
  177. $this->_finishBuffer($buf);
  178. try {
  179. $smtp->start();
  180. $smtp->send($message);
  181. } catch (Exception $e) {
  182. $this->fail('MAIL FROM should accept a 250 response');
  183. }
  184. }
  185. public function testInvalidResponseCodeFromMailCausesException()
  186. {
  187. $buf = $this->_getBuffer();
  188. $smtp = $this->_getTransport($buf);
  189. $message = $this->_createMessage();
  190. $this->_checking(Expectations::create()
  191. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  192. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  193. -> allowing($message)
  194. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(1)
  195. -> one($buf)->readLine(1) -> returns('553 Bad' . "\r\n")
  196. );
  197. $this->_finishBuffer($buf);
  198. try {
  199. $smtp->start();
  200. $smtp->send($message);
  201. $this->fail('MAIL FROM should accept a 250 response');
  202. } catch (Exception $e) {
  203. }
  204. }
  205. public function testSenderIsPreferredOverFrom()
  206. {
  207. $buf = $this->_getBuffer();
  208. $smtp = $this->_getTransport($buf);
  209. $message = $this->_createMessage();
  210. $this->_checking(Expectations::create()
  211. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  212. -> allowing($message)->getSender() -> returns(array('another@domain.com'=>'Someone'))
  213. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  214. -> allowing($message)
  215. -> one($buf)->write("MAIL FROM: <another@domain.com>\r\n") -> returns(1)
  216. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  217. );
  218. $this->_finishBuffer($buf);
  219. $smtp->start();
  220. $smtp->send($message);
  221. }
  222. public function testReturnPathIsPreferredOverSender()
  223. {
  224. $buf = $this->_getBuffer();
  225. $smtp = $this->_getTransport($buf);
  226. $message = $this->_createMessage();
  227. $this->_checking(Expectations::create()
  228. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  229. -> allowing($message)->getSender() -> returns(array('another@domain.com'=>'Someone'))
  230. -> allowing($message)->getReturnPath() -> returns('more@domain.com')
  231. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  232. -> allowing($message)
  233. -> one($buf)->write("MAIL FROM: <more@domain.com>\r\n") -> returns(1)
  234. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  235. );
  236. $this->_finishBuffer($buf);
  237. $smtp->start();
  238. $smtp->send($message);
  239. }
  240. public function testSuccessfulRcptCommandWith250Response()
  241. {
  242. /* -- RFC 2821, 3.3.
  243. The second step in the procedure is the RCPT command.
  244. RCPT TO:<forward-path> [ SP <rcpt-parameters> ] <CRLF>
  245. The first or only argument to this command includes a forward-path
  246. (normally a mailbox and domain, always surrounded by "<" and ">"
  247. brackets) identifying one recipient. If accepted, the SMTP server
  248. returns a 250 OK reply and stores the forward-path. If the recipient
  249. is known not to be a deliverable address, the SMTP server returns a
  250. 550 reply, typically with a string such as "no such user - " and the
  251. mailbox name (other circumstances and reply codes are possible).
  252. This step of the procedure can be repeated any number of times.
  253. -- RFC 2821, 4.1.1.3.
  254. This command is used to identify an individual recipient of the mail
  255. data; multiple recipients are specified by multiple use of this
  256. command. The argument field contains a forward-path and may contain
  257. optional parameters.
  258. The forward-path normally consists of the required destination
  259. mailbox. Sending systems SHOULD not generate the optional list of
  260. hosts known as a source route.
  261. .......
  262. "RCPT TO:" ("<Postmaster@" domain ">" / "<Postmaster>" / Forward-Path)
  263. [SP Rcpt-parameters] CRLF
  264. -- RFC 2821, 4.2.2.
  265. 250 Requested mail action okay, completed
  266. 251 User not local; will forward to <forward-path>
  267. (See section 3.4)
  268. 252 Cannot VRFY user, but will accept message and attempt
  269. delivery
  270. -- RFC 2821, 4.3.2.
  271. RCPT
  272. S: 250, 251 (but see section 3.4 for discussion of 251 and 551)
  273. E: 550, 551, 552, 553, 450, 451, 452, 503, 550
  274. */
  275. //We'll treat 252 as accepted since it isn't really a failure
  276. $buf = $this->_getBuffer();
  277. $smtp = $this->_getTransport($buf);
  278. $message = $this->_createMessage();
  279. $s = $this->_sequence('SMTP-envelope');
  280. $this->_checking(Expectations::create()
  281. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  282. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  283. -> allowing($message)
  284. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> inSequence($s) -> returns(1)
  285. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  286. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> inSequence($s) -> returns(2)
  287. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  288. );
  289. $this->_finishBuffer($buf);
  290. try {
  291. $smtp->start();
  292. $smtp->send($message);
  293. } catch (Exception $e) {
  294. $this->fail('RCPT TO should accept a 250 response');
  295. }
  296. }
  297. public function testMailFromCommandIsOnlySentOncePerMessage()
  298. {
  299. $buf = $this->_getBuffer();
  300. $smtp = $this->_getTransport($buf);
  301. $message = $this->_createMessage();
  302. $s = $this->_sequence('SMTP-envelope');
  303. $this->_checking(Expectations::create()
  304. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  305. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  306. -> allowing($message)
  307. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> inSequence($s) -> returns(1)
  308. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  309. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> inSequence($s) -> returns(2)
  310. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  311. -> never($buf)->write("MAIL FROM: <me@domain.com>\r\n")
  312. );
  313. $this->_finishBuffer($buf);
  314. $smtp->start();
  315. $smtp->send($message);
  316. }
  317. public function testMultipleRecipientsSendsMultipleRcpt()
  318. {
  319. $buf = $this->_getBuffer();
  320. $smtp = $this->_getTransport($buf);
  321. $message = $this->_createMessage();
  322. $this->_checking(Expectations::create()
  323. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  324. -> allowing($message)->getTo() -> returns(array(
  325. 'foo@bar' => null,
  326. 'zip@button' => 'Zip Button',
  327. 'test@domain' => 'Test user'
  328. ))
  329. -> allowing($message)
  330. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(1)
  331. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  332. -> one($buf)->write("RCPT TO: <zip@button>\r\n") -> returns(2)
  333. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  334. -> one($buf)->write("RCPT TO: <test@domain>\r\n") -> returns(3)
  335. -> one($buf)->readLine(3) -> returns('250 OK' . "\r\n")
  336. );
  337. $this->_finishBuffer($buf);
  338. $smtp->start();
  339. $smtp->send($message);
  340. }
  341. public function testCcRecipientsSendsMultipleRcpt()
  342. {
  343. $buf = $this->_getBuffer();
  344. $smtp = $this->_getTransport($buf);
  345. $message = $this->_createMessage();
  346. $this->_checking(Expectations::create()
  347. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  348. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  349. -> allowing($message)->getCc() -> returns(array(
  350. 'zip@button' => 'Zip Button',
  351. 'test@domain' => 'Test user'
  352. ))
  353. -> allowing($message)
  354. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(1)
  355. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  356. -> one($buf)->write("RCPT TO: <zip@button>\r\n") -> returns(2)
  357. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  358. -> one($buf)->write("RCPT TO: <test@domain>\r\n") -> returns(3)
  359. -> one($buf)->readLine(3) -> returns('250 OK' . "\r\n")
  360. );
  361. $this->_finishBuffer($buf);
  362. $smtp->start();
  363. $smtp->send($message);
  364. }
  365. public function testSendReturnsNumberOfSuccessfulRecipients()
  366. {
  367. $buf = $this->_getBuffer();
  368. $smtp = $this->_getTransport($buf);
  369. $message = $this->_createMessage();
  370. $this->_checking(Expectations::create()
  371. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  372. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  373. -> allowing($message)->getCc() -> returns(array(
  374. 'zip@button' => 'Zip Button',
  375. 'test@domain' => 'Test user'
  376. ))
  377. -> allowing($message)
  378. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(1)
  379. -> one($buf)->readLine(1) -> returns('250 OK' . "\r\n")
  380. -> one($buf)->write("RCPT TO: <zip@button>\r\n") -> returns(2)
  381. -> one($buf)->readLine(2) -> returns('501 Nobody here' . "\r\n")
  382. -> one($buf)->write("RCPT TO: <test@domain>\r\n") -> returns(3)
  383. -> one($buf)->readLine(3) -> returns('250 OK' . "\r\n")
  384. );
  385. $this->_finishBuffer($buf);
  386. $smtp->start();
  387. $this->assertEqual(2, $smtp->send($message),
  388. '%s: 1 of 3 recipients failed so 2 should be returned'
  389. );
  390. }
  391. public function testRsetIsSentIfNoSuccessfulRecipients()
  392. {
  393. /* --RFC 2821, 4.1.1.5.
  394. This command specifies that the current mail transaction will be
  395. aborted. Any stored sender, recipients, and mail data MUST be
  396. discarded, and all buffers and state tables cleared. The receiver
  397. MUST send a "250 OK" reply to a RSET command with no arguments. A
  398. reset command may be issued by the client at any time.
  399. -- RFC 2821, 4.3.2.
  400. RSET
  401. S: 250
  402. */
  403. $buf = $this->_getBuffer();
  404. $smtp = $this->_getTransport($buf);
  405. $message = $this->_createMessage();
  406. $this->_checking(Expectations::create()
  407. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  408. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  409. -> allowing($message)
  410. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(1)
  411. -> one($buf)->readLine(1) -> returns('503 Bad' . "\r\n")
  412. -> one($buf)->write("RSET\r\n") -> returns(2)
  413. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  414. );
  415. $this->_finishBuffer($buf);
  416. $smtp->start();
  417. $this->assertEqual(0, $smtp->send($message),
  418. '%s: 1 of 1 recipients failed so 0 should be returned'
  419. );
  420. }
  421. public function testSuccessfulDataCommand()
  422. {
  423. /* -- RFC 2821, 3.3.
  424. The third step in the procedure is the DATA command (or some
  425. alternative specified in a service extension).
  426. DATA <CRLF>
  427. If accepted, the SMTP server returns a 354 Intermediate reply and
  428. considers all succeeding lines up to but not including the end of
  429. mail data indicator to be the message text.
  430. -- RFC 2821, 4.1.1.4.
  431. The receiver normally sends a 354 response to DATA, and then treats
  432. the lines (strings ending in <CRLF> sequences, as described in
  433. section 2.3.7) following the command as mail data from the sender.
  434. This command causes the mail data to be appended to the mail data
  435. buffer. The mail data may contain any of the 128 ASCII character
  436. codes, although experience has indicated that use of control
  437. characters other than SP, HT, CR, and LF may cause problems and
  438. SHOULD be avoided when possible.
  439. -- RFC 2821, 4.3.2.
  440. DATA
  441. I: 354 -> data -> S: 250
  442. E: 552, 554, 451, 452
  443. E: 451, 554, 503
  444. */
  445. $buf = $this->_getBuffer();
  446. $smtp = $this->_getTransport($buf);
  447. $message = $this->_createMessage();
  448. $this->_checking(Expectations::create()
  449. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  450. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  451. -> allowing($message)
  452. -> one($buf)->write("DATA\r\n") -> returns(1)
  453. -> one($buf)->readLine(1) -> returns('354 Go ahead' . "\r\n")
  454. );
  455. $this->_finishBuffer($buf);
  456. try {
  457. $smtp->start();
  458. $smtp->send($message);
  459. } catch (Exception $e) {
  460. $this->fail('354 is the expected response to DATA');
  461. }
  462. }
  463. public function testBadDataResponseCausesException()
  464. {
  465. $buf = $this->_getBuffer();
  466. $smtp = $this->_getTransport($buf);
  467. $message = $this->_createMessage();
  468. $this->_checking(Expectations::create()
  469. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  470. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  471. -> allowing($message)
  472. -> one($buf)->write("DATA\r\n") -> returns(1)
  473. -> one($buf)->readLine(1) -> returns('451 Bad' . "\r\n")
  474. );
  475. $this->_finishBuffer($buf);
  476. try {
  477. $smtp->start();
  478. $smtp->send($message);
  479. $this->fail('354 is the expected response to DATA (not observed)');
  480. } catch (Exception $e) {
  481. }
  482. }
  483. public function testMessageIsStreamedToBufferForData()
  484. {
  485. $buf = $this->_getBuffer();
  486. $smtp = $this->_getTransport($buf);
  487. $message = $this->_createMessage();
  488. $s = $this->_sequence('DATA Streaming');
  489. $this->_checking(Expectations::create()
  490. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  491. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  492. -> one($buf)->write("DATA\r\n") -> inSequence($s) -> returns(1)
  493. -> one($buf)->readLine(1) -> returns('354 OK' . "\r\n")
  494. -> one($message)->toByteStream($buf) -> inSequence($s)
  495. -> one($buf)->write("\r\n.\r\n") -> inSequence($s) -> returns(2)
  496. -> one($buf)->readLine(2) -> returns('250 OK' . "\r\n")
  497. -> allowing($message)
  498. );
  499. $this->_finishBuffer($buf);
  500. $smtp->start();
  501. $smtp->send($message);
  502. }
  503. public function testBadResponseAfterDataTransmissionCausesException()
  504. {
  505. $buf = $this->_getBuffer();
  506. $smtp = $this->_getTransport($buf);
  507. $message = $this->_createMessage();
  508. $s = $this->_sequence('DATA Streaming');
  509. $this->_checking(Expectations::create()
  510. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  511. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  512. -> one($buf)->write("DATA\r\n") -> inSequence($s) -> returns(1)
  513. -> one($buf)->readLine(1) -> returns('354 OK' . "\r\n")
  514. -> one($message)->toByteStream($buf) -> inSequence($s)
  515. -> one($buf)->write("\r\n.\r\n") -> inSequence($s) -> returns(2)
  516. -> one($buf)->readLine(2) -> returns('554 Error' . "\r\n")
  517. -> allowing($message)
  518. );
  519. $this->_finishBuffer($buf);
  520. try {
  521. $smtp->start();
  522. $smtp->send($message);
  523. $this->fail('250 is the expected response after a DATA transmission (not observed)');
  524. } catch (Exception $e) {
  525. }
  526. }
  527. public function testBccRecipientsAreRemovedFromHeaders()
  528. {
  529. /* -- RFC 2821, 7.2.
  530. Addresses that do not appear in the message headers may appear in the
  531. RCPT commands to an SMTP server for a number of reasons. The two
  532. most common involve the use of a mailing address as a "list exploder"
  533. (a single address that resolves into multiple addresses) and the
  534. appearance of "blind copies". Especially when more than one RCPT
  535. command is present, and in order to avoid defeating some of the
  536. purpose of these mechanisms, SMTP clients and servers SHOULD NOT copy
  537. the full set of RCPT command arguments into the headers, either as
  538. part of trace headers or as informational or private-extension
  539. headers. Since this rule is often violated in practice, and cannot
  540. be enforced, sending SMTP systems that are aware of "bcc" use MAY
  541. find it helpful to send each blind copy as a separate message
  542. transaction containing only a single RCPT command.
  543. */
  544. $buf = $this->_getBuffer();
  545. $smtp = $this->_getTransport($buf);
  546. $message = $this->_createMessage();
  547. $this->_checking(Expectations::create()
  548. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  549. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  550. -> allowing($message)->getBcc() -> returns(array(
  551. 'zip@button' => 'Zip Button',
  552. 'test@domain' => 'Test user'
  553. ))
  554. -> atLeast(1)->of($message)->setBcc(array())
  555. -> allowing($message)
  556. );
  557. $this->_finishBuffer($buf);
  558. $smtp->start();
  559. $smtp->send($message);
  560. }
  561. public function testEachBccRecipientIsSentASeparateMessage()
  562. {
  563. $buf = $this->_getBuffer();
  564. $smtp = $this->_getTransport($buf);
  565. $message = $this->_createMessage();
  566. $this->_checking(Expectations::create()
  567. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  568. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  569. -> allowing($message)->getBcc() -> returns(array(
  570. 'zip@button' => 'Zip Button',
  571. 'test@domain' => 'Test user'
  572. ))
  573. -> atLeast(1)->of($message)->setBcc(array())
  574. -> one($message)->setBcc(array('zip@button' => 'Zip Button'))
  575. -> one($message)->setBcc(array('test@domain' => 'Test user'))
  576. -> atLeast(1)->of($message)->setBcc(array(
  577. 'zip@button' => 'Zip Button',
  578. 'test@domain' => 'Test user'
  579. ))
  580. -> allowing($message)
  581. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(1)
  582. -> one($buf)->readLine(1) -> returns("250 OK\r\n")
  583. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(2)
  584. -> one($buf)->readLine(2) -> returns("250 OK\r\n")
  585. -> one($buf)->write("DATA\r\n") -> returns(3)
  586. -> one($buf)->readLine(3) -> returns("354 OK\r\n")
  587. -> one($buf)->write("\r\n.\r\n") -> returns(4)
  588. -> one($buf)->readLine(4) -> returns("250 OK\r\n")
  589. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(5)
  590. -> one($buf)->readLine(5) -> returns("250 OK\r\n")
  591. -> one($buf)->write("RCPT TO: <zip@button>\r\n") -> returns(6)
  592. -> one($buf)->readLine(6) -> returns("250 OK\r\n")
  593. -> one($buf)->write("DATA\r\n") -> returns(7)
  594. -> one($buf)->readLine(7) -> returns("354 OK\r\n")
  595. -> one($buf)->write("\r\n.\r\n") -> returns(8)
  596. -> one($buf)->readLine(8) -> returns("250 OK\r\n")
  597. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(9)
  598. -> one($buf)->readLine(9) -> returns("250 OK\r\n")
  599. -> one($buf)->write("RCPT TO: <test@domain>\r\n") -> returns(10)
  600. -> one($buf)->readLine(10) -> returns("250 OK\r\n")
  601. -> one($buf)->write("DATA\r\n") -> returns(11)
  602. -> one($buf)->readLine(11) -> returns("354 OK\r\n")
  603. -> one($buf)->write("\r\n.\r\n") -> returns(12)
  604. -> one($buf)->readLine(12) -> returns("250 OK\r\n")
  605. );
  606. $this->_finishBuffer($buf);
  607. $smtp->start();
  608. $this->assertEqual(3, $smtp->send($message));
  609. }
  610. public function testMessageStateIsRestoredOnFailure()
  611. {
  612. $buf = $this->_getBuffer();
  613. $smtp = $this->_getTransport($buf);
  614. $message = $this->_createMessage();
  615. $this->_checking(Expectations::create()
  616. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  617. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  618. -> allowing($message)->getBcc() -> returns(array(
  619. 'zip@button' => 'Zip Button',
  620. 'test@domain' => 'Test user'
  621. ))
  622. -> one($message)->setBcc(array())
  623. -> one($message)->setBcc(array(
  624. 'zip@button' => 'Zip Button',
  625. 'test@domain' => 'Test user'
  626. ))
  627. -> allowing($message)
  628. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(1)
  629. -> one($buf)->readLine(1) -> returns("250 OK\r\n")
  630. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(2)
  631. -> one($buf)->readLine(2) -> returns("250 OK\r\n")
  632. -> one($buf)->write("DATA\r\n") -> returns(3)
  633. -> one($buf)->readLine(3) -> returns("451 No\r\n")
  634. );
  635. $this->_finishBuffer($buf);
  636. $smtp->start();
  637. try {
  638. $smtp->send($message);
  639. $this->fail('A bad response was given so exception is expected');
  640. } catch (Exception $e) {
  641. }
  642. }
  643. public function testStopSendsQuitCommand()
  644. {
  645. /* -- RFC 2821, 4.1.1.10.
  646. This command specifies that the receiver MUST send an OK reply, and
  647. then close the transmission channel.
  648. The receiver MUST NOT intentionally close the transmission channel
  649. until it receives and replies to a QUIT command (even if there was an
  650. error). The sender MUST NOT intentionally close the transmission
  651. channel until it sends a QUIT command and SHOULD wait until it
  652. receives the reply (even if there was an error response to a previous
  653. command). If the connection is closed prematurely due to violations
  654. of the above or system or network failure, the server MUST cancel any
  655. pending transaction, but not undo any previously completed
  656. transaction, and generally MUST act as if the command or transaction
  657. in progress had received a temporary error (i.e., a 4yz response).
  658. The QUIT command may be issued at any time.
  659. Syntax:
  660. "QUIT" CRLF
  661. */
  662. $buf = $this->_getBuffer();
  663. $smtp = $this->_getTransport($buf);
  664. $message = $this->_createMessage();
  665. $this->_checking(Expectations::create()
  666. -> one($buf)->initialize()
  667. -> one($buf)->write("QUIT\r\n") -> returns(1)
  668. -> one($buf)->readLine(1) -> returns("221 Bye\r\n")
  669. -> one($buf)->terminate()
  670. );
  671. $this->_finishBuffer($buf);
  672. $this->assertFalse($smtp->isStarted());
  673. $smtp->start();
  674. $this->assertTrue($smtp->isStarted());
  675. $smtp->stop();
  676. $this->assertFalse($smtp->isStarted());
  677. }
  678. public function testBufferCanBeFetched()
  679. {
  680. $buf = $this->_getBuffer();
  681. $smtp = $this->_getTransport($buf);
  682. $ref = $smtp->getBuffer();
  683. $this->assertReference($buf, $ref);
  684. }
  685. public function testBufferCanBeWrittenToUsingExecuteCommand()
  686. {
  687. $buf = $this->_getBuffer();
  688. $smtp = $this->_getTransport($buf);
  689. $message = $this->_createMessage();
  690. $this->_checking(Expectations::create()
  691. -> one($buf)->write("FOO\r\n") -> returns(1)
  692. -> one($buf)->readLine(1) -> returns("250 OK\r\n")
  693. -> ignoring($buf)
  694. );
  695. $res = $smtp->executeCommand("FOO\r\n");
  696. $this->assertEqual("250 OK\r\n", $res);
  697. }
  698. public function testResponseCodesAreValidated()
  699. {
  700. $buf = $this->_getBuffer();
  701. $smtp = $this->_getTransport($buf);
  702. $message = $this->_createMessage();
  703. $this->_checking(Expectations::create()
  704. -> one($buf)->write("FOO\r\n") -> returns(1)
  705. -> one($buf)->readLine(1) -> returns("551 Not ok\r\n")
  706. -> ignoring($buf)
  707. );
  708. try {
  709. $smtp->executeCommand("FOO\r\n", array(250, 251));
  710. $this->fail('A 250 or 251 response was needed but 551 was returned.');
  711. } catch (Exception $e) {
  712. }
  713. }
  714. public function testFailedRecipientsCanBeCollectedByReference()
  715. {
  716. $buf = $this->_getBuffer();
  717. $smtp = $this->_getTransport($buf);
  718. $message = $this->_createMessage();
  719. $this->_checking(Expectations::create()
  720. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  721. -> allowing($message)->getTo() -> returns(array('foo@bar' => null))
  722. -> allowing($message)->getBcc() -> returns(array(
  723. 'zip@button' => 'Zip Button',
  724. 'test@domain' => 'Test user'
  725. ))
  726. -> atLeast(1)->of($message)->setBcc(array())
  727. -> one($message)->setBcc(array('zip@button' => 'Zip Button'))
  728. -> one($message)->setBcc(array('test@domain' => 'Test user'))
  729. -> atLeast(1)->of($message)->setBcc(array(
  730. 'zip@button' => 'Zip Button',
  731. 'test@domain' => 'Test user'
  732. ))
  733. -> allowing($message)
  734. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(1)
  735. -> one($buf)->readLine(1) -> returns("250 OK\r\n")
  736. -> one($buf)->write("RCPT TO: <foo@bar>\r\n") -> returns(2)
  737. -> one($buf)->readLine(2) -> returns("250 OK\r\n")
  738. -> one($buf)->write("DATA\r\n") -> returns(3)
  739. -> one($buf)->readLine(3) -> returns("354 OK\r\n")
  740. -> one($buf)->write("\r\n.\r\n") -> returns(4)
  741. -> one($buf)->readLine(4) -> returns("250 OK\r\n")
  742. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(5)
  743. -> one($buf)->readLine(5) -> returns("250 OK\r\n")
  744. -> one($buf)->write("RCPT TO: <zip@button>\r\n") -> returns(6)
  745. -> one($buf)->readLine(6) -> returns("500 Bad\r\n")
  746. -> one($buf)->write("RSET\r\n") -> returns(7)
  747. -> one($buf)->readLine(7) -> returns("250 OK\r\n")
  748. -> one($buf)->write("MAIL FROM: <me@domain.com>\r\n") -> returns(8)
  749. -> one($buf)->readLine(8) -> returns("250 OK\r\n")
  750. -> one($buf)->write("RCPT TO: <test@domain>\r\n") -> returns(9)
  751. -> one($buf)->readLine(9) -> returns("500 Bad\r\n")
  752. -> one($buf)->write("RSET\r\n") -> returns(10)
  753. -> one($buf)->readLine(10) -> returns("250 OK\r\n")
  754. );
  755. $this->_finishBuffer($buf);
  756. $smtp->start();
  757. $this->assertEqual(1, $smtp->send($message, $failures));
  758. $this->assertEqual(array('zip@button', 'test@domain'), $failures,
  759. '%s: Failures should be caught in an array'
  760. );
  761. }
  762. public function testSendingRegeneratesMessageId()
  763. {
  764. $buf = $this->_getBuffer();
  765. $smtp = $this->_getTransport($buf);
  766. $message = $this->_createMessage();
  767. $this->_checking(Expectations::create()
  768. -> allowing($message)->getFrom() -> returns(array('me@domain.com'=>'Me'))
  769. -> allowing($message)->getTo() -> returns(array('foo@bar'=>null))
  770. -> one($message)->generateId()
  771. -> allowing($message)
  772. );
  773. $this->_finishBuffer($buf);
  774. $smtp->start();
  775. $smtp->send($message);
  776. }
  777. // -- Protected methods
  778. protected function _getBuffer()
  779. {
  780. return $this->_mock('Swift_Transport_IoBuffer');
  781. }
  782. protected function _createMessage()
  783. {
  784. return $this->_mock('Swift_Mime_Message');
  785. }
  786. protected function _finishBuffer($buf)
  787. {
  788. $this->_checking(Expectations::create()
  789. -> ignoring($buf)->readLine(0) -> returns('220 server.com foo' . "\r\n")
  790. -> ignoring($buf)->write(pattern('~^(EH|HE)LO .*?\r\n$~D')) -> returns($x = uniqid())
  791. -> ignoring($buf)->readLine($x) -> returns('250 ServerName' . "\r\n")
  792. -> ignoring($buf)->write(pattern('~^MAIL FROM: <.*?>\r\n$~D')) -> returns($x = uniqid())
  793. -> ignoring($buf)->readLine($x) -> returns('250 OK' . "\r\n")
  794. -> ignoring($buf)->write(pattern('~^RCPT TO: <.*?>\r\n$~D')) -> returns($x = uniqid())
  795. -> ignoring($buf)->readLine($x) -> returns('250 OK' . "\r\n")
  796. -> ignoring($buf)->write("DATA\r\n") -> returns($x = uniqid())
  797. -> ignoring($buf)->readLine($x) -> returns('354 OK' . "\r\n")
  798. -> ignoring($buf)->write("\r\n.\r\n") -> returns($x = uniqid())
  799. -> ignoring($buf)->readLine($x) -> returns('250 OK' . "\r\n")
  800. -> ignoring($buf)->write("RSET\r\n") -> returns($x = uniqid())
  801. -> ignoring($buf)->readLine($x) -> returns("250 OK\r\n")
  802. -> ignoring($buf) -> returns(false)
  803. );
  804. }
  805. }