Packages

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • NMail
  • NMailMimePart
  • NSendmailMailer
  • NSmtpMailer

Interfaces

  • IMailer

Exceptions

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