3 abstract class Swift_Transport_AbstractSmtpTest extends \SwiftMailerTestCase
5 abstract protected function getTransport($buf);
7 public function testStartAccepts220ServiceGreeting()
11 Greeting = "220 " Domain [ SP text ] CRLF
15 CONNECTION ESTABLISHMENT
20 $buf = $this->getBuffer();
21 $smtp = $this->getTransport($buf);
22 $buf->shouldReceive('initialize')
24 $buf->shouldReceive('readLine')
27 ->andReturn("220 some.server.tld bleh\r\n");
29 $this->finishBuffer($buf);
31 $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
33 $this->assertTrue($smtp->isStarted(), '%s: start() should have started connection');
34 } catch (Exception $e) {
35 $this->fail('220 is a valid SMTP greeting and should be accepted');
39 public function testBadGreetingCausesException()
41 $buf = $this->getBuffer();
42 $smtp = $this->getTransport($buf);
43 $buf->shouldReceive('initialize')
45 $buf->shouldReceive('readLine')
48 ->andReturn("554 I'm busy\r\n");
49 $this->finishBuffer($buf);
51 $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
53 $this->fail('554 greeting indicates an error and should cause an exception');
54 } catch (Swift_TransportException $e) {
55 $this->assertFalse($smtp->isStarted(), '%s: start() should have failed');
59 public function testStartSendsHeloToInitiate()
65 Once the server has sent the welcoming message and the client has
66 received it, the client normally sends the EHLO command to the
67 server, indicating the client's identity. In addition to opening the
68 session, use of EHLO indicates that the client is able to process
69 service extensions and requests that the server provide a list of the
70 extensions it supports. Older SMTP systems which are unable to
71 support service extensions and contemporary clients which do not
72 require service extensions in the mail session being initiated, MAY
73 use HELO instead of EHLO. Servers MUST NOT return the extended
74 EHLO-style response to a HELO command. For a particular connection
75 attempt, if the server returns a "command not recognized" response to
76 EHLO, the client SHOULD be able to fall back and send HELO.
78 In the EHLO command the host sending the command identifies itself;
79 the command may be interpreted as saying "Hello, I am <domain>" (and,
80 in the case of EHLO, "and I support service extension requests").
84 ehlo = "EHLO" SP Domain CRLF
85 helo = "HELO" SP Domain CRLF
95 $buf = $this->getBuffer();
96 $smtp = $this->getTransport($buf);
98 $buf->shouldReceive('initialize')
100 $buf->shouldReceive('readLine')
103 ->andReturn("220 some.server.tld bleh\r\n");
104 $buf->shouldReceive('write')
106 ->with('~^HELO example.org\r\n$~D')
108 $buf->shouldReceive('readLine')
111 ->andReturn('250 ServerName'."\r\n");
113 $this->finishBuffer($buf);
116 } catch (Exception $e) {
117 $this->fail('Starting SMTP should send HELO and accept 250 response');
121 public function testInvalidHeloResponseCausesException()
123 $buf = $this->getBuffer();
124 $smtp = $this->getTransport($buf);
126 $buf->shouldReceive('initialize')
128 $buf->shouldReceive('readLine')
131 ->andReturn("220 some.server.tld bleh\r\n");
132 $buf->shouldReceive('write')
134 ->with('~^HELO example.org\r\n$~D')
136 $buf->shouldReceive('readLine')
139 ->andReturn('504 WTF'."\r\n");
141 $this->finishBuffer($buf);
143 $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
145 $this->fail('Non 250 HELO response should raise Exception');
146 } catch (Swift_TransportException $e) {
147 $this->assertFalse($smtp->isStarted(), '%s: SMTP start() should have failed');
151 public function testDomainNameIsPlacedInHelo()
153 /* -- RFC 2821, 4.1.4.
155 The SMTP client MUST, if possible, ensure that the domain parameter
156 to the EHLO command is a valid principal host name (not a CNAME or MX
157 name) for its host. If this is not possible (e.g., when the client's
158 address is dynamically assigned and the client does not have an
159 obvious name), an address literal SHOULD be substituted for the
160 domain name and supplemental information provided that will assist in
161 identifying the client.
164 $buf = $this->getBuffer();
165 $smtp = $this->getTransport($buf);
167 $buf->shouldReceive('initialize')
169 $buf->shouldReceive('readLine')
172 ->andReturn("220 some.server.tld bleh\r\n");
173 $buf->shouldReceive('write')
175 ->with("HELO mydomain.com\r\n")
177 $buf->shouldReceive('readLine')
180 ->andReturn('250 ServerName'."\r\n");
182 $this->finishBuffer($buf);
183 $smtp->setLocalDomain('mydomain.com');
187 public function testSuccessfulMailCommand()
191 There are three steps to SMTP mail transactions. The transaction
192 starts with a MAIL command which gives the sender identification.
196 The first step in the procedure is the MAIL command.
198 MAIL FROM:<reverse-path> [SP <mail-parameters> ] <CRLF>
200 -- RFC 2821, 4.1.1.2.
204 "MAIL FROM:" ("<>" / Reverse-Path)
205 [SP Mail-parameters] CRLF
210 Path = "<" [ A-d-l ":" ] Mailbox ">"
211 A-d-l = At-domain *( "," A-d-l )
212 ; Note that this form, the so-called "source route",
213 ; MUST BE accepted, SHOULD NOT be generated, and SHOULD be
215 At-domain = "@" domain
221 E: 552, 451, 452, 550, 553, 503
224 $buf = $this->getBuffer();
225 $smtp = $this->getTransport($buf);
226 $message = $this->createMessage();
227 $message->shouldReceive('getFrom')
229 ->andReturn(['me@domain.com' => 'Me']);
230 $message->shouldReceive('getTo')
232 ->andReturn(['foo@bar' => null]);
233 $buf->shouldReceive('initialize')
235 $buf->shouldReceive('write')
237 ->with("MAIL FROM:<me@domain.com>\r\n")
239 $buf->shouldReceive('readLine')
242 ->andReturn("250 OK\r\n");
244 $this->finishBuffer($buf);
247 $smtp->send($message);
248 } catch (Exception $e) {
249 $this->fail('MAIL FROM should accept a 250 response');
253 public function testInvalidResponseCodeFromMailCausesException()
255 $buf = $this->getBuffer();
256 $smtp = $this->getTransport($buf);
257 $message = $this->createMessage();
259 $message->shouldReceive('getFrom')
261 ->andReturn(['me@domain.com' => 'Me']);
262 $message->shouldReceive('getTo')
264 ->andReturn(['foo@bar' => null]);
265 $buf->shouldReceive('write')
267 ->with("MAIL FROM:<me@domain.com>\r\n")
269 $buf->shouldReceive('readLine')
272 ->andReturn('553 Bad'."\r\n");
274 $this->finishBuffer($buf);
277 $smtp->send($message);
278 $this->fail('MAIL FROM should accept a 250 response');
279 } catch (Swift_TransportException $e) {
283 public function testSenderIsPreferredOverFrom()
285 $buf = $this->getBuffer();
286 $smtp = $this->getTransport($buf);
287 $message = $this->createMessage();
289 $message->shouldReceive('getFrom')
291 ->andReturn(['me@domain.com' => 'Me']);
292 $message->shouldReceive('getSender')
294 ->andReturn(['another@domain.com' => 'Someone']);
295 $message->shouldReceive('getTo')
297 ->andReturn(['foo@bar' => null]);
298 $buf->shouldReceive('write')
300 ->with("MAIL FROM:<another@domain.com>\r\n")
302 $buf->shouldReceive('readLine')
305 ->andReturn('250 OK'."\r\n");
307 $this->finishBuffer($buf);
309 $smtp->send($message);
312 public function testReturnPathIsPreferredOverSender()
314 $buf = $this->getBuffer();
315 $smtp = $this->getTransport($buf);
316 $message = $this->createMessage();
318 $message->shouldReceive('getFrom')
320 ->andReturn(['me@domain.com' => 'Me']);
321 $message->shouldReceive('getSender')
323 ->andReturn(['another@domain.com' => 'Someone']);
324 $message->shouldReceive('getReturnPath')
326 ->andReturn('more@domain.com');
327 $message->shouldReceive('getTo')
329 ->andReturn(['foo@bar' => null]);
330 $buf->shouldReceive('write')
332 ->with("MAIL FROM:<more@domain.com>\r\n")
334 $buf->shouldReceive('readLine')
337 ->andReturn('250 OK'."\r\n");
339 $this->finishBuffer($buf);
341 $smtp->send($message);
344 public function testSuccessfulRcptCommandWith250Response()
348 The second step in the procedure is the RCPT command.
350 RCPT TO:<forward-path> [ SP <rcpt-parameters> ] <CRLF>
352 The first or only argument to this command includes a forward-path
353 (normally a mailbox and domain, always surrounded by "<" and ">"
354 brackets) identifying one recipient. If accepted, the SMTP server
355 returns a 250 OK reply and stores the forward-path. If the recipient
356 is known not to be a deliverable address, the SMTP server returns a
357 550 reply, typically with a string such as "no such user - " and the
358 mailbox name (other circumstances and reply codes are possible).
359 This step of the procedure can be repeated any number of times.
361 -- RFC 2821, 4.1.1.3.
363 This command is used to identify an individual recipient of the mail
364 data; multiple recipients are specified by multiple use of this
365 command. The argument field contains a forward-path and may contain
368 The forward-path normally consists of the required destination
369 mailbox. Sending systems SHOULD not generate the optional list of
370 hosts known as a source route.
374 "RCPT TO:" ("<Postmaster@" domain ">" / "<Postmaster>" / Forward-Path)
375 [SP Rcpt-parameters] CRLF
379 250 Requested mail action okay, completed
380 251 User not local; will forward to <forward-path>
382 252 Cannot VRFY user, but will accept message and attempt
388 S: 250, 251 (but see section 3.4 for discussion of 251 and 551)
389 E: 550, 551, 552, 553, 450, 451, 452, 503, 550
392 //We'll treat 252 as accepted since it isn't really a failure
394 $buf = $this->getBuffer();
395 $smtp = $this->getTransport($buf);
396 $message = $this->createMessage();
398 $message->shouldReceive('getFrom')
400 ->andReturn(['me@domain.com' => 'Me']);
401 $message->shouldReceive('getTo')
403 ->andReturn(['foo@bar' => null]);
404 $buf->shouldReceive('write')
406 ->with("MAIL FROM:<me@domain.com>\r\n")
408 $buf->shouldReceive('readLine')
411 ->andReturn('250 OK'."\r\n");
412 $buf->shouldReceive('write')
414 ->with("RCPT TO:<foo@bar>\r\n")
416 $buf->shouldReceive('readLine')
419 ->andReturn('250 OK'."\r\n");
421 $this->finishBuffer($buf);
424 $smtp->send($message);
425 } catch (Exception $e) {
426 $this->fail('RCPT TO should accept a 250 response');
430 public function testUtf8AddressWithIdnEncoder()
432 $buf = $this->getBuffer();
433 $smtp = $this->getTransport($buf);
434 $message = $this->createMessage();
436 $message->shouldReceive('getFrom')
438 ->andReturn(['me@dömain.com' => 'Me']);
439 $message->shouldReceive('getTo')
441 ->andReturn(['foo@bär' => null]);
442 $buf->shouldReceive('write')
444 ->with("MAIL FROM:<me@xn--dmain-jua.com>\r\n")
446 $buf->shouldReceive('write')
448 ->with("RCPT TO:<foo@xn--br-via>\r\n")
450 $buf->shouldReceive('readLine')
453 ->andReturn('250 OK'."\r\n");
455 $this->finishBuffer($buf);
457 $smtp->send($message);
460 public function testUtf8AddressWithUtf8Encoder()
462 $buf = $this->getBuffer();
463 $smtp = $this->getTransport($buf, null, new Swift_AddressEncoder_Utf8AddressEncoder());
464 $message = $this->createMessage();
466 $message->shouldReceive('getFrom')
468 ->andReturn(['më@dömain.com' => 'Me']);
469 $message->shouldReceive('getTo')
471 ->andReturn(['föö@bär' => null]);
472 $buf->shouldReceive('write')
474 ->with("MAIL FROM:<më@dömain.com>\r\n")
476 $buf->shouldReceive('write')
478 ->with("RCPT TO:<föö@bär>\r\n")
480 $buf->shouldReceive('readLine')
483 ->andReturn('250 OK'."\r\n");
485 $this->finishBuffer($buf);
487 $smtp->send($message);
490 public function testNonEncodableSenderCausesException()
492 $buf = $this->getBuffer();
493 $smtp = $this->getTransport($buf);
494 $message = $this->createMessage();
496 $message->shouldReceive('getFrom')
498 ->andReturn(['më@domain.com' => 'Me']);
499 $message->shouldReceive('getTo')
501 ->andReturn(['foo@bar' => null]);
503 $this->finishBuffer($buf);
506 $smtp->send($message);
507 $this->fail('më@domain.com cannot be encoded (not observed)');
508 } catch (Swift_AddressEncoderException $e) {
509 $this->assertEquals('më@domain.com', $e->getAddress());
513 public function testMailFromCommandIsOnlySentOncePerMessage()
515 $buf = $this->getBuffer();
516 $smtp = $this->getTransport($buf);
517 $message = $this->createMessage();
519 $message->shouldReceive('getFrom')
521 ->andReturn(['me@domain.com' => 'Me']);
522 $message->shouldReceive('getTo')
524 ->andReturn(['foo@bar' => null]);
525 $buf->shouldReceive('write')
527 ->with("MAIL FROM:<me@domain.com>\r\n")
529 $buf->shouldReceive('readLine')
532 ->andReturn('250 OK'."\r\n");
533 $buf->shouldReceive('write')
535 ->with("RCPT TO:<foo@bar>\r\n")
537 $buf->shouldReceive('readLine')
540 ->andReturn('250 OK'."\r\n");
541 $buf->shouldReceive('write')
543 ->with("MAIL FROM:<me@domain.com>\r\n");
545 $this->finishBuffer($buf);
547 $smtp->send($message);
550 public function testMultipleRecipientsSendsMultipleRcpt()
552 $buf = $this->getBuffer();
553 $smtp = $this->getTransport($buf);
554 $message = $this->createMessage();
556 $message->shouldReceive('getFrom')
558 ->andReturn(['me@domain.com' => 'Me']);
559 $message->shouldReceive('getTo')
563 'zip@button' => 'Zip Button',
564 'test@domain' => 'Test user',
565 'tëst@domain' => 'Test user',
567 $buf->shouldReceive('write')
569 ->with("RCPT TO:<foo@bar>\r\n")
571 $buf->shouldReceive('readLine')
574 ->andReturn('250 OK'."\r\n");
575 $buf->shouldReceive('write')
577 ->with("RCPT TO:<zip@button>\r\n")
579 $buf->shouldReceive('readLine')
582 ->andReturn('250 OK'."\r\n");
583 $buf->shouldReceive('write')
585 ->with("RCPT TO:<test@domain>\r\n")
587 $buf->shouldReceive('readLine')
590 ->andReturn('250 OK'."\r\n");
592 $this->finishBuffer($buf);
594 $smtp->send($message);
597 public function testCcRecipientsSendsMultipleRcpt()
599 $buf = $this->getBuffer();
600 $smtp = $this->getTransport($buf);
601 $message = $this->createMessage();
603 $message->shouldReceive('getFrom')
605 ->andReturn(['me@domain.com' => 'Me']);
606 $message->shouldReceive('getTo')
608 ->andReturn(['foo@bar' => null]);
609 $message->shouldReceive('getCc')
612 'zip@button' => 'Zip Button',
613 'test@domain' => 'Test user',
615 $buf->shouldReceive('write')
617 ->with("RCPT TO:<foo@bar>\r\n")
619 $buf->shouldReceive('readLine')
622 ->andReturn('250 OK'."\r\n");
623 $buf->shouldReceive('write')
625 ->with("RCPT TO:<zip@button>\r\n")
627 $buf->shouldReceive('readLine')
630 ->andReturn('250 OK'."\r\n");
631 $buf->shouldReceive('write')
633 ->with("RCPT TO:<test@domain>\r\n")
635 $buf->shouldReceive('readLine')
638 ->andReturn('250 OK'."\r\n");
640 $this->finishBuffer($buf);
642 $smtp->send($message);
645 public function testSendReturnsNumberOfSuccessfulRecipients()
647 $buf = $this->getBuffer();
648 $smtp = $this->getTransport($buf);
649 $message = $this->createMessage();
651 $message->shouldReceive('getFrom')
653 ->andReturn(['me@domain.com' => 'Me']);
654 $message->shouldReceive('getTo')
656 ->andReturn(['foo@bar' => null]);
657 $message->shouldReceive('getCc')
660 'zip@button' => 'Zip Button',
661 'test@domain' => 'Test user',
663 $buf->shouldReceive('write')
665 ->with("RCPT TO:<foo@bar>\r\n")
667 $buf->shouldReceive('readLine')
670 ->andReturn('250 OK'."\r\n");
671 $buf->shouldReceive('write')
673 ->with("RCPT TO:<zip@button>\r\n")
675 $buf->shouldReceive('readLine')
678 ->andReturn('501 Nobody here'."\r\n");
679 $buf->shouldReceive('write')
681 ->with("RCPT TO:<test@domain>\r\n")
683 $buf->shouldReceive('readLine')
686 ->andReturn('250 OK'."\r\n");
688 $this->finishBuffer($buf);
690 $this->assertEquals(2, $smtp->send($message),
691 '%s: 1 of 3 recipients failed so 2 should be returned'
695 public function testRsetIsSentIfNoSuccessfulRecipients()
697 /* --RFC 2821, 4.1.1.5.
699 This command specifies that the current mail transaction will be
700 aborted. Any stored sender, recipients, and mail data MUST be
701 discarded, and all buffers and state tables cleared. The receiver
702 MUST send a "250 OK" reply to a RSET command with no arguments. A
703 reset command may be issued by the client at any time.
711 $buf = $this->getBuffer();
712 $smtp = $this->getTransport($buf);
713 $message = $this->createMessage();
715 $message->shouldReceive('getFrom')
717 ->andReturn(['me@domain.com' => 'Me']);
718 $message->shouldReceive('getTo')
720 ->andReturn(['foo@bar' => null]);
721 $buf->shouldReceive('write')
723 ->with("RCPT TO:<foo@bar>\r\n")
725 $buf->shouldReceive('readLine')
728 ->andReturn('503 Bad'."\r\n");
729 $buf->shouldReceive('write')
733 $buf->shouldReceive('readLine')
736 ->andReturn('250 OK'."\r\n");
738 $this->finishBuffer($buf);
740 $this->assertEquals(0, $smtp->send($message),
741 '%s: 1 of 1 recipients failed so 0 should be returned'
745 public function testSuccessfulDataCommand()
749 The third step in the procedure is the DATA command (or some
750 alternative specified in a service extension).
754 If accepted, the SMTP server returns a 354 Intermediate reply and
755 considers all succeeding lines up to but not including the end of
756 mail data indicator to be the message text.
758 -- RFC 2821, 4.1.1.4.
760 The receiver normally sends a 354 response to DATA, and then treats
761 the lines (strings ending in <CRLF> sequences, as described in
762 section 2.3.7) following the command as mail data from the sender.
763 This command causes the mail data to be appended to the mail data
764 buffer. The mail data may contain any of the 128 ASCII character
765 codes, although experience has indicated that use of control
766 characters other than SP, HT, CR, and LF may cause problems and
767 SHOULD be avoided when possible.
772 I: 354 -> data -> S: 250
773 E: 552, 554, 451, 452
777 $buf = $this->getBuffer();
778 $smtp = $this->getTransport($buf);
779 $message = $this->createMessage();
781 $message->shouldReceive('getFrom')
783 ->andReturn(['me@domain.com' => 'Me']);
784 $message->shouldReceive('getTo')
786 ->andReturn(['foo@bar' => null]);
787 $buf->shouldReceive('write')
791 $buf->shouldReceive('readLine')
794 ->andReturn('354 Go ahead'."\r\n");
796 $this->finishBuffer($buf);
799 $smtp->send($message);
800 } catch (Exception $e) {
801 $this->fail('354 is the expected response to DATA');
805 public function testBadDataResponseCausesException()
807 $buf = $this->getBuffer();
808 $smtp = $this->getTransport($buf);
809 $message = $this->createMessage();
811 $message->shouldReceive('getFrom')
813 ->andReturn(['me@domain.com' => 'Me']);
814 $message->shouldReceive('getTo')
816 ->andReturn(['foo@bar' => null]);
817 $buf->shouldReceive('write')
821 $buf->shouldReceive('readLine')
824 ->andReturn('451 Bad'."\r\n");
826 $this->finishBuffer($buf);
829 $smtp->send($message);
830 $this->fail('354 is the expected response to DATA (not observed)');
831 } catch (Swift_TransportException $e) {
835 public function testMessageIsStreamedToBufferForData()
837 $buf = $this->getBuffer();
838 $smtp = $this->getTransport($buf);
839 $message = $this->createMessage();
841 $message->shouldReceive('getFrom')
843 ->andReturn(['me@domain.com' => 'Me']);
844 $message->shouldReceive('getTo')
846 ->andReturn(['foo@bar' => null]);
847 $buf->shouldReceive('write')
851 $buf->shouldReceive('readLine')
854 ->andReturn('354 OK'."\r\n");
855 $buf->shouldReceive('write')
859 $buf->shouldReceive('readLine')
862 ->andReturn('250 OK'."\r\n");
864 $this->finishBuffer($buf);
866 $smtp->send($message);
869 public function testBadResponseAfterDataTransmissionCausesException()
871 $buf = $this->getBuffer();
872 $smtp = $this->getTransport($buf);
873 $message = $this->createMessage();
875 $message->shouldReceive('getFrom')
877 ->andReturn(['me@domain.com' => 'Me']);
878 $message->shouldReceive('getTo')
880 ->andReturn(['foo@bar' => null]);
881 $buf->shouldReceive('write')
885 $buf->shouldReceive('readLine')
888 ->andReturn('354 OK'."\r\n");
889 $buf->shouldReceive('write')
893 $buf->shouldReceive('readLine')
896 ->andReturn('554 Error'."\r\n");
898 $this->finishBuffer($buf);
901 $smtp->send($message);
902 $this->fail('250 is the expected response after a DATA transmission (not observed)');
903 } catch (Swift_TransportException $e) {
907 public function testBccRecipientsAreRemovedFromHeaders()
911 Addresses that do not appear in the message headers may appear in the
912 RCPT commands to an SMTP server for a number of reasons. The two
913 most common involve the use of a mailing address as a "list exploder"
914 (a single address that resolves into multiple addresses) and the
915 appearance of "blind copies". Especially when more than one RCPT
916 command is present, and in order to avoid defeating some of the
917 purpose of these mechanisms, SMTP clients and servers SHOULD NOT copy
918 the full set of RCPT command arguments into the headers, either as
919 part of trace headers or as informational or private-extension
920 headers. Since this rule is often violated in practice, and cannot
921 be enforced, sending SMTP systems that are aware of "bcc" use MAY
922 find it helpful to send each blind copy as a separate message
923 transaction containing only a single RCPT command.
926 $buf = $this->getBuffer();
927 $smtp = $this->getTransport($buf);
928 $message = $this->createMessage();
929 $message->shouldReceive('getFrom')
931 ->andReturn(['me@domain.com' => 'Me']);
932 $message->shouldReceive('getTo')
934 ->andReturn(['foo@bar' => null]);
935 $message->shouldReceive('getBcc')
938 'zip@button' => 'Zip Button',
939 'test@domain' => 'Test user',
941 $message->shouldReceive('setBcc')
944 $message->shouldReceive('setBcc')
947 $this->finishBuffer($buf);
949 $smtp->send($message);
952 public function testEachBccRecipientIsSentASeparateMessage()
954 $buf = $this->getBuffer();
955 $smtp = $this->getTransport($buf);
956 $message = $this->createMessage();
958 $message->shouldReceive('getFrom')
960 ->andReturn(['me@domain.com' => 'Me']);
961 $message->shouldReceive('getTo')
963 ->andReturn(['foo@bar' => null]);
964 $message->shouldReceive('getBcc')
967 'zip@button' => 'Zip Button',
968 'test@domain' => 'Test user',
970 $message->shouldReceive('setBcc')
973 $message->shouldReceive('setBcc')
975 ->with(['zip@button' => 'Zip Button']);
976 $message->shouldReceive('setBcc')
978 ->with(['test@domain' => 'Test user']);
979 $message->shouldReceive('setBcc')
982 'zip@button' => 'Zip Button',
983 'test@domain' => 'Test user',
986 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(1);
987 $buf->shouldReceive('readLine')->once()->with(1)->andReturn("250 OK\r\n");
988 $buf->shouldReceive('write')->once()->with("RCPT TO:<foo@bar>\r\n")->andReturn(2);
989 $buf->shouldReceive('readLine')->once()->with(2)->andReturn("250 OK\r\n");
990 $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(3);
991 $buf->shouldReceive('readLine')->once()->with(3)->andReturn("354 OK\r\n");
992 $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(4);
993 $buf->shouldReceive('readLine')->once()->with(4)->andReturn("250 OK\r\n");
995 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(5);
996 $buf->shouldReceive('readLine')->once()->with(5)->andReturn("250 OK\r\n");
997 $buf->shouldReceive('write')->once()->with("RCPT TO:<zip@button>\r\n")->andReturn(6);
998 $buf->shouldReceive('readLine')->once()->with(6)->andReturn("250 OK\r\n");
999 $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(7);
1000 $buf->shouldReceive('readLine')->once()->with(7)->andReturn("354 OK\r\n");
1001 $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(8);
1002 $buf->shouldReceive('readLine')->once()->with(8)->andReturn("250 OK\r\n");
1004 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(9);
1005 $buf->shouldReceive('readLine')->once()->with(9)->andReturn("250 OK\r\n");
1006 $buf->shouldReceive('write')->once()->with("RCPT TO:<test@domain>\r\n")->andReturn(10);
1007 $buf->shouldReceive('readLine')->once()->with(10)->andReturn("250 OK\r\n");
1008 $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(11);
1009 $buf->shouldReceive('readLine')->once()->with(11)->andReturn("354 OK\r\n");
1010 $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(12);
1011 $buf->shouldReceive('readLine')->once()->with(12)->andReturn("250 OK\r\n");
1013 $this->finishBuffer($buf);
1015 $this->assertEquals(3, $smtp->send($message));
1018 public function testMessageStateIsRestoredOnFailure()
1020 $buf = $this->getBuffer();
1021 $smtp = $this->getTransport($buf);
1022 $message = $this->createMessage();
1024 $message->shouldReceive('getFrom')
1026 ->andReturn(['me@domain.com' => 'Me']);
1027 $message->shouldReceive('getTo')
1029 ->andReturn(['foo@bar' => null]);
1030 $message->shouldReceive('getBcc')
1033 'zip@button' => 'Zip Button',
1034 'test@domain' => 'Test user',
1036 $message->shouldReceive('setBcc')
1039 $message->shouldReceive('setBcc')
1042 'zip@button' => 'Zip Button',
1043 'test@domain' => 'Test user',
1045 $buf->shouldReceive('write')
1047 ->with("MAIL FROM:<me@domain.com>\r\n")
1049 $buf->shouldReceive('readLine')
1052 ->andReturn("250 OK\r\n");
1053 $buf->shouldReceive('write')
1055 ->with("RCPT TO:<foo@bar>\r\n")
1057 $buf->shouldReceive('readLine')
1060 ->andReturn("250 OK\r\n");
1061 $buf->shouldReceive('write')
1065 $buf->shouldReceive('readLine')
1068 ->andReturn("451 No\r\n");
1070 $this->finishBuffer($buf);
1074 $smtp->send($message);
1075 $this->fail('A bad response was given so exception is expected');
1076 } catch (Swift_TransportException $e) {
1080 public function testStopSendsQuitCommand()
1082 /* -- RFC 2821, 4.1.1.10.
1084 This command specifies that the receiver MUST send an OK reply, and
1085 then close the transmission channel.
1087 The receiver MUST NOT intentionally close the transmission channel
1088 until it receives and replies to a QUIT command (even if there was an
1089 error). The sender MUST NOT intentionally close the transmission
1090 channel until it sends a QUIT command and SHOULD wait until it
1091 receives the reply (even if there was an error response to a previous
1092 command). If the connection is closed prematurely due to violations
1093 of the above or system or network failure, the server MUST cancel any
1094 pending transaction, but not undo any previously completed
1095 transaction, and generally MUST act as if the command or transaction
1096 in progress had received a temporary error (i.e., a 4yz response).
1098 The QUIT command may be issued at any time.
1104 $buf = $this->getBuffer();
1105 $smtp = $this->getTransport($buf);
1106 $message = $this->createMessage();
1107 $buf->shouldReceive('initialize')
1109 $buf->shouldReceive('write')
1113 $buf->shouldReceive('readLine')
1116 ->andReturn("221 Bye\r\n");
1117 $buf->shouldReceive('terminate')
1120 $this->finishBuffer($buf);
1122 $this->assertFalse($smtp->isStarted());
1124 $this->assertTrue($smtp->isStarted());
1126 $this->assertFalse($smtp->isStarted());
1129 public function testBufferCanBeFetched()
1131 $buf = $this->getBuffer();
1132 $smtp = $this->getTransport($buf);
1133 $ref = $smtp->getBuffer();
1134 $this->assertEquals($buf, $ref);
1137 public function testBufferCanBeWrittenToUsingExecuteCommand()
1139 $buf = $this->getBuffer();
1140 $smtp = $this->getTransport($buf);
1141 $message = $this->createMessage();
1142 $buf->shouldReceive('write')
1146 $buf->shouldReceive('readLine')
1149 ->andReturn("250 OK\r\n");
1151 $res = $smtp->executeCommand("FOO\r\n");
1152 $this->assertEquals("250 OK\r\n", $res);
1155 public function testResponseCodesAreValidated()
1157 $buf = $this->getBuffer();
1158 $smtp = $this->getTransport($buf);
1159 $message = $this->createMessage();
1160 $buf->shouldReceive('write')
1164 $buf->shouldReceive('readLine')
1167 ->andReturn("551 Not ok\r\n");
1170 $smtp->executeCommand("FOO\r\n", [250, 251]);
1171 $this->fail('A 250 or 251 response was needed but 551 was returned.');
1172 } catch (Swift_TransportException $e) {
1176 public function testFailedRecipientsCanBeCollectedByReference()
1178 $buf = $this->getBuffer();
1179 $smtp = $this->getTransport($buf);
1180 $message = $this->createMessage();
1182 $message->shouldReceive('getFrom')
1184 ->andReturn(['me@domain.com' => 'Me']);
1185 $message->shouldReceive('getTo')
1187 ->andReturn(['foo@bar' => null]);
1188 $message->shouldReceive('getBcc')
1191 'zip@button' => 'Zip Button',
1192 'test@domain' => 'Test user',
1194 $message->shouldReceive('setBcc')
1197 $message->shouldReceive('setBcc')
1199 ->with(['zip@button' => 'Zip Button']);
1200 $message->shouldReceive('setBcc')
1202 ->with(['test@domain' => 'Test user']);
1203 $message->shouldReceive('setBcc')
1206 'zip@button' => 'Zip Button',
1207 'test@domain' => 'Test user',
1210 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(1);
1211 $buf->shouldReceive('readLine')->once()->with(1)->andReturn("250 OK\r\n");
1212 $buf->shouldReceive('write')->once()->with("RCPT TO:<foo@bar>\r\n")->andReturn(2);
1213 $buf->shouldReceive('readLine')->once()->with(2)->andReturn("250 OK\r\n");
1214 $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(3);
1215 $buf->shouldReceive('readLine')->once()->with(3)->andReturn("354 OK\r\n");
1216 $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(4);
1217 $buf->shouldReceive('readLine')->once()->with(4)->andReturn("250 OK\r\n");
1219 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(5);
1220 $buf->shouldReceive('readLine')->once()->with(5)->andReturn("250 OK\r\n");
1221 $buf->shouldReceive('write')->once()->with("RCPT TO:<zip@button>\r\n")->andReturn(6);
1222 $buf->shouldReceive('readLine')->once()->with(6)->andReturn("500 Bad\r\n");
1223 $buf->shouldReceive('write')->once()->with("RSET\r\n")->andReturn(7);
1224 $buf->shouldReceive('readLine')->once()->with(7)->andReturn("250 OK\r\n");
1226 $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(9);
1227 $buf->shouldReceive('readLine')->once()->with(9)->andReturn("250 OK\r\n");
1228 $buf->shouldReceive('write')->once()->with("RCPT TO:<test@domain>\r\n")->andReturn(10);
1229 $buf->shouldReceive('readLine')->once()->with(10)->andReturn("500 Bad\r\n");
1230 $buf->shouldReceive('write')->once()->with("RSET\r\n")->andReturn(11);
1231 $buf->shouldReceive('readLine')->once()->with(11)->andReturn("250 OK\r\n");
1233 $this->finishBuffer($buf);
1235 $this->assertEquals(1, $smtp->send($message, $failures));
1236 $this->assertEquals(['zip@button', 'test@domain'], $failures,
1237 '%s: Failures should be caught in an array'
1241 public function testSendingRegeneratesMessageId()
1243 $buf = $this->getBuffer();
1244 $smtp = $this->getTransport($buf);
1245 $message = $this->createMessage();
1246 $message->shouldReceive('getFrom')
1248 ->andReturn(['me@domain.com' => 'Me']);
1249 $message->shouldReceive('getTo')
1251 ->andReturn(['foo@bar' => null]);
1252 $message->shouldReceive('generateId')
1255 $this->finishBuffer($buf);
1257 $smtp->send($message);
1260 public function testPing()
1262 $buf = $this->getBuffer();
1263 $smtp = $this->getTransport($buf);
1265 $buf->shouldReceive('initialize')
1267 $buf->shouldReceive('readLine')
1270 ->andReturn("220 some.server.tld bleh\r\n");
1271 $buf->shouldReceive('write')
1273 ->with('~^NOOP\r\n$~D')
1275 $buf->shouldReceive('readLine')
1278 ->andReturn('250 OK'."\r\n");
1280 $this->finishBuffer($buf);
1281 $this->assertTrue($smtp->ping());
1284 public function testPingOnDeadConnection()
1286 $buf = $this->getBuffer();
1287 $smtp = $this->getTransport($buf);
1289 $buf->shouldReceive('initialize')
1291 $buf->shouldReceive('readLine')
1294 ->andReturn("220 some.server.tld bleh\r\n");
1295 $buf->shouldReceive('write')
1297 ->with('~^NOOP\r\n$~D')
1298 ->andThrow('Swift_TransportException');
1300 $this->finishBuffer($buf);
1302 $this->assertTrue($smtp->isStarted());
1303 $this->assertFalse($smtp->ping());
1304 $this->assertFalse($smtp->isStarted());
1307 public function testSetLocalDomain()
1309 $buf = $this->getBuffer();
1310 $smtp = $this->getTransport($buf);
1312 $smtp->setLocalDomain('example.com');
1313 $this->assertEquals('example.com', $smtp->getLocalDomain());
1315 $smtp->setLocalDomain('192.168.0.1');
1316 $this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
1318 $smtp->setLocalDomain('[192.168.0.1]');
1319 $this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
1321 $smtp->setLocalDomain('fd00::');
1322 $this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
1324 $smtp->setLocalDomain('[IPv6:fd00::]');
1325 $this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
1328 protected function getBuffer()
1330 return $this->getMockery('Swift_Transport_IoBuffer')->shouldIgnoreMissing();
1333 protected function createMessage()
1335 return $this->getMockery('Swift_Mime_SimpleMessage')->shouldIgnoreMissing();
1338 protected function finishBuffer($buf)
1340 $buf->shouldReceive('readLine')
1343 ->andReturn('220 server.com foo'."\r\n");
1344 $buf->shouldReceive('write')
1346 ->with('~^(EH|HE)LO .*?\r\n$~D')
1347 ->andReturn($x = uniqid('', true));
1348 $buf->shouldReceive('readLine')
1351 ->andReturn('250 ServerName'."\r\n");
1352 $buf->shouldReceive('write')
1354 ->with('~^MAIL FROM:<.*?>\r\n$~D')
1355 ->andReturn($x = uniqid('', true));
1356 $buf->shouldReceive('readLine')
1359 ->andReturn("250 OK\r\n");
1360 $buf->shouldReceive('write')
1362 ->with('~^RCPT TO:<.*?>\r\n$~D')
1363 ->andReturn($x = uniqid('', true));
1364 $buf->shouldReceive('readLine')
1367 ->andReturn("250 OK\r\n");
1368 $buf->shouldReceive('write')
1371 ->andReturn($x = uniqid('', true));
1372 $buf->shouldReceive('readLine')
1375 ->andReturn("354 OK\r\n");
1376 $buf->shouldReceive('write')
1379 ->andReturn($x = uniqid('', true));
1380 $buf->shouldReceive('readLine')
1383 ->andReturn("250 OK\r\n");
1384 $buf->shouldReceive('write')
1387 ->andReturn($x = uniqid('', true));
1388 $buf->shouldReceive('readLine')
1391 ->andReturn("250 OK\r\n");
1393 $buf->shouldReceive('write')
1396 $buf->shouldReceive('readLine')