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
private string $ argsPath ;
25
27
@@ -86,4 +88,27 @@ public function testRecipientsAreUsedWhenSet()
86
88
87
89
$ this ->assertStringEqualsFile ($ this ->argsPath , __DIR__ .'/Fixtures/fake-sendmail.php -ffrom@mail.com recipient@mail.com ' );
88
90
}
91
+
92
+ public function testThrowsTransportExceptionOnFailure ()
93
+ {
94
+ if ('\\' === \DIRECTORY_SEPARATOR ) {
95
+ $ this ->markTestSkipped ('Windows does not support shebangs nor non-blocking standard streams ' );
96
+ }
97
+
98
+ $ mail = new Email ();
99
+ $ mail
100
+ ->from ('from@mail.com ' )
101
+ ->to ('to@mail.com ' )
102
+ ->subject ('Subject ' )
103
+ ->text ('Some text ' )
104
+ ;
105
+
106
+ $ envelope = new DelayedEnvelope ($ mail );
107
+ $ envelope ->setRecipients ([new Address ('recipient@mail.com ' )]);
108
+
109
+ $ sendmailTransport = new SendmailTransport (self ::FAKE_FAILING_SENDMAIL );
110
+ $ this ->expectException (TransportException::class);
111
+ $ this ->expectExceptionMessage ('Process failed with exit code 42: Sending failed ' );
112
+ $ sendmailTransport ->send ($ mail , $ envelope );
113
+ }
89
114
}
Original file line number Diff line number Diff line change @@ -30,6 +30,7 @@ abstract class AbstractStream
30
30
protected $ in ;
31
31
/** @var resource|null */
32
32
protected $ out ;
33
+ protected $ err ;
33
34
34
35
private string $ debug = '' ;
35
36
@@ -68,7 +69,7 @@ abstract public function initialize(): void;
68
69
69
70
public function terminate (): void
70
71
{
71
- $ this ->stream = $ this ->out = $ this ->in = null ;
72
+ $ this ->stream = $ this ->err = $ this -> out = $ this ->in = null ;
72
73
}
73
74
74
75
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