/usr/share/php/Nette/Mail/SmtpMailer.php is in php-nette 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\Mail;
use Nette;
/**
* Sends emails via the SMTP server.
*
* @author David Grudl
*/
class SmtpMailer extends Nette\Object implements IMailer
{
/** @var resource */
private $connection;
/** @var string */
private $host;
/** @var int */
private $port;
/** @var string */
private $username;
/** @var string */
private $password;
/** @var string ssl | tls | (empty) */
private $secure;
/** @var int */
private $timeout;
/** @var bool */
private $persistent;
public function __construct(array $options = array())
{
if (isset($options['host'])) {
$this->host = $options['host'];
$this->port = isset($options['port']) ? (int) $options['port'] : NULL;
} else {
$this->host = ini_get('SMTP');
$this->port = (int) ini_get('smtp_port');
}
$this->username = isset($options['username']) ? $options['username'] : '';
$this->password = isset($options['password']) ? $options['password'] : '';
$this->secure = isset($options['secure']) ? $options['secure'] : '';
$this->timeout = isset($options['timeout']) ? (int) $options['timeout'] : 20;
if (!$this->port) {
$this->port = $this->secure === 'ssl' ? 465 : 25;
}
$this->persistent = !empty($options['persistent']);
}
/**
* Sends email.
* @return void
*/
public function send(Message $mail)
{
$mail = clone $mail;
try {
if (!$this->connection) {
$this->connect();
}
if (($from = $mail->getHeader('Return-Path'))
|| ($from = key($mail->getHeader('From')))
) {
$this->write("MAIL FROM:<$from>", 250);
}
foreach (array_merge(
(array) $mail->getHeader('To'),
(array) $mail->getHeader('Cc'),
(array) $mail->getHeader('Bcc')
) as $email => $name) {
$this->write("RCPT TO:<$email>", array(250, 251));
}
$mail->setHeader('Bcc', NULL);
$data = $mail->generateMessage();
$this->write('DATA', 354);
$data = preg_replace('#^\.#m', '..', $data);
$this->write($data);
$this->write('.', 250);
if (!$this->persistent) {
$this->write('QUIT', 221);
$this->disconnect();
}
} catch (SmtpException $e) {
if ($this->connection) {
$this->disconnect();
}
throw $e;
}
}
/**
* Connects and authenticates to SMTP server.
* @return void
*/
protected function connect()
{
$this->connection = @fsockopen( // intentionally @
($this->secure === 'ssl' ? 'ssl://' : '') . $this->host,
$this->port, $errno, $error, $this->timeout
);
if (!$this->connection) {
throw new SmtpException($error, $errno);
}
stream_set_timeout($this->connection, $this->timeout, 0);
$this->read(); // greeting
$self = isset($_SERVER['HTTP_HOST']) && preg_match('#^[\w.-]+\z#', $_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$this->write("EHLO $self");
if ((int) $this->read() !== 250) {
$this->write("HELO $self", 250);
}
if ($this->secure === 'tls') {
$this->write('STARTTLS', 220);
if (!stream_socket_enable_crypto($this->connection, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
throw new SmtpException('Unable to connect via TLS.');
}
$this->write("EHLO $self", 250);
}
if ($this->username != NULL && $this->password != NULL) {
$this->write('AUTH LOGIN', 334);
$this->write(base64_encode($this->username), 334, 'username');
$this->write(base64_encode($this->password), 235, 'password');
}
}
/**
* Disconnects from SMTP server.
* @return void
*/
protected function disconnect()
{
fclose($this->connection);
$this->connection = NULL;
}
/**
* Writes data to server and checks response.
* @param string
* @param int response code
* @param string error message
* @return void
*/
protected function write($line, $expectedCode = NULL, $message = NULL)
{
fwrite($this->connection, $line . Message::EOL);
if ($expectedCode && !in_array((int) $this->read(), (array) $expectedCode)) {
throw new SmtpException('SMTP server did not accept ' . ($message ? $message : $line));
}
}
/**
* Reads response from server.
* @return string
*/
protected function read()
{
$s = '';
while (($line = fgets($this->connection, 1e3)) != NULL) { // intentionally ==
$s .= $line;
if (substr($line, 3, 1) === ' ') {
break;
}
}
return $s;
}
}
/**
* SMTP mailer exception.
*
* @author David Grudl
*/
class SmtpException extends \Exception
{
}
|