CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AbstractTransport
  • CakeEmail
  • DebugTransport
  • MailTransport
  • SmtpTransport
  1: <?php
  2: /**
  3:  * Send mail using SMTP protocol
  4:  *
  5:  * PHP 5
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * Redistributions of files must retain the above copyright notice.
 12:  *
 13:  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @package       Cake.Network.Email
 16:  * @since         CakePHP(tm) v 2.0.0
 17:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 18:  */
 19: 
 20: App::uses('CakeSocket', 'Network');
 21: 
 22: /**
 23:  * Send mail using SMTP protocol
 24:  *
 25:  * @package       Cake.Network.Email
 26:  */
 27: class SmtpTransport extends AbstractTransport {
 28: 
 29: /**
 30:  * Socket to SMTP server
 31:  *
 32:  * @var CakeSocket
 33:  */
 34:     protected $_socket;
 35: 
 36: /**
 37:  * CakeEmail
 38:  *
 39:  * @var CakeEmail
 40:  */
 41:     protected $_cakeEmail;
 42: 
 43: /**
 44:  * Content of email to return
 45:  *
 46:  * @var string
 47:  */
 48:     protected $_content;
 49: 
 50: /**
 51:  * Send mail
 52:  *
 53:  * @param CakeEmail $email CakeEmail
 54:  * @return array
 55:  * @throws SocketException
 56:  */
 57:     public function send(CakeEmail $email) {
 58:         $this->_cakeEmail = $email;
 59: 
 60:         $this->_connect();
 61:         $this->_auth();
 62:         $this->_sendRcpt();
 63:         $this->_sendData();
 64:         $this->_disconnect();
 65: 
 66:         return $this->_content;
 67:     }
 68: 
 69: /**
 70:  * Set the configuration
 71:  *
 72:  * @param array $config
 73:  * @return void
 74:  */
 75:     public function config($config = array()) {
 76:         $default = array(
 77:             'host' => 'localhost',
 78:             'port' => 25,
 79:             'timeout' => 30,
 80:             'username' => null,
 81:             'password' => null,
 82:             'client' => null
 83:         );
 84:         $this->_config = $config + $default;
 85:     }
 86: 
 87: /**
 88:  * Connect to SMTP Server
 89:  *
 90:  * @return void
 91:  * @throws SocketException
 92:  */
 93:     protected function _connect() {
 94:         $this->_generateSocket();
 95:         if (!$this->_socket->connect()) {
 96:             throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
 97:         }
 98:         $this->_smtpSend(null, '220');
 99: 
100:         if (isset($this->_config['client'])) {
101:             $host = $this->_config['client'];
102:         } elseif ($httpHost = env('HTTP_HOST')) {
103:             list($host) = explode(':', $httpHost);
104:         } else {
105:             $host = 'localhost';
106:         }
107: 
108:         try {
109:             $this->_smtpSend("EHLO {$host}", '250');
110:         } catch (SocketException $e) {
111:             try {
112:                 $this->_smtpSend("HELO {$host}", '250');
113:             } catch (SocketException $e2) {
114:                 throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
115:             }
116:         }
117:     }
118: 
119: /**
120:  * Send authentication
121:  *
122:  * @return void
123:  * @throws SocketException
124:  */
125:     protected function _auth() {
126:         if (isset($this->_config['username']) && isset($this->_config['password'])) {
127:             $authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
128:             if ($authRequired == '334') {
129:                 if (!$this->_smtpSend(base64_encode($this->_config['username']), '334')) {
130:                     throw new SocketException(__d('cake_dev', 'SMTP server did not accept the username.'));
131:                 }
132:                 if (!$this->_smtpSend(base64_encode($this->_config['password']), '235')) {
133:                     throw new SocketException(__d('cake_dev', 'SMTP server did not accept the password.'));
134:                 }
135:             } elseif ($authRequired != '503') {
136:                 throw new SocketException(__d('cake_dev', 'SMTP does not require authentication.'));
137:             }
138:         }
139:     }
140: 
141: /**
142:  * Send emails
143:  *
144:  * @return void
145:  * @throws SocketException
146:  */
147:     protected function _sendRcpt() {
148:         $from = $this->_cakeEmail->from();
149:         $this->_smtpSend('MAIL FROM:<' . key($from) . '>');
150: 
151:         $to = $this->_cakeEmail->to();
152:         $cc = $this->_cakeEmail->cc();
153:         $bcc = $this->_cakeEmail->bcc();
154:         $emails = array_merge(array_keys($to), array_keys($cc), array_keys($bcc));
155:         foreach ($emails as $email) {
156:             $this->_smtpSend('RCPT TO:<' . $email . '>');
157:         }
158:     }
159: 
160: /**
161:  * Send Data
162:  *
163:  * @return void
164:  * @throws SocketException
165:  */
166:     protected function _sendData() {
167:         $this->_smtpSend('DATA', '354');
168: 
169:         $headers = $this->_cakeEmail->getHeaders(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'subject'));
170:         $headers = $this->_headersToString($headers);
171:         $message = implode("\r\n", $this->_cakeEmail->message());
172:         $this->_smtpSend($headers . "\r\n\r\n" . $message . "\r\n\r\n\r\n.");
173:         $this->_content = array('headers' => $headers, 'message' => $message);
174:     }
175: 
176: /**
177:  * Disconnect
178:  *
179:  * @return void
180:  * @throws SocketException
181:  */
182:     protected function _disconnect() {
183:         $this->_smtpSend('QUIT', false);
184:         $this->_socket->disconnect();
185:     }
186: 
187: /**
188:  * Helper method to generate socket
189:  *
190:  * @return void
191:  * @throws SocketException
192:  */
193:     protected function _generateSocket() {
194:         $this->_socket = new CakeSocket($this->_config);
195:     }
196: 
197: /**
198:  * Protected method for sending data to SMTP connection
199:  *
200:  * @param string $data data to be sent to SMTP server
201:  * @param mixed $checkCode code to check for in server response, false to skip
202:  * @return void
203:  * @throws SocketException
204:  */
205:     protected function _smtpSend($data, $checkCode = '250') {
206:         if (!is_null($data)) {
207:             $this->_socket->write($data . "\r\n");
208:         }
209:         while ($checkCode !== false) {
210:             $response = '';
211:             $startTime = time();
212:             while (substr($response, -2) !== "\r\n" && ((time() - $startTime) < $this->_config['timeout'])) {
213:                 $response .= $this->_socket->read();
214:             }
215:             if (substr($response, -2) !== "\r\n") {
216:                 throw new SocketException(__d('cake_dev', 'SMTP timeout.'));
217:             }
218:             $responseLines = explode("\r\n", rtrim($response, "\r\n"));
219:             $response = end($responseLines);
220: 
221:             if (preg_match('/^(' . $checkCode . ')(.)/', $response, $code)) {
222:                 if ($code[2] === '-') {
223:                     continue;
224:                 }
225:                 return $code[1];
226:             }
227:             throw new SocketException(__d('cake_dev', 'SMTP Error: %s', $response));
228:         }
229:     }
230: 
231: }
232: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs