1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* SendmailTransport for sending mail through a Sendmail/Postfix (etc..) binary.
*
* Supported modes are -bs and -t, with any additional flags desired.
* It is advised to use -bs mode since error reporting with -t mode is not
* possible.
*
* @author Chris Corbyn
*/
class Swift_Transport_SendmailTransport extends Swift_Transport_AbstractSmtpTransport
{
/**
* Connection buffer parameters.
*
* @var array
*/
private $_params = array(
'timeout' => 30,
'blocking' => 1,
'command' => '/usr/sbin/sendmail -bs',
'type' => Swift_Transport_IoBuffer::TYPE_PROCESS
);
/**
* Create a new SendmailTransport with $buf for I/O.
*
* @param Swift_Transport_IoBuffer $buf
* @param Swift_Events_EventDispatcher $dispatcher
*/
public function __construct(Swift_Transport_IoBuffer $buf, Swift_Events_EventDispatcher $dispatcher)
{
parent::__construct($buf, $dispatcher);
}
/**
* Start the standalone SMTP session if running in -bs mode.
*/
public function start()
{
if (false !== strpos($this->getCommand(), ' -bs')) {
parent::start();
}
}
/**
* Set the command to invoke.
*
* If using -t mode you are strongly advised to include -oi or -i in the flags.
* For example: /usr/sbin/sendmail -oi -t
* Swift will append a -f<sender> flag if one is not present.
*
* The recommended mode is "-bs" since it is interactive and failure notifications
* are hence possible.
*
* @param string $command
*
* @return Swift_Transport_SendmailTransport
*/
public function setCommand($command)
{
$this->_params['command'] = $command;
return $this;
}
/**
* Get the sendmail command which will be invoked.
*
* @return string
*/
public function getCommand()
{
return $this->_params['command'];
}
/**
* Send the given Message.
*
* Recipient/sender data will be retrieved from the Message API.
*
* The return value is the number of recipients who were accepted for delivery.
* NOTE: If using 'sendmail -t' you will not be aware of any failures until
* they bounce (i.e. send() will always return 100% success).
*
* @param Swift_Mime_Message $message
* @param string[] $failedRecipients An array of failures by-reference
*
* @return int
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$failedRecipients = (array) $failedRecipients;
$command = $this->getCommand();
$buffer = $this->getBuffer();
if (false !== strpos($command, ' -t')) {
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
return 0;
}
}
if (false === strpos($command, ' -f')) {
$command .= ' -f' . escapeshellarg($this->_getReversePath($message));
}
$buffer->initialize(array_merge($this->_params, array('command' => $command)));
if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
$buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
} else {
$buffer->setWriteTranslations(array("\r\n"=>"\n"));
}
$count = count((array) $message->getTo())
+ count((array) $message->getCc())
+ count((array) $message->getBcc())
;
$message->toByteStream($buffer);
$buffer->flushBuffers();
$buffer->setWriteTranslations(array());
$buffer->terminate();
if ($evt) {
$evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
$evt->setFailedRecipients($failedRecipients);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
}
$message->generateId();
} elseif (false !== strpos($command, ' -bs')) {
$count = parent::send($message, $failedRecipients);
} else {
$this->_throwException(new Swift_TransportException(
'Unsupported sendmail command flags [' . $command . ']. ' .
'Must be one of "-bs" or "-t" but can include additional flags.'
));
}
return $count;
}
/** Get the params to initialize the buffer */
protected function _getBufferParams()
{
return $this->_params;
}
}
|