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