Hoa central
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Hoa\Mail\Transport\Smtp Class Reference
Inheritance diagram for Hoa\Mail\Transport\Smtp:

Public Member Functions

 __construct (Socket\Client $client, $username=null, $password=null)
 
 getClient ()
 
 getUsername ()
 
 getPassword ()
 
 send (Mail\Message $message)
 

Static Public Member Functions

static hmac ($key, $data)
 

Protected Member Functions

 setClient (Socket\Client $client)
 
 setUsername ($username)
 
 setPassword ($password)
 
 ifNot ($code, $errorMessage)
 

Protected Attributes

 $_client = null
 
 $_username = null
 
 $_password = null
 

Detailed Description

Class .

This class allows to send an email by using the SMTP protocol.

Definition at line 50 of file Smtp.php.

Constructor & Destructor Documentation

Hoa\Mail\Transport\Smtp::__construct ( Socket\Client  $client,
  $username = null,
  $password = null 
)

Constructor.

Parameters
\Hoa\Socket\Client$clientClient.
string$usernameUsername (if auth is needed).
string$passwordPassword (if auth is needed).
Returns
void

Definition at line 83 of file Smtp.php.

87  {
88  $this->setClient($client);
89  $this->setUsername($username);
90  $this->setPassword($password);
91 
92  return;
93  }
setPassword($password)
Definition: Smtp.php:149
setUsername($username)
Definition: Smtp.php:125
setClient(Socket\Client $client)
Definition: Smtp.php:101

Here is the call graph for this function:

Member Function Documentation

Hoa\Mail\Transport\Smtp::getClient ( )

Get client.

Returns

Definition at line 114 of file Smtp.php.

115  {
116  return $this->_client;
117  }

Here is the caller graph for this function:

Hoa\Mail\Transport\Smtp::getPassword ( )

Get password.

Returns
string

Definition at line 162 of file Smtp.php.

163  {
164  return $this->_password;
165  }

Here is the caller graph for this function:

Hoa\Mail\Transport\Smtp::getUsername ( )

Get username.

Returns
string

Definition at line 138 of file Smtp.php.

139  {
140  return $this->_username;
141  }

Here is the caller graph for this function:

static Hoa\Mail\Transport\Smtp::hmac (   $key,
  $data 
)
static

H-MAC. Please, see RFC2104, section 2 Definition of HMAC.

Parameters
string$keyKey.
string$dataData.
Returns
string

Definition at line 325 of file Smtp.php.

326  {
327  if (true === function_exists('hash_hmac')) {
328  return hash_hmac('md5', $data, $key);
329  }
330 
331  if (64 < strlen($key)) {
332  $key = pack('H32', md5($key));
333  }
334 
335  if (64 > strlen($key)) {
336  $key = str_pad($key, 64, chr(0x0));
337  }
338 
339  $key = substr($key, 0, 64);
340  $oKeyPad = $key ^ str_repeat(chr(0x5c), 64);
341  $iKeyPad = $key ^ str_repeat(chr(0x36), 64);
342 
343  return md5($oKeyPad . pack('H32', md5($iKeyPad . $data)));
344  }
Hoa\Mail\Transport\Smtp::ifNot (   $code,
  $errorMessage 
)
protected

Check if the client replied correctly. If not, throw an exception containing an error message.

Returns
bool
Exceptions

Definition at line 174 of file Smtp.php.

175  {
176  $client = $this->getClient();
177  $line = $client->readLine();
178  $_code = intval(substr($line, 0, 3));
179 
180  if ($code === $_code) {
181  return $line;
182  }
183 
184  $_message = trim(substr($line, 4));
185  $errorMessage .= ' (code: %d, message: “%s”).';
186  $client->writeAll('QUIT' . CRLF);
187 
188  throw new Mail\Exception\Transport(
189  $errorMessage,
190  0,
191  [$_code, $_message]
192  );
193  }

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Mail\Transport\Smtp::send ( Mail\Message  $message)

Send a message. : Implement the DIGEST-MD5 and GSSAPI auth protocol. Implement SSLv1 and v2 support.

Parameters
\Hoa\Mail\Message$messageMessage.
Returns
bool

Implements Hoa\Mail\Transport\ITransport\Out.

Definition at line 203 of file Smtp.php.

