/usr/share/php/Nette/Mail/SmtpMailer.php is in php-nette 2.4-20160731-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 204 205 206 207 208 209 210 211 212 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Mail;
use Nette;
/**
* Sends emails via the SMTP server.
*/
class SmtpMailer implements IMailer
{
use Nette\SmartObject;
/** @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 resource */
private $context;
/** @var bool */
private $persistent;
public function __construct(array $options = [])
{
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;
$this->context = isset($options['context']) ? stream_context_create($options['context']) : stream_context_get_default();
if (!$this->port) {
$this->port = $this->secure === 'ssl' ? 465 : 25;
}
$this->persistent = !empty($options['persistent']);
}
/**
* Sends email.
* @return void
* @throws SmtpException
*/
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>", [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 = @stream_socket_client( // @ is escalated to exception
($this->secure === 'ssl' ? 'ssl://' : '') . $this->host . ':' . $this->port,
$errno, $error, $this->timeout, STREAM_CLIENT_CONNECT, $this->context
);
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");
$ehloResponse = $this->read();
if ((int) $ehloResponse !== 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) {
$authMechanisms = [];
if (preg_match('~^250[ -]AUTH (.*)$~im', $ehloResponse, $matches)) {
$authMechanisms = explode(' ', trim($matches[1]));
}
if (in_array('PLAIN', $authMechanisms, TRUE)) {
$credentials = $this->username . "\0" . $this->username . "\0" . $this->password;
$this->write('AUTH PLAIN ' . base64_encode($credentials), 235, 'PLAIN credentials');
} else {
$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 against expected code if some provided.
* @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) {
$response = $this->read();
if (!in_array((int) $response, (array) $expectedCode, TRUE)) {
throw new SmtpException('SMTP server did not accept ' . ($message ? $message : $line) . ' with error: ' . trim($response));
}
}
}
/**
* 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;
}
}
|