Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mail / Transport / Sendmail.php
blobfbe8583fd0130ecb09d4c65680ccf17ce16e7a18
1 <?php
2 /**
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
8 */
10 namespace Zend\Mail\Transport;
12 use Traversable;
13 use Zend\Mail;
14 use Zend\Mail\Address\AddressInterface;
15 use Zend\Mail\Exception;
16 use Zend\Mail\Header\HeaderInterface;
18 /**
19 * Class for sending email via the PHP internal mail() function
21 class Sendmail implements TransportInterface
23 /**
24 * Config options for sendmail parameters
26 * @var string
28 protected $parameters;
30 /**
31 * Callback to use when sending mail; typically, {@link mailHandler()}
33 * @var callable
35 protected $callable;
37 /**
38 * error information
39 * @var string
41 protected $errstr;
43 /**
44 * @var string
46 protected $operatingSystem;
48 /**
49 * Constructor.
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');
61 /**
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
68 * @return Sendmail
70 public function setParameters($parameters)
72 if ($parameters === null || is_string($parameters)) {
73 $this->parameters = $parameters;
74 return $this;
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"',
80 __METHOD__,
81 (is_object($parameters) ? get_class($parameters) : gettype($parameters))
82 ));
85 $string = '';
86 foreach ($parameters as $param) {
87 $string .= ' ' . $param;
89 trim($string);
91 $this->parameters = $string;
92 return $this;
95 /**
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
102 * @return Sendmail
104 public function setCallable($callable)
106 if (!is_callable($callable)) {
107 throw new Exception\InvalidArgumentException(sprintf(
108 '%s expects a callable argument; received "%s"',
109 __METHOD__,
110 (is_object($callable) ? get_class($callable) : gettype($callable))
113 $this->callable = $callable;
114 return $this;
118 * Send a message
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
147 * @return string
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);
174 return $addresses;
178 * Prepare the subject line string
180 * @param \Zend\Mail\Message $message
181 * @return string
183 protected function prepareSubject(Mail\Message $message)
185 $headers = $message->getHeaders();
186 if (!$headers->has('subject')) {
187 return null;
189 $header = $headers->get('subject');
190 return $header->getFieldValue(HeaderInterface::FORMAT_ENCODED);
194 * Prepare the body string
196 * @param \Zend\Mail\Message $message
197 * @return string
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);
209 return $text;
213 * Prepare the textual representation of headers
215 * @param \Zend\Mail\Message $message
216 * @return string
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
236 * From address.
238 * @param \Zend\Mail\Message $message
239 * @return string
241 protected function prepareParameters(Mail\Message $message)
243 if ($this->isWindowsOs()) {
244 return null;
247 $parameters = (string) $this->parameters;
249 $sender = $message->getSender();
250 if ($sender instanceof AddressInterface) {
251 $parameters .= ' -f ' . $sender->getEmail();
252 return $parameters;
255 $from = $message->getFrom();
256 if (count($from)) {
257 $from->rewind();
258 $sender = $from->current();
259 $parameters .= ' -f ' . $sender->getEmail();
260 return $parameters;
263 return $parameters;
267 * Send mail using PHP native mail()
269 * @param string $to
270 * @param string $subject
271 * @param string $message
272 * @param string $headers
273 * @param $parameters
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);
281 } else {
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().
298 * @param int $errno
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;
308 return true;
312 * Is this a windows OS?
314 * @return bool
316 protected function isWindowsOs()
318 if (!$this->operatingSystem) {
319 $this->operatingSystem = strtoupper(substr(PHP_OS, 0, 3));
321 return ($this->operatingSystem == 'WIN');