204  {
205  $content = $message->getFormattedContent();
206  $headers = $message->getHeaders();
207 
208  $client = $this->getClient();
209  $client->connect();
210 
211  $this->ifNot(220, 'Not able to connect to the server');
212 
213  $domain = $message->getDomain(
214  $this->getUsername() ?: $headers['from']
215  );
216  $client->writeAll('EHLO ' . $domain . CRLF);
217  $ehlo = preg_split('#' . CRLF . '250[\-\s]+#', $client->read(2048));
218 
219  if (true === in_array('STARTTLS', $ehlo)) {
220  $client->writeAll('STARTTLS' . CRLF);
221  $this->ifNot(220, 'Cannot start a TLS connection');
222 
223  if (true !== $client->enableEncryption(true, $client::ENCRYPTION_TLS)) {
224  throw new Mail\Exception\Transport(
225  'Cannot enable a TLS connection.',
226  1
227  );
228  }
229 
230  $client->writeAll('EHLO ' . $domain . CRLF);
231  $ehlo = preg_split('#' . CRLF . '250[\-\s]+#', $client->read(2048));
232  }
233 
234  $matches = null;
235 
236  foreach ($ehlo as $entry) {
237  if (0 !== preg_match('#^AUTH (.+)$#', $entry, $matches)) {
238  break;
239  }
240  }
241 
242  if (empty($matches)) {
243  throw new Mail\Exception\Transport(
244  'The server does not support authentication, we cannot ' .
245  'authenticate.',
246  2
247  );
248  }
249 
250  $auth = explode(' ', $matches[1]);
251  $username = $this->getUsername();
252  $password = $this->getPassword();
253 
254  if (true === in_array('PLAIN', $auth)) {
255  $client->writeAll('AUTH PLAIN' . CRLF);
256  $this->ifNot(334, 'Authentication failed (PLAIN)');
257 
258  $challenge = base64_encode("\0" . $username . "\0" . $password);
259  $client->writeAll($challenge . CRLF);
260  $this->ifNot(235, 'Wrong username or password');
261  } elseif (true === in_array('LOGIN', $auth)) {
262  $client->writeAll('AUTH LOGIN' . CRLF);
263  $this->ifNot(334, 'Authentication failed (LOGIN)');
264 
265  $challenge = base64_encode($username);
266  $client->writeAll($challenge . CRLF);
267  $this->ifNot(334, 'Wrong username');
268 
269  $challenge = base64_encode($password);
270  $client->writeAll($challenge . CRLF);
271  $this->ifNot(235, 'Wrong password');
272  } elseif (true === in_array('CRAM-MD5', $auth)) {
273  $client->writeAll('AUTH CRAM-MD5' . CRLF);
274  $line = $this->ifNot(334, 'Authentication failed (CRAM-MD5)');
275 
276  $handle = base64_decode(substr($line, 4));
277  $challenge = base64_encode(
278  $username . ' ' . static::hmac($password, $handle)
279  );
280  $client->writeAll($challenge . CRLF);
281  $this->ifNot(235, 'Wrong username or password');
282  } else {
283  throw new Mail\Transport(
284  '%s does not support authentication algorithms available ' .
285  'on the server (%s).',
286  3,
287  [__CLASS__, implode(', ', $auth)]
288  );
289  }
290 
291  $from = $message->getAddress($headers['from']);
292  $client->writeAll('MAIL FROM: <' . $from . '>' . CRLF);
293  $this->ifNot(250, 'Sender ' . $from . ' is wrong');
294 
295  foreach ($message->getRecipients() as $recipient) {
296  $client->writeAll('RCPT TO: <' . $recipient . '>' . CRLF);
297  $this->ifNot(250, 'Recipient ' . $recipient . ' is wrong');
298  }
299 
300  $client->writeAll('DATA' . CRLF);
301  $this->ifNot(354, 'Cannot send data');
302 
303  $client->writeAll(
304  $content . CRLF .
305  '.' . CRLF
306  );
307  $this->ifNot(250, 'Something went wrong with data');
308 
309  $client->writeAll('QUIT' . CRLF);
310  $this->ifNot(221, 'Cannot quit properly');
311 
312  $client->disconnect();
313 
314  return true;
315  }
ifNot($code, $errorMessage)
Definition: Smtp.php:174
$content
Definition: Hoa.php:119

Here is the call graph for this function:

Hoa\Mail\Transport\Smtp::setClient ( Socket\Client  $client)
protected

Set client.

Parameters
\Hoa\Socket\Client$clientClient.
Returns

Definition at line 101 of file Smtp.php.

102  {
103  $old = $this->_client;
104  $this->_client = $client;
105 
106  return $old;
107  }

Here is the caller graph for this function:

Hoa\Mail\Transport\Smtp::setPassword (   $password)
protected

Set password (if authentication is needed).

Parameters
string$passwordPassword.
Returns
string

Definition at line 149 of file Smtp.php.

150  {
151  $old = $this->_password;
152  $this->_password = $password;
153 
154  return $old;
155  }

Here is the caller graph for this function:

Hoa\Mail\Transport\Smtp::setUsername (   $username)
protected

Set username (if authentication is needed).

Parameters
string$usernameUsername.
Returns
string

Definition at line 125 of file Smtp.php.

126  {
127  $old = $this->_username;
128  $this->_username = $username;
129 
130  return $old;
131  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Mail\Transport\Smtp::$_client = null
protected

Definition at line 57 of file Smtp.php.

Hoa\Mail\Transport\Smtp::$_password = null
protected

Definition at line 71 of file Smtp.php.

Hoa\Mail\Transport\Smtp::$_username = null
protected

Definition at line 64 of file Smtp.php.


The documentation for this class was generated from the following file: