3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Transport
;
14 use Zend\Mail\Address\AddressInterface
;
15 use Zend\Mail\Exception
;
16 use Zend\Mail\Header\HeaderInterface
;
19 * Class for sending email via the PHP internal mail() function
21 class Sendmail
implements TransportInterface
24 * Config options for sendmail parameters
28 protected $parameters;
31 * Callback to use when sending mail; typically, {@link mailHandler()}
46 protected $operatingSystem;
51 * @param null|string|array|Traversable $parameters OPTIONAL (Default: null)
53 public function __construct($parameters = null)
55 if ($parameters !== null) {
56 $this->setParameters($parameters);
58 $this->callable
= array($this, 'mailHandler');
62 * Set sendmail parameters
64 * Used to populate the additional_parameters argument to mail()
66 * @param null|string|array|Traversable $parameters
67 * @throws \Zend\Mail\Exception\InvalidArgumentException
70 public function setParameters($parameters)
72 if ($parameters === null ||
is_string($parameters)) {
73 $this->parameters
= $parameters;
77 if (!is_array($parameters) && !$parameters instanceof Traversable
) {
78 throw new Exception\
InvalidArgumentException(sprintf(
79 '%s expects a string, array, or Traversable object of parameters; received "%s"',
81 (is_object($parameters) ?
get_class($parameters) : gettype($parameters))
86 foreach ($parameters as $param) {
87 $string .= ' ' . $param;
91 $this->parameters
= $string;
96 * Set callback to use for mail
98 * Primarily for testing purposes, but could be used to curry arguments.
100 * @param callable $callable
101 * @throws \Zend\Mail\Exception\InvalidArgumentException
104 public function setCallable($callable)
106 if (!is_callable($callable)) {
107 throw new Exception\
InvalidArgumentException(sprintf(
108 '%s expects a callable argument; received "%s"',
110 (is_object($callable) ?
get_class($callable) : gettype($callable))
113 $this->callable
= $callable;
120 * @param \Zend\Mail\Message $message
122 public function send(Mail\Message
$message)
124 $to = $this->prepareRecipients($message);
125 $subject = $this->prepareSubject($message);
126 $body = $this->prepareBody($message);
127 $headers = $this->prepareHeaders($message);
128 $params = $this->prepareParameters($message);
130 // On *nix platforms, we need to replace \r\n with \n
131 // sendmail is not an SMTP server, it is a unix command - it expects LF
132 if (!$this->isWindowsOs()) {
133 $to = str_replace("\r\n", "\n", $to);
134 $subject = str_replace("\r\n", "\n", $subject);
135 $body = str_replace("\r\n", "\n", $body);
136 $headers = str_replace("\r\n", "\n", $headers);
139 call_user_func($this->callable
, $to, $subject, $body, $headers, $params);
143 * Prepare recipients list
145 * @param \Zend\Mail\Message $message
146 * @throws \Zend\Mail\Exception\RuntimeException
149 protected function prepareRecipients(Mail\Message
$message)
151 $headers = $message->getHeaders();
153 if (!$headers->has('to')) {
154 throw new Exception\
RuntimeException('Invalid email; contains no "To" header');
157 $to = $headers->get('to');
158 $list = $to->getAddressList();
159 if (0 == count($list)) {
160 throw new Exception\
RuntimeException('Invalid "To" header; contains no addresses');
163 // If not on Windows, return normal string
164 if (!$this->isWindowsOs()) {
165 return $to->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
168 // Otherwise, return list of emails
169 $addresses = array();
170 foreach ($list as $address) {
171 $addresses[] = $address->getEmail();
173 $addresses = implode(', ', $addresses);
178 * Prepare the subject line string
180 * @param \Zend\Mail\Message $message
183 protected function prepareSubject(Mail\Message
$message)
185 $headers = $message->getHeaders();
186 if (!$headers->has('subject')) {
189 $header = $headers->get('subject');
190 return $header->getFieldValue(HeaderInterface
::FORMAT_ENCODED
);
194 * Prepare the body string
196 * @param \Zend\Mail\Message $message
199 protected function prepareBody(Mail\Message
$message)
201 if (!$this->isWindowsOs()) {
202 // *nix platforms can simply return the body text
203 return $message->getBodyText();
206 // On windows, lines beginning with a full stop need to be fixed
207 $text = $message->getBodyText();
208 $text = str_replace("\n.", "\n..", $text);
213 * Prepare the textual representation of headers
215 * @param \Zend\Mail\Message $message
218 protected function prepareHeaders(Mail\Message
$message)
220 // On Windows, simply return verbatim
221 if ($this->isWindowsOs()) {
222 return $message->getHeaders()->toString();
225 // On *nix platforms, strip the "to" header
226 $headers = clone $message->getHeaders();
227 $headers->removeHeader('To');
228 $headers->removeHeader('Subject');
229 return $headers->toString();
233 * Prepare additional_parameters argument
235 * Basically, overrides the MAIL FROM envelope with either the Sender or
238 * @param \Zend\Mail\Message $message
241 protected function prepareParameters(Mail\Message
$message)
243 if ($this->isWindowsOs()) {
247 $parameters = (string) $this->parameters
;
249 $sender = $message->getSender();
250 if ($sender instanceof AddressInterface
) {
251 $parameters .= ' -f ' . $sender->getEmail();
255 $from = $message->getFrom();
258 $sender = $from->current();
259 $parameters .= ' -f ' . $sender->getEmail();
267 * Send mail using PHP native mail()
270 * @param string $subject
271 * @param string $message
272 * @param string $headers
274 * @throws \Zend\Mail\Exception\RuntimeException
276 public function mailHandler($to, $subject, $message, $headers, $parameters)
278 set_error_handler(array($this, 'handleMailErrors'));
279 if ($parameters === null) {
280 $result = mail($to, $subject, $message, $headers);
282 $result = mail($to, $subject, $message, $headers, $parameters);
284 restore_error_handler();
286 if ($this->errstr
!== null ||
!$result) {
287 $errstr = $this->errstr
;
288 if (empty($errstr)) {
289 $errstr = 'Unknown error';
291 throw new Exception\
RuntimeException('Unable to send mail: ' . $errstr);
296 * Temporary error handler for PHP native mail().
299 * @param string $errstr
300 * @param string $errfile
301 * @param string $errline
302 * @param array $errcontext
303 * @return bool always true
305 public function handleMailErrors($errno, $errstr, $errfile = null, $errline = null, array $errcontext = null)
307 $this->errstr
= $errstr;
312 * Is this a windows OS?
316 protected function isWindowsOs()
318 if (!$this->operatingSystem
) {
319 $this->operatingSystem
= strtoupper(substr(PHP_OS
, 0, 3));
321 return ($this->operatingSystem
== 'WIN');