AbstractSmtpTest.php 34KB

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