Overview

Packages

  • Jyxo_Beholder
  • Jyxo_Charset
  • Jyxo_Color
  • Jyxo_Css
  • Jyxo_ErrorHandling
  • Jyxo_FirePhp
  • Jyxo_Gettext
    • Parser
  • Jyxo_Html
  • Jyxo_Input
    • Chain
    • Filter
    • Validator
  • Jyxo_Mail
    • Email
    • Parser
    • Sender
  • Jyxo_Rpc
    • Json
    • Xml
  • Jyxo_Shell
  • Jyxo_SpamFilter
  • Jyxo_Spl
  • Jyxo_String
  • Jyxo_Svn
  • Jyxo_Time
  • Jyxo_Timer
  • Jyxo_Webdav
  • Jyxo_XmlReader
  • PHP

Classes

  • Jyxo_Mail_Sender
  • Jyxo_Mail_Sender_Result
  • Jyxo_Mail_Sender_Smtp

Exceptions

  • Jyxo_Mail_Sender_CreateException
  • Jyxo_Mail_Sender_Exception
  • Jyxo_Mail_Sender_RecipientUnknownException
  • Jyxo_Mail_Sender_SmtpException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: /**
 15:  * Class for sending emails using a SMTP server.
 16:  * Works in combination with Jyxo_Mail_Sender.
 17:  *
 18:  * @category Jyxo
 19:  * @package Jyxo_Mail
 20:  * @subpackage Sender
 21:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 22:  * @license https://github.com/jyxo/php/blob/master/license.txt
 23:  * @author Jaroslav HanslĂ­k
 24:  */
 25: class Jyxo_Mail_Sender_Smtp
 26: {
 27:     /**
 28:      * Line endings.
 29:      *
 30:      * @var string
 31:      */
 32:     const LINE_END = "\r\n";
 33: 
 34:     /**
 35:      * Established connection.
 36:      *
 37:      * @var resource
 38:      */
 39:     private $connection = null;
 40: 
 41:     /**
 42:      * SMTP server.
 43:      *
 44:      * @var string
 45:      */
 46:     private $host = 'localhost';
 47: 
 48:     /**
 49:      * SMTP port.
 50:      *
 51:      * @var integer
 52:      */
 53:     private $port = 25;
 54: 
 55:     /**
 56:      * SMTP HELO value.
 57:      *
 58:      * @var string
 59:      */
 60:     private $helo = 'localhost';
 61: 
 62:     /**
 63:      * SMTP connection timeout.
 64:      *
 65:      * @var string
 66:      */
 67:     private $timeout = 5;
 68: 
 69:     /**
 70:      * Creates an instance.
 71:      *
 72:      * @param string $host Server hostname
 73:      * @param integer $port Server port
 74:      * @param string $helo HELO value
 75:      * @param integer $timeout Connection timeout
 76:      */
 77:     public function __construct($host = 'localhost', $port = 25, $helo = 'localhost', $timeout = 5)
 78:     {
 79:         $this->host = (string) $host;
 80:         $this->port = (int) $port;
 81:         $this->timeout = (int) $timeout;
 82:         $this->helo = (string) $helo;
 83:     }
 84: 
 85:     /**
 86:      * Destroys an instance and disconnects from the server.
 87:      */
 88:     public function __destruct()
 89:     {
 90:         if (is_resource($this->connection)) {
 91:             $this->disconnect();
 92:         }
 93:     }
 94: 
 95:     /**
 96:      * Connects to the SMTP server.
 97:      *
 98:      * @return Jyxo_Mail_Sender_Smtp
 99:      * @throws Jyxo_Mail_Sender_SmtpException If a connection error occurs
100:      */
101:     public function connect()
102:     {
103:         $this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
104:         if (false === $this->connection) {
105:             throw new Jyxo_Mail_Sender_SmtpException('CONNECTION: ' . $errno . ' ' . $errstr);
106:         }
107: 
108:         // Reads the initial connection data
109:         $this->readData();
110: 
111:         // Sends EHLO/HELO
112:         $this->commandHelo();
113: 
114:         return $this;
115:     }
116: 
117:     /**
118:      * Disconnects from server.
119:      *
120:      * @return Jyxo_Mail_Sender_Smtp
121:      */
122:     public function disconnect()
123:     {
124:         if (is_resource($this->connection)) {
125:             try {
126:                 $this->reset();
127:                 $this->writeData('QUIT');
128:                 fclose($this->connection);
129:                 $this->connection = null;
130:             } catch (Exception $e) {
131:                 // Disconnecting; ignore possible exceptions
132:             }
133:         }
134: 
135:         return $this;
136:     }
137: 
138:     /**
139:      * Connects to the server using a username and password.
140:      *
141:      * @param string $user Username
142:      * @param string $password Password
143:      * @return Jyxo_Mail_Sender_Smtp
144:      * @throws Jyxo_Mail_Sender_SmtpException On authentication error
145:      */
146:     public function auth($user, $password)
147:     {
148:         $this->writeData('AUTH LOGIN');
149:         $response = $this->readData();
150:         if ('334' !== substr($response, 0, 3)) {
151:             throw new Jyxo_Mail_Sender_SmtpException('AUTH: ' . $response);
152:         }
153:         $this->writeData(base64_encode($user));
154:         $response = $this->readData();
155:         if ('334' !== substr($response, 0, 3)) {
156:             throw new Jyxo_Mail_Sender_SmtpException('AUTH: ' . $response);
157:         }
158:         $this->writeData(base64_encode($password));
159:         $response = $this->readData();
160:         if ('235' !== substr($response, 0, 3)) {
161:             throw new Jyxo_Mail_Sender_SmtpException('AUTH: ' . $response);
162:         }
163: 
164:         return $this;
165:     }
166: 
167:     /**
168:      * Sets the sender.
169:      *
170:      * @param string $from Sender
171:      * @return Jyxo_Mail_Sender_Smtp
172:      */
173:     public function from($from)
174:     {
175:         $this->commandMailFrom($from);
176: 
177:         return $this;
178:     }
179: 
180:     /**
181:      * Adds a recipient.
182:      *
183:      * @param string $recipient Recipient
184:      * @return Jyxo_Mail_Sender_Smtp
185:      */
186:     public function recipient($recipient)
187:     {
188:         $this->commandRcptTo($recipient);
189: 
190:         return $this;
191:     }
192: 
193:     /**
194:      * Sends email headers and body.
195:      *
196:      * @param string $header Headers
197:      * @param string $body Body
198:      * @return Jyxo_Mail_Sender_Smtp
199:      * @throws Jyxo_Mail_Sender_SmtpException On data sending error
200:      */
201:     public function data($header, $body)
202:     {
203:         $lineEnds = array(Jyxo_Mail_Sender::LINE_END . '.' => self::LINE_END . '..', Jyxo_Mail_Sender::LINE_END => self::LINE_END);
204:         $header = strtr($header, $lineEnds);
205:         $body = strtr($body, $lineEnds);
206:         if ('.' == $body[0]) {
207:             $body = '.' . $body;
208:         }
209: 
210:         $this->commandData();
211:         $this->writeData(trim($header));
212:         $this->writeData('');
213:         $this->writeData($body);
214:         $this->writeData('.');
215: 
216:         $response = $this->readData();
217:         if ('250' !== substr($response, 0, 3)) {
218:             throw new Jyxo_Mail_Sender_SmtpException('SEND: ' . $response);
219:         }
220: 
221:         return $this;
222:     }
223: 
224:     /**
225:      * Resets previous commands.
226:      *
227:      * @return Jyxo_Mail_Sender_Smtp
228:      */
229:     public function reset()
230:     {
231:         $this->commandRset();
232: 
233:         return $this;
234:     }
235: 
236:     /**
237:      * Sends the EHLO/HELO command.
238:      *
239:      * @throws Jyxo_Mail_Sender_SmtpException On error
240:      */
241:     private function commandHelo()
242:     {
243:         $this->writeData('EHLO ' . $this->helo);
244:         $response = $this->readData();
245:         if ('250' !== substr($response, 0, 3)) {
246:             $this->writeData('HELO ' . $this->helo);
247:             $response = $this->readData();
248:             if ('250' !== substr($response, 0, 3)) {
249:                 throw new Jyxo_Mail_Sender_SmtpException('HELO: ' . $response);
250:             }
251:         }
252:     }
253: 
254:     /**
255:      * Sends the MAIL FROM command.
256:      *
257:      * @param string $from
258:      * @throws Jyxo_Mail_Sender_SmtpException On error
259:      */
260:     private function commandMailFrom($from)
261:     {
262:         $this->writeData('MAIL FROM: <' . $from . '>');
263:         $response = $this->readData();
264:         if ('250' !== substr($response, 0, 3)) {
265:             throw new Jyxo_Mail_Sender_SmtpException('MAIL FROM: ' . $response);
266:         }
267:     }
268: 
269:     /**
270:      * Sends the RCPT TO command.
271:      *
272:      * @param string $recipient
273:      * @throws Jyxo_Mail_Sender_SmtpException On error
274:      */
275:     private function commandRcptTo($recipient)
276:     {
277:         $this->writeData('RCPT TO: <' . $recipient . '>');
278:         $response = $this->readData();
279:         if ('250' !== substr($response, 0, 3)) {
280:             throw new Jyxo_Mail_Sender_SmtpException('RCPT TO: ' . $response);
281:         }
282:     }
283: 
284:     /**
285:      * Sends the DATA command.
286:      *
287:      * @throws Jyxo_Mail_Sender_SmtpException On error
288:      */
289:     private function commandData()
290:     {
291:         $this->writeData('DATA');
292:         $response = $this->readData();
293:         if ('354' !== substr($response, 0, 3)) {
294:             throw new Jyxo_Mail_Sender_SmtpException('DATA: ' . $response);
295:         }
296:     }
297: 
298:     /**
299:      * Sends the RSET command.
300:      *
301:      * @throws Jyxo_Mail_Sender_SmtpException On error
302:      */
303:     private function commandRset()
304:     {
305:         $this->writeData('RSET');
306:         $response = $this->readData();
307:         if ('250' !== substr($response, 0, 3)) {
308:             throw new Jyxo_Mail_Sender_SmtpException('RSET: ' . $response);
309:         }
310:     }
311: 
312:     /**
313:      * Reads data from the server.
314:      *
315:      * @return string
316:      */
317:     private function readData()
318:     {
319:         $data = '';
320:         $i = 0;
321:         while ($line = fgets($this->connection)) {
322:             $data .= $line;
323:             if (' ' == substr($line, 3, 1)) {
324:                 break;
325:             }
326:         }
327:         return $data;
328:     }
329: 
330:     /**
331:      * Sends data to the server.
332:      *
333:      * @param string $data Data
334:      * @throws Jyxo_Mail_Sender_SmtpException On error
335:      */
336:     private function writeData($data)
337:     {
338:         if (!fwrite($this->connection, $data . self::LINE_END)) {
339:             throw new Jyxo_Mail_Sender_SmtpException('Error while writing data.');
340:         }
341:     }
342: }
343: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0