File tree Expand file tree Collapse file tree 4 files changed +38
-2
lines changed Expand file tree Collapse file tree 4 files changed +38
-2
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env php
2
+ <?php
3
+ print "Sending failed " ;
4
+ exit (42 );
Original file line number Diff line number Diff line change 13
13
14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \Mailer \DelayedEnvelope ;
16
+ use Symfony \Component \Mailer \Exception \TransportException ;
16
17
use Symfony \Component \Mailer \Transport \SendmailTransport ;
17
18
use Symfony \Component \Mime \Address ;
18
19
use Symfony \Component \Mime \Email ;
19
20
20
21
class SendmailTransportTest extends TestCase
21
22
{
22
23
private const FAKE_SENDMAIL = __DIR__ .'/Fixtures/fake-sendmail.php -t ' ;
24
+ private const FAKE_FAILING_SENDMAIL = __DIR__ .'/Fixtures/fake-failing-sendmail.php -t ' ;
23
25
24
26
/**
25
27
* @var string
@@ -89,4 +91,27 @@ public function testRecipientsAreUsedWhenSet()
89
91
90
92
$ this ->assertStringEqualsFile ($ this ->argsPath , __DIR__ .'/Fixtures/fake-sendmail.php -ffrom@mail.com recipient@mail.com ' );
91
93
}
94
+
95
+ public function testThrowsTransportExceptionOnFailure ()
96
+ {
97
+ if ('\\' === \DIRECTORY_SEPARATOR ) {
98
+ $ this ->markTestSkipped ('Windows does not support shebangs nor non-blocking standard streams ' );
99
+ }
100
+
101
+ $ mail = new Email ();
102
+ $ mail
103
+ ->from ('from@mail.com ' )
104
+ ->to ('to@mail.com ' )
105
+ ->subject ('Subject ' )
106
+ ->text ('Some text ' )
107
+ ;
108
+
109
+ $ envelope = new DelayedEnvelope ($ mail );
110
+ $ envelope ->setRecipients ([new Address ('recipient@mail.com ' )]);
111
+
112
+ $ sendmailTransport = new SendmailTransport (self ::FAKE_FAILING_SENDMAIL );
113
+ $ this ->expectException (TransportException::class);
114
+ $ this ->expectExceptionMessage ('Process failed with exit code 42: Sending failed ' );
115
+ $ sendmailTransport ->send ($ mail , $ envelope );
116
+ }
92
117
}
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ abstract class AbstractStream
27
27
protected $ stream ;
28
28
protected $ in ;
29
29
protected $ out ;
30
+ protected $ err ;
30
31
31
32
private $ debug = '' ;
32
33
@@ -65,7 +66,7 @@ abstract public function initialize(): void;
65
66
66
67
public function terminate (): void
67
68
{
68
- $ this ->stream = $ this ->out = $ this ->in = null ;
69
+ $ this ->stream = $ this ->err = $ this -> out = $ this ->in = null ;
69
70
}
70
71
71
72
public function readLine (): string
Original file line number Diff line number Diff line change @@ -45,14 +45,20 @@ public function initialize(): void
45
45
}
46
46
$ this ->in = &$ pipes [0 ];
47
47
$ this ->out = &$ pipes [1 ];
48
+ $ this ->err = &$ pipes [2 ];
48
49
}
49
50
50
51
public function terminate (): void
51
52
{
52
53
if (null !== $ this ->stream ) {
53
54
fclose ($ this ->in );
55
+ $ out = stream_get_contents ($ this ->out );
54
56
fclose ($ this ->out );
55
- proc_close ($ this ->stream );
57
+ $ err = stream_get_contents ($ this ->err );
58
+ fclose ($ this ->err );
59
+ if (0 !== $ exitCode = proc_close ($ this ->stream )) {
60
+ throw new TransportException ('Process failed with exit code ' .$ exitCode .': ' .$ out .$ err );
61
+ }
56
62
}
57
63
58
64
parent ::terminate ();
You can’t perform that action at this time.
0 commit comments