first commit

This commit is contained in:
2026-04-30 14:38:11 +02:00
commit e22bbde336
1994 changed files with 613950 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
<?php
/**
* Swift Mailer Multiple Redundant Connection component.
* Please read the LICENSE file
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Connection
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_ConnectionBase");
/**
* Swift Multi Connection
* Tries to connect to a number of connections until one works successfully
* @package Swift_Connection
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Connection_Multi extends Swift_ConnectionBase
{
/**
* The list of available connections
* @var array
*/
protected $connections = array();
/**
* The id of the active connection
* @var string
*/
protected $active = null;
/**
* Constructor
*/
public function __construct($connections=array())
{
foreach ($connections as $id => $conn)
{
$this->addConnection($connections[$id], $id);
}
}
/**
* Add a connection to the list of options
* @param Swift_Connection An instance of the connection
* @param string An ID to assign to the connection
*/
public function addConnection(Swift_Connection $connection, $id=null)
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Adding new connection of type '" . get_class($connection) . "' to the multi-redundant connection.");
}
if ($id !== null) $this->connections[$id] = $connection;
else $this->connections[] = $connection;
}
/**
* Read a full response from the buffer
* @return string
* @throws Swift_ConnectionException Upon failure to read
*/
public function read()
{
if ($this->active === null)
{
throw new Swift_ConnectionException("None of the connections set have been started");
}
return $this->connections[$this->active]->read();
}
/**
* Write a command to the server (leave off trailing CRLF)
* @param string The command to send
* @throws Swift_ConnectionException Upon failure to write
*/
public function write($command, $end="\r\n")
{
if ($this->active === null)
{
throw new Swift_ConnectionException("None of the connections set have been started");
}
return $this->connections[$this->active]->write($command, $end);
}
/**
* Try to start the connection
* @throws Swift_ConnectionException Upon failure to start
*/
public function start()
{
$log = Swift_LogContainer::getLog();
$fail_messages = array();
foreach ($this->connections as $id => $conn)
{
try {
$this->connections[$id]->start();
if ($this->connections[$id]->isAlive())
{
$this->active = $id;
return true;
}
else
{
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Connection (" . $id . ") failed. Will try next connection if available.");
}
throw new Swift_ConnectionException("The connection started but reported that it was not active");
}
} catch (Swift_ConnectionException $e) {
$fail_messages[] = $id . ": " . $e->getMessage();
}
}
$failure = implode("<br />", $fail_messages);
throw new Swift_ConnectionException($failure);
}
/**
* Try to close the connection
* @throws Swift_ConnectionException Upon failure to close
*/
public function stop()
{
if ($this->active !== null) $this->connections[$this->active]->stop();
$this->active = null;
}
/**
* Check if the current connection is alive
* @return boolean
*/
public function isAlive()
{
return (($this->active !== null) && $this->connections[$this->active]->isAlive());
}
/**
* Call the current connection's postConnect() method
*/
public function postConnect(Swift $instance)
{
$this->connections[$this->active]->postConnect($instance);
}
/**
* Call the current connection's setExtension() method
*/
public function setExtension($extension, $attributes=array())
{
$this->connections[$this->active]->setExtension($extension, $attributes);
}
/**
* Call the current connection's hasExtension() method
*/
public function hasExtension($name)
{
return $this->connections[$this->active]->hasExtension($name);
}
/**
* Call the current connection's getAttributes() method
*/
public function getAttributes($name)
{
return $this->connections[$this->active]->getAttributes($name);
}
}

View File

@@ -0,0 +1,136 @@
<?php
/**
* Swift Mailer mail() connection component
* Please read the LICENSE file
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Connection
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_ConnectionBase");
Swift_ClassLoader::load("Swift_Plugin_MailSend");
/**
* Swift mail() Connection
* NOTE: This class is nothing more than a stub. The MailSend plugin does the actual sending.
* @package Swift_Connection
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Connection_NativeMail extends Swift_ConnectionBase
{
/**
* The response the stub will be giving next
* @var string Response
*/
protected $response = "220 Stubbed";
/**
* The 5th parameter in mail() is a sprintf() formatted string.
* @var string
*/
protected $pluginParams;
/**
* An instance of the MailSend plugin.
* @var Swift_Plugin_MailSend
*/
protected $plugin = null;
/**
* Ctor.
* @param string The 5th parameter in mail() as a sprintf() formatted string where %s is the sender address. This only comes into effect if safe_mode is OFF.
*/
public function __construct($additional_params="-oi -f %s")
{
$this->setAdditionalMailParams($additional_params);
}
/**
* Sets the MailSend plugin in Swift once Swift has connected
* @param Swift The current instance of Swift
*/
public function postConnect(Swift $instance)
{
$this->plugin = new Swift_Plugin_MailSend($this->getAdditionalMailParams());
$instance->attachPlugin($this->plugin, "_MAIL_SEND");
}
/**
* Set the 5th parameter in mail() as a sprintf() formatted string. Only used if safe_mode is off.
* @param string
*/
public function setAdditionalMailParams($params)
{
$this->pluginParams = $params;
if ($this->plugin !== null)
{
$this->plugin->setAdditionalParams($params);
}
}
/**
* Get the 5th parameter in mail() as a sprintf() formatted string.
* @return string
*/
public function getAdditionalMailParams()
{
return $this->pluginParams;
}
/**
* Read a full response from the buffer (this is spoofed if running in -t mode)
* @return string
* @throws Swift_ConnectionException Upon failure to read
*/
public function read()
{
return $this->response;
}
/**
* Set the response this stub will return
* @param string The response to send
*/
public function setResponse($int)
{
$this->response = $int . " Stubbed";
}
/**
* Write a command to the process (leave off trailing CRLF)
* @param string The command to send
* @throws Swift_ConnectionException Upon failure to write
*/
public function write($command, $end="\r\n")
{
$command = strtoupper($command);
if (strpos($command, " ")) $command = substr($command, 0, strpos($command, " "));
switch ($command)
{
case "DATA":
$this->setResponse(354);
break;
case "EHLO": case "MAIL": case "RCPT": case "QUIT": case "RSET": default:
$this->setResponse(250);
break;
}
}
/**
* Try to start the connection
* @throws Swift_ConnectionException Upon failure to start
*/
public function start()
{
$this->response = "220 Stubbed";
}
/**
* Try to close the connection
* @throws Swift_ConnectionException Upon failure to close
*/
public function stop()
{
$this->response = "220 Stubbed";
}
/**
* Check if the process is still alive
* @return boolean
*/
public function isAlive()
{
return function_exists("mail");
}
}

View File

@@ -0,0 +1,194 @@
<?php
/**
* Swift Mailer Multiple Redundant Cycling Connection component.
* Please read the LICENSE file
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Connection
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_ConnectionBase");
/**
* Swift Rotator Connection
* Switches through each connection in turn after sending each message
* @package Swift_Connection
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Connection_Rotator extends Swift_ConnectionBase
{
/**
* The list of available connections
* @var array
*/
protected $connections = array();
/**
* The id of the active connection
* @var int
*/
protected $active = null;
/**
* Contains a list of any connections which were tried but found to be dead
* @var array
*/
protected $dead = array();
/**
* Constructor
*/
public function __construct($connections=array())
{
foreach ($connections as $id => $conn)
{
$this->addConnection($connections[$id], $id);
}
}
/**
* Add a connection to the list of options
* @param Swift_Connection An instance of the connection
*/
public function addConnection(Swift_Connection $connection)
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Adding new connection of type '" . get_class($connection) . "' to rotator.");
}
$this->connections[] = $connection;
}
/**
* Rotate to the next working connection
* @throws Swift_ConnectionException If no connections are available
*/
public function nextConnection()
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add(" <==> Rotating connection.");
}
$total = count($this->connections);
$start = $this->active === null ? 0 : ($this->active + 1);
if ($start >= $total) $start = 0;
$fail_messages = array();
for ($id = $start; $id < $total; $id++)
{
if (in_array($id, $this->dead)) continue; //The connection was previously found to be useless
try {
if (!$this->connections[$id]->isAlive()) $this->connections[$id]->start();
if ($this->connections[$id]->isAlive())
{
$this->active = $id;
return true;
}
else
{
$this->dead[] = $id;
$this->connections[$id]->stop();
throw new Swift_ConnectionException("The connection started but reported that it was not active");
}
} catch (Swift_ConnectionException $e) {
$fail_messages[] = $id . ": " . $e->getMessage();
}
}
$failure = implode("<br />", $fail_messages);
throw new Swift_ConnectionException("No connections were started.<br />" . $failure);
}
/**
* Read a full response from the buffer
* @return string
* @throws Swift_ConnectionException Upon failure to read
*/
public function read()
{
if ($this->active === null)
{
throw new Swift_ConnectionException("None of the connections set have been started");
}
return $this->connections[$this->active]->read();
}
/**
* Write a command to the server (leave off trailing CRLF)
* @param string The command to send
* @throws Swift_ConnectionException Upon failure to write
*/
public function write($command, $end="\r\n")
{
if ($this->active === null)
{
throw new Swift_ConnectionException("None of the connections set have been started");
}
return $this->connections[$this->active]->write($command, $end);
}
/**
* Try to start the connection
* @throws Swift_ConnectionException Upon failure to start
*/
public function start()
{
if ($this->active === null) $this->nextConnection();
}
/**
* Try to close the connection
* @throws Swift_ConnectionException Upon failure to close
*/
public function stop()
{
foreach ($this->connections as $id => $conn)
{
if ($this->connections[$id]->isAlive()) $this->connections[$id]->stop();
}
$this->active = null;
}
/**
* Check if the current connection is alive
* @return boolean
*/
public function isAlive()
{
return (($this->active !== null) && $this->connections[$this->active]->isAlive());
}
/**
* Get the ID of the active connection
* @return int
*/
public function getActive()
{
return $this->active;
}
/**
* Call the current connection's postConnect() method
*/
public function postConnect(Swift $instance)
{
Swift_ClassLoader::load("Swift_Plugin_ConnectionRotator");
if (!$instance->getPlugin("_ROTATOR")) $instance->attachPlugin(new Swift_Plugin_ConnectionRotator(), "_ROTATOR");
$this->connections[$this->active]->postConnect($instance);
}
/**
* Call the current connection's setExtension() method
*/
public function setExtension($extension, $attributes=array())
{
$this->connections[$this->active]->setExtension($extension, $attributes);
}
/**
* Call the current connection's hasExtension() method
*/
public function hasExtension($name)
{
return $this->connections[$this->active]->hasExtension($name);
}
/**
* Call the current connection's getAttributes() method
*/
public function getAttributes($name)
{
return $this->connections[$this->active]->getAttributes($name);
}
}

View File

@@ -0,0 +1,452 @@
<?php
/**
* Swift Mailer SMTP Connection component.
* Please read the LICENSE file
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Connection
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_ConnectionBase");
Swift_ClassLoader::load("Swift_Authenticator");
/**
* Swift SMTP Connection
* @package Swift_Connection
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Connection_SMTP extends Swift_ConnectionBase
{
/**
* Constant for TLS connections
*/
const ENC_TLS = 2;
/**
* Constant for SSL connections
*/
const ENC_SSL = 4;
/**
* Constant for unencrypted connections
*/
const ENC_OFF = 8;
/**
* Constant for the default SMTP port
*/
const PORT_DEFAULT = 25;
/**
* Constant for the default secure SMTP port
*/
const PORT_SECURE = 465;
/**
* Constant for auto-detection of paramters
*/
const AUTO_DETECT = -2;
/**
* A connection handle
* @var resource
*/
protected $handle = null;
/**
* The remote port number
* @var int
*/
protected $port = null;
/**
* Encryption type to use
* @var int
*/
protected $encryption = null;
/**
* A connection timeout
* @var int
*/
protected $timeout = 15;
/**
* A username to authenticate with
* @var string
*/
protected $username = false;
/**
* A password to authenticate with
* @var string
*/
protected $password = false;
/**
* Loaded authentication mechanisms
* @var array
*/
protected $authenticators = array();
/**
* Fsockopen() error codes.
* @var int
*/
protected $errno;
/**
* Fsockopen() error codes.
* @var string
*/
protected $errstr;
/**
* Constructor
* @param string The remote server to connect to
* @param int The remote port to connect to
* @param int The encryption level to use
*/
public function __construct($server="localhost", $port=null, $encryption=null)
{
$this->setServer($server);
$this->setEncryption($encryption);
$this->setPort($port);
}
/**
* Set the timeout to connect in seconds
* @param int Timeout to use
*/
public function setTimeout($time)
{
$this->timeout = (int) $time;
}
/**
* Get the timeout currently set for connecting
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Set the remote server to connect to as a FQDN
* @param string Server name
*/
public function setServer($server)
{
if ($server == self::AUTO_DETECT)
{
$server = @ini_get("SMTP");
if (!$server) $server = "localhost";
}
$this->server = (string) $server;
}
/**
* Get the remote server name
* @return string
*/
public function getServer()
{
return $this->server;
}
/**
* Set the remote port number to connect to
* @param int Port number
*/
public function setPort($port)
{
if ($port == self::AUTO_DETECT)
{
$port = @ini_get("SMTP_PORT");
}
if (!$port) $port = ($this->getEncryption() == self::ENC_OFF) ? self::PORT_DEFAULT : self::PORT_SECURE;
$this->port = (int) $port;
}
/**
* Get the remote port number currently used to connect
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* Provide a username for authentication
* @param string The username
*/
public function setUsername($user)
{
$this->setRequiresEHLO(true);
$this->username = $user;
}
/**
* Get the username for authentication
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set the password for SMTP authentication
* @param string Password to use
*/
public function setPassword($pass)
{
$this->setRequiresEHLO(true);
$this->password = $pass;
}
/**
* Get the password for authentication
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Add an authentication mechanism to authenticate with
* @param Swift_Authenticator
*/
public function attachAuthenticator(Swift_Authenticator $auth)
{
$this->authenticators[$auth->getAuthExtensionName()] = $auth;
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Authentication mechanism '" . $auth->getAuthExtensionName() . "' attached.");
}
}
/**
* Set the encryption level to use on the connection
* See the constants ENC_TLS, ENC_SSL and ENC_OFF
* NOTE: PHP needs to have been compiled with OpenSSL for SSL and TLS to work
* NOTE: Some PHP installations will not have the TLS stream wrapper
* @param int Level of encryption
*/
public function setEncryption($enc)
{
if (!$enc) $enc = self::ENC_OFF;
$this->encryption = (int) $enc;
}
/**
* Get the current encryption level used
* This method returns an integer corresponding to one of the constants ENC_TLS, ENC_SSL or ENC_OFF
* @return int
*/
public function getEncryption()
{
return $this->encryption;
}
/**
* Read a full response from the buffer
* inner !feof() patch provided by Christian Rodriguez:
* <a href="http://www.flyspray.org/">www.flyspray.org</a>
* @return string
* @throws Swift_ConnectionException Upon failure to read
*/
public function read()
{
if (!$this->handle) throw new Swift_ConnectionException(
"The SMTP connection is not alive and cannot be read from." . $this->smtpErrors());
$ret = "";
$line = 0;
while (!feof($this->handle))
{
$line++;
stream_set_timeout($this->handle, $this->timeout);
$tmp = @fgets($this->handle);
if ($tmp === false && !feof($this->handle))
{
throw new Swift_ConnectionException(
"There was a problem reading line " . $line . " of an SMTP response. The response so far was:<br />[" . $ret .
"]. It appears the connection has died without saying goodbye to us! Too many emails in one go perhaps?" .
$this->smtpErrors());
}
$ret .= trim($tmp) . "\r\n";
if ($tmp{3} == " ") break;
}
return $ret = substr($ret, 0, -2);
}
/**
* Write a command to the server (leave off trailing CRLF)
* @param string The command to send
* @throws Swift_ConnectionException Upon failure to write
*/
public function write($command, $end="\r\n")
{
if (!$this->handle) throw new Swift_ConnectionException(
"The SMTP connection is not alive and cannot be written to." .
$this->smtpErrors());
if (!@fwrite($this->handle, $command . $end) && !empty($command)) throw new Swift_ConnectionException("The SMTP connection did not allow the command '" . $command . "' to be sent." . $this->smtpErrors());
}
/**
* Try to start the connection
* @throws Swift_ConnectionException Upon failure to start
*/
public function start()
{
if ($this->port === null)
{
switch ($this->encryption)
{
case self::ENC_TLS: case self::ENC_SSL:
$this->port = 465;
break;
case null: default:
$this->port = 25;
break;
}
}
$server = $this->server;
if ($this->encryption == self::ENC_TLS) $server = "tls://" . $server;
elseif ($this->encryption == self::ENC_SSL) $server = "ssl://" . $server;
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_log::LOG_EVERYTHING))
{
$log->add("Trying to connect to SMTP server at '" . $server . ":" . $this->port);
}
if (!$this->handle = @fsockopen($server, $this->port, $errno, $errstr, $this->timeout))
{
$error_msg = "The SMTP connection failed to start [" . $server . ":" . $this->port . "]: fsockopen returned Error Number " . $errno . " and Error String '" . $errstr . "'";
if ($log->isEnabled())
{
$log->add($error_msg, Swift_Log::ERROR);
}
$this->handle = null;
throw new Swift_ConnectionException($error_msg);
}
$this->errno =& $errno;
$this->errstr =& $errstr;
}
/**
* Get the smtp error string as recorded by fsockopen()
* @return string
*/
public function smtpErrors()
{
return " (fsockopen: " . $this->errstr . "#" . $this->errno . ") ";
}
/**
* Authenticate if required to do so
* @param Swift An instance of Swift
* @throws Swift_ConnectionException If authentication fails
*/
public function postConnect(Swift $instance)
{
if ($this->getUsername() && $this->getPassword())
{
$this->runAuthenticators($this->getUsername(), $this->getPassword(), $instance);
}
}
/**
* Run each authenticator in turn an try for a successful login
* If none works, throw an exception
* @param string Username
* @param string Password
* @param Swift An instance of swift
* @throws Swift_ConnectionException Upon failure to authenticate
*/
public function runAuthenticators($user, $pass, Swift $swift)
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Trying to authenticate with username '" . $user . "'.");
}
//Load in defaults
if (empty($this->authenticators))
{
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("No authenticators loaded; looking for defaults.");
}
$dir = dirname(__FILE__) . "/../Authenticator";
$handle = opendir($dir);
while (false !== $file = readdir($handle))
{
if (preg_match("/^[A-Za-z0-9-]+\\.php\$/", $file))
{
$name = preg_replace('/[^a-zA-Z0-9]+/', '', substr($file, 0, -4));
require_once $dir . "/" . $file;
$class = "Swift_Authenticator_" . $name;
$this->attachAuthenticator(new $class());
}
}
closedir($handle);
}
$tried = 0;
$looks_supported = true;
//Allow everything we have if the server has the audacity not to help us out.
if (!$this->hasExtension("AUTH"))
{
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Server (perhaps wrongly) is not advertising AUTH... manually overriding.");
}
$looks_supported = false;
$this->setExtension("AUTH", array_keys($this->authenticators));
}
foreach ($this->authenticators as $name => $obj)
{
//Server supports this authentication mechanism
if (in_array($name, $this->getAttributes("AUTH")) || $name{0} == "*")
{
$tried++;
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Trying '" . $name . "' authentication...");
}
if ($this->authenticators[$name]->isAuthenticated($user, $pass, $swift))
{
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Success! Authentication accepted.");
}
return true;
}
}
}
//Server doesn't support authentication
if (!$looks_supported && $tried == 0)
throw new Swift_ConnectionException("Authentication is not supported by the server but a username and password was given.");
if ($tried == 0)
throw new Swift_ConnectionException("No authentication mechanisms were tried since the server did not support any of the ones loaded. " .
"Loaded authenticators: [" . implode(", ", array_keys($this->authenticators)) . "]");
else
throw new Swift_ConnectionException("Authentication failed using username '" . $user . "' and password '". str_repeat("*", strlen($pass)) . "'");
}
/**
* Try to close the connection
* @throws Swift_ConnectionException Upon failure to close
*/
public function stop()
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Closing down SMTP connection.");
}
if ($this->handle)
{
if (!fclose($this->handle))
{
throw new Swift_ConnectionException("The SMTP connection could not be closed for an unknown reason." . $this->smtpErrors());
}
$this->handle = null;
}
}
/**
* Check if the SMTP connection is alive
* @return boolean
*/
public function isAlive()
{
return ($this->handle !== null);
}
/**
* Destructor.
* Cleans up any open connections.
*/
public function __destruct()
{
$this->stop();
}
}

View File

@@ -0,0 +1,352 @@
<?php
/**
* Swift Mailer Sendmail Connection component.
* Please read the LICENSE file
* @author Chris Corbyn <chris@w3style.co.uk>
* @package Swift_Connection
* @license GNU Lesser General Public License
*/
require_once dirname(__FILE__) . "/../ClassLoader.php";
Swift_ClassLoader::load("Swift_ConnectionBase");
/**
* Swift Sendmail Connection
* @package Swift_Connection
* @author Chris Corbyn <chris@w3style.co.uk>
*/
class Swift_Connection_Sendmail extends Swift_ConnectionBase
{
/**
* Constant for auto-detection of paths
*/
const AUTO_DETECT = -2;
/**
* Flags for the MTA (options such as bs or t)
* @var string
*/
protected $flags = null;
/**
* The full path to the MTA
* @var string
*/
protected $path = null;
/**
* The type of last request sent
* For example MAIL, RCPT, DATA
* @var string
*/
protected $request = null;
/**
* The process handle
* @var resource
*/
protected $proc;
/**
* I/O pipes for the process
* @var array
*/
protected $pipes;
/**
* Switches to true for just one command when DATA has been issued
* @var boolean
*/
protected $send = false;
/**
* The timeout in seconds before giving up
* @var int Seconds
*/
protected $timeout = 10;
/**
* Constructor
* @param string The command to execute
* @param int The timeout in seconds before giving up
*/
public function __construct($command="/usr/sbin/sendmail -bs", $timeout=10)
{
$this->setCommand($command);
$this->setTimeout($timeout);
}
/**
* Set the timeout on the process
* @param int The number of seconds
*/
public function setTimeout($secs)
{
$this->timeout = (int)$secs;
}
/**
* Get the timeout on the process
* @return int
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Set the operating flags for the MTA
* @param string
*/
public function setFlags($flags)
{
$this->flags = $flags;
}
/**
* Get the operating flags for the MTA
* @return string
*/
public function getFlags()
{
return $this->flags;
}
/**
* Set the path to the binary
* @param string The path (must be absolute!)
*/
public function setPath($path)
{
if ($path == self::AUTO_DETECT) $path = $this->findSendmail();
$this->path = $path;
}
/**
* Get the path to the binary
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* For auto-detection of sendmail path
* Thanks to "Joe Cotroneo" for providing the enhancement
* @return string
*/
public function findSendmail()
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Sendmail path auto-detection in progress. Trying `which sendmail`");
}
$path = @trim(shell_exec('which sendmail'));
if (!is_executable($path))
{
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("No luck so far, trying some common paths...");
}
$common_locations = array(
'/usr/bin/sendmail',
'/usr/lib/sendmail',
'/var/qmail/bin/sendmail',
'/bin/sendmail',
'/usr/sbin/sendmail',
'/sbin/sendmail'
);
foreach ($common_locations as $path)
{
if (is_executable($path)) return $path;
}
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Falling back to /usr/sbin/sendmail (but it doesn't look good)!");
}
//Fallback (swift will still throw an error)
return "/usr/sbin/sendmail";
}
else return $path;
}
/**
* Set the sendmail command (path + flags)
* @param string Command
* @throws Swift_ConnectionException If the command is not correctly structured
*/
public function setCommand($command)
{
if ($command == self::AUTO_DETECT) $command = $this->findSendmail() . " -bs";
if (!strrpos($command, " -"))
{
throw new Swift_ConnectionException("Cannot set sendmail command with no command line flags. e.g. /usr/sbin/sendmail -t");
}
$path = substr($command, 0, strrpos($command, " -"));
$flags = substr($command, strrpos($command, " -")+2);
$this->setPath($path);
$this->setFlags($flags);
}
/**
* Get the sendmail command (path + flags)
* @return string
*/
public function getCommand()
{
return $this->getPath() . " -" . $this->getFlags();
}
/**
* Write a command to the open pipe
* @param string The command to write
* @throws Swift_ConnectionException If the pipe cannot be written to
*/
protected function pipeIn($command, $end="\r\n")
{
if (!$this->isAlive()) throw new Swift_ConnectionException("The sendmail process is not alive and cannot be written to.");
if (!@fwrite($this->pipes[0], $command . $end) && !empty($command)) throw new Swift_ConnectionException("The sendmail process did not allow the command '" . $command . "' to be sent.");
fflush($this->pipes[0]);
}
/**
* Read data from the open pipe
* @return string
* @throws Swift_ConnectionException If the pipe is not operating as expected
*/
protected function pipeOut()
{
if (strpos($this->getFlags(), "t") !== false) return;
if (!$this->isAlive()) throw new Swift_ConnectionException("The sendmail process is not alive and cannot be read from.");
$ret = "";
$line = 0;
while (true)
{
$line++;
stream_set_timeout($this->pipes[1], $this->timeout);
$tmp = @fgets($this->pipes[1]);
if ($tmp === false)
{
throw new Swift_ConnectionException("There was a problem reading line " . $line . " of a sendmail SMTP response. The response so far was:<br />[" . $ret . "]. It appears the process has died.");
}
$ret .= trim($tmp) . "\r\n";
if ($tmp{3} == " ") break;
}
fflush($this->pipes[1]);
return $ret = substr($ret, 0, -2);
}
/**
* Read a full response from the buffer (this is spoofed if running in -t mode)
* @return string
* @throws Swift_ConnectionException Upon failure to read
*/
public function read()
{
if (strpos($this->getFlags(), "t") !== false)
{
switch (strtolower($this->request))
{
case null:
return "220 Greetings";
case "helo": case "ehlo":
return "250 hello";
case "mail": case "rcpt": case "rset":
return "250 ok";
case "quit":
return "221 bye";
case "data":
$this->send = true;
return "354 go ahead";
default:
return "250 ok";
}
}
else return $this->pipeOut();
}
/**
* Write a command to the process (leave off trailing CRLF)
* @param string The command to send
* @throws Swift_ConnectionException Upon failure to write
*/
public function write($command, $end="\r\n")
{
if (strpos($this->getFlags(), "t") !== false)
{
if (!$this->send && strpos($command, " ")) $command = substr($command, strpos($command, " ")+1);
elseif ($this->send)
{
$this->pipeIn($command);
}
$this->request = $command;
$this->send = (strtolower($command) == "data");
}
else $this->pipeIn($command, $end);
}
/**
* Try to start the connection
* @throws Swift_ConnectionException Upon failure to start
*/
public function start()
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Trying to start a sendmail process.");
}
if (!$this->getPath() || !$this->getFlags())
{
throw new Swift_ConnectionException("Sendmail cannot be started without a path to the binary including flags.");
}
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Trying to stat the executable '" . $this->getPath() . "'.");
}
if (!@lstat($this->getPath()))
{
throw new Swift_ConnectionException(
"Sendmail cannot be seen with lstat(). The command given [" . $this->getCommand() . "] does not appear to be valid.");
}
$pipes_spec = array(
array("pipe", "r"),
array("pipe", "w"),
array("pipe", "w")
);
$this->proc = proc_open($this->getCommand(), $pipes_spec, $this->pipes);
if (!$this->isAlive())
{
throw new Swift_ConnectionException(
"The sendmail process failed to start. Please verify that the path exists and PHP has permission to execute it.");
}
}
/**
* Try to close the connection
*/
public function stop()
{
$log = Swift_LogContainer::getLog();
if ($log->hasLevel(Swift_Log::LOG_EVERYTHING))
{
$log->add("Terminating sendmail process.");
}
foreach ((array)$this->pipes as $pipe)
{
@fclose($pipe);
}
if ($this->proc)
{
proc_close($this->proc);
$this->pipes = null;
$this->proc = null;
}
}
/**
* Check if the process is still alive
* @return boolean
*/
public function isAlive()
{
return ($this->proc !== false
&& is_resource($this->proc)
&& is_resource($this->pipes[0])
&& is_resource($this->pipes[1])
&& $this->proc !== null);
}
/**
* Destructor.
* Cleans up by stopping any running processes.
*/
public function __destruct()
{
$this->stop();
}
}