1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Mail;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class SmtpMailer extends Nette\Object implements IMailer
24: {
25:
26: private $connection;
27:
28:
29: private $host;
30:
31:
32: private $port;
33:
34:
35: private $username;
36:
37:
38: private $password;
39:
40:
41: private $secure;
42:
43:
44: private $timeout;
45:
46:
47:
48: public function __construct(array $options = array())
49: {
50: if (isset($options['host'])) {
51: $this->host = $options['host'];
52: $this->port = isset($options['port']) ? (int) $options['port'] : NULL;
53: } else {
54: $this->host = ini_get('SMTP');
55: $this->port = (int) ini_get('smtp_port');
56: }
57: $this->username = isset($options['username']) ? $options['username'] : '';
58: $this->password = isset($options['password']) ? $options['password'] : '';
59: $this->secure = isset($options['secure']) ? $options['secure'] : '';
60: $this->timeout = isset($options['timeout']) ? (int) $options['timeout'] : 20;
61: if (!$this->port) {
62: $this->port = $this->secure === 'ssl' ? 465 : 25;
63: }
64: }
65:
66:
67:
68: 69: 70: 71:
72: public function send(Message $mail)
73: {
74: $data = $mail->generateMessage();
75:
76: $this->connect();
77:
78: $from = $mail->getHeader('From');
79: if ($from) {
80: $from = array_keys($from);
81: $this->write("MAIL FROM:<$from[0]>", 250);
82: }
83:
84: foreach (array_merge(
85: (array) $mail->getHeader('To'),
86: (array) $mail->getHeader('Cc'),
87: (array) $mail->getHeader('Bcc')
88: ) as $email => $name) {
89: $this->write("RCPT TO:<$email>", array(250, 251));
90: }
91:
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: 106: 107:
108: private function connect()
109: {
110: $this->connection = @fsockopen(
111: ($this->secure === 'ssl' ? 'ssl://' : '') . $this->host,
112: $this->port, $errno, $error, $this->timeout
113: );
114: if (!$this->connection) {
115: throw new SmtpException($error, $errno);
116: }
117: stream_set_timeout($this->connection, $this->timeout, 0);
118: $this->read();
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 SmtpException('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: 145: 146:
147: private function disconnect()
148: {
149: fclose($this->connection);
150: $this->connection = NULL;
151: }
152:
153:
154:
155: 156: 157: 158: 159: 160: 161:
162: private function write($line, $expectedCode = NULL, $message = NULL)
163: {
164: fwrite($this->connection, $line . Message::EOL);
165: if ($expectedCode && !in_array((int) $this->read(), (array) $expectedCode)) {
166: throw new SmtpException('SMTP server did not accept ' . ($message ? $message : $line));
167: }
168: }
169:
170:
171:
172: 173: 174: 175:
176: private function read()
177: {
178: $s = '';
179: while (($line = fgets($this->connection, 1e3)) != NULL) {
180: $s .= $line;
181: if (substr($line, 3, 1) === ' ') {
182: break;
183: }
184: }
185: return $s;
186: }
187:
188: }
189:
190:
191:
192: 193: 194: 195: 196:
197: class SmtpException extends \Exception
198: {
199: }
200: