| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mailer\Transport;
|
|
|
13 |
|
|
|
14 |
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
|
15 |
use Psr\Log\LoggerInterface;
|
|
|
16 |
use Symfony\Component\Mailer\Envelope;
|
|
|
17 |
use Symfony\Component\Mailer\SentMessage;
|
|
|
18 |
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
|
|
|
19 |
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
|
|
|
20 |
use Symfony\Component\Mailer\Transport\Smtp\Stream\ProcessStream;
|
|
|
21 |
use Symfony\Component\Mime\RawMessage;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
|
|
|
25 |
*
|
|
|
26 |
* Transport can be instantiated through SendmailTransportFactory or NativeTransportFactory:
|
|
|
27 |
*
|
|
|
28 |
* - SendmailTransportFactory to use most common sendmail path and recommended options
|
|
|
29 |
* - NativeTransportFactory when configuration is set via php.ini
|
|
|
30 |
*
|
|
|
31 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
32 |
* @author Chris Corbyn
|
|
|
33 |
*/
|
|
|
34 |
class SendmailTransport extends AbstractTransport
|
|
|
35 |
{
|
|
|
36 |
private string $command = '/usr/sbin/sendmail -bs';
|
|
|
37 |
private ProcessStream $stream;
|
|
|
38 |
private ?SmtpTransport $transport = null;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Constructor.
|
|
|
42 |
*
|
|
|
43 |
* Supported modes are -bs and -t, with any additional flags desired.
|
|
|
44 |
*
|
|
|
45 |
* The recommended mode is "-bs" since it is interactive and failure notifications are hence possible.
|
|
|
46 |
* Note that the -t mode does not support error reporting and does not support Bcc properly (the Bcc headers are not removed).
|
|
|
47 |
*
|
|
|
48 |
* If using -t mode, you are strongly advised to include -oi or -i in the flags (like /usr/sbin/sendmail -oi -t)
|
|
|
49 |
*
|
|
|
50 |
* -f<sender> flag will be appended automatically if one is not present.
|
|
|
51 |
*/
|
|
|
52 |
public function __construct(string $command = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
|
|
|
53 |
{
|
|
|
54 |
parent::__construct($dispatcher, $logger);
|
|
|
55 |
|
|
|
56 |
if (null !== $command) {
|
|
|
57 |
if (!str_contains($command, ' -bs') && !str_contains($command, ' -t')) {
|
|
|
58 |
throw new \InvalidArgumentException(sprintf('Unsupported sendmail command flags "%s"; must be one of "-bs" or "-t" but can include additional flags.', $command));
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$this->command = $command;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$this->stream = new ProcessStream();
|
|
|
65 |
if (str_contains($this->command, ' -bs')) {
|
|
|
66 |
$this->stream->setCommand($this->command);
|
|
|
67 |
$this->transport = new SmtpTransport($this->stream, $dispatcher, $logger);
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
|
|
|
72 |
{
|
|
|
73 |
if ($this->transport) {
|
|
|
74 |
return $this->transport->send($message, $envelope);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return parent::send($message, $envelope);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public function __toString(): string
|
|
|
81 |
{
|
|
|
82 |
if ($this->transport) {
|
|
|
83 |
return (string) $this->transport;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
return 'smtp://sendmail';
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
protected function doSend(SentMessage $message): void
|
|
|
90 |
{
|
|
|
91 |
$this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));
|
|
|
92 |
|
|
|
93 |
$command = $this->command;
|
|
|
94 |
|
|
|
95 |
if ($recipients = $message->getEnvelope()->getRecipients()) {
|
|
|
96 |
$command = str_replace(' -t', '', $command);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
if (!str_contains($command, ' -f')) {
|
|
|
100 |
$command .= ' -f'.escapeshellarg($message->getEnvelope()->getSender()->getEncodedAddress());
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
$chunks = AbstractStream::replace("\r\n", "\n", $message->toIterable());
|
|
|
104 |
|
|
|
105 |
if (!str_contains($command, ' -i') && !str_contains($command, ' -oi')) {
|
|
|
106 |
$chunks = AbstractStream::replace("\n.", "\n..", $chunks);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
foreach ($recipients as $recipient) {
|
|
|
110 |
$command .= ' '.escapeshellarg($recipient->getEncodedAddress());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$this->stream->setCommand($command);
|
|
|
114 |
$this->stream->initialize();
|
|
|
115 |
foreach ($chunks as $chunk) {
|
|
|
116 |
$this->stream->write($chunk);
|
|
|
117 |
}
|
|
|
118 |
$this->stream->flush();
|
|
|
119 |
$this->stream->terminate();
|
|
|
120 |
|
|
|
121 |
$this->getLogger()->debug(sprintf('Email transport "%s" stopped', __CLASS__));
|
|
|
122 |
}
|
|
|
123 |
}
|