4
* This file is part of SwiftMailer.
5
* (c) 2004-2009 Chris Corbyn
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
11
//@require 'Swift/Transport.php';
12
//@require 'Swift/Transport/MailInvoker.php';
13
//@require 'Swift/Mime/Message.php';
14
//@require 'Swift/Events/EventListener.php';
17
* Sends Messages using the mail() function.
19
* It is advised that users do not use this transport if at all possible
20
* since a number of plugin features cannot be used in conjunction with this
21
* transport due to the internal interface in PHP itself.
23
* The level of error reporting with this transport is incredibly weak, again
24
* due to limitations of PHP's internal mail() function. You'll get an
25
* all-or-nothing result from sending.
28
* @subpackage Transport
29
* @author Chris Corbyn
31
class Swift_Transport_MailTransport implements Swift_Transport
34
/** Addtional parameters to pass to mail() */
35
private $_extraParams = '-f%s';
37
/** The event dispatcher from the plugin API */
38
private $_eventDispatcher;
40
/** An invoker that calls the mail() function */
44
* Create a new MailTransport with the $log.
45
* @param Swift_Transport_Log $log
47
public function __construct(Swift_Transport_MailInvoker $invoker,
48
Swift_Events_EventDispatcher $eventDispatcher)
50
$this->_invoker = $invoker;
51
$this->_eventDispatcher = $eventDispatcher;
57
public function isStarted()
65
public function start()
72
public function stop()
77
* Set the additional parameters used on the mail() function.
79
* This string is formatted for sprintf() where %s is the sender address.
81
* @param string $params
83
public function setExtraParams($params)
85
$this->_extraParams = $params;
90
* Get the additional parameters used on the mail() function.
92
* This string is formatted for sprintf() where %s is the sender address.
96
public function getExtraParams()
98
return $this->_extraParams;
102
* Send the given Message.
104
* Recipient/sender data will be retreived from the Message API.
105
* The return value is the number of recipients who were accepted for delivery.
107
* @param Swift_Mime_Message $message
108
* @param string[] &$failedRecipients to collect failures by-reference
111
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
113
$failedRecipients = (array) $failedRecipients;
115
if ($evt = $this->_eventDispatcher->createSendEvent($this, $message))
117
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
118
if ($evt->bubbleCancelled())
125
count((array) $message->getTo())
126
+ count((array) $message->getCc())
127
+ count((array) $message->getBcc())
130
$toHeader = $message->getHeaders()->get('To');
131
$subjectHeader = $message->getHeaders()->get('Subject');
133
$to = $toHeader->getFieldBody();
134
$subject = $subjectHeader->getFieldBody();
136
$reversePath = $this->_getReversePath($message);
138
//Remove headers that would otherwise be duplicated
139
$message->getHeaders()->remove('To');
140
$message->getHeaders()->remove('Subject');
142
$messageStr = $message->toString();
144
$message->getHeaders()->set($toHeader);
145
$message->getHeaders()->set($subjectHeader);
147
//Separate headers from body
148
if (false !== $endHeaders = strpos($messageStr, "\r\n\r\n"))
150
$headers = substr($messageStr, 0, $endHeaders) . "\r\n"; //Keep last EOL
151
$body = substr($messageStr, $endHeaders + 4);
155
$headers = $messageStr . "\r\n";
161
if ("\r\n" != PHP_EOL) //Non-windows (not using SMTP)
163
$headers = str_replace("\r\n", PHP_EOL, $headers);
164
$body = str_replace("\r\n", PHP_EOL, $body);
166
else //Windows, using SMTP
168
$headers = str_replace("\r\n.", "\r\n..", $headers);
169
$body = str_replace("\r\n.", "\r\n..", $body);
172
if ($this->_invoker->mail($to, $subject, $body, $headers,
173
sprintf($this->_extraParams, $reversePath)))
177
$evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
178
$evt->setFailedRecipients($failedRecipients);
179
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
184
$failedRecipients = array_merge(
186
array_keys((array) $message->getTo()),
187
array_keys((array) $message->getCc()),
188
array_keys((array) $message->getBcc())
193
$evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
194
$evt->setFailedRecipients($failedRecipients);
195
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
198
$message->generateId();
209
* @param Swift_Events_EventListener $plugin
211
public function registerPlugin(Swift_Events_EventListener $plugin)
213
$this->_eventDispatcher->bindEventListener($plugin);
216
// -- Private methods
218
/** Determine the best-use reverse path for this message */
219
private function _getReversePath(Swift_Mime_Message $message)
221
$return = $message->getReturnPath();
222
$sender = $message->getSender();
223
$from = $message->getFrom();
229
elseif (!empty($sender))
231
$keys = array_keys($sender);
232
$path = array_shift($keys);
234
elseif (!empty($from))
236
$keys = array_keys($from);
237
$path = array_shift($keys);