Author: Frederik Holljen
Date: 2006-01-13 12:06:24 +0100 (Fri, 13 Jan 2006)
New Revision: 1816
Log:
- Moved to new exception style.
- Made the exposed interface of ezcMailSmtpTransport private. It is very
unlikely that anyone would ever want to extend from it. If they want to,
copying it is probably the better option anyway since the methods are very
tightly coupled.
Modified:
packages/Mail/trunk/ChangeLog
packages/Mail/trunk/src/composer.php
packages/Mail/trunk/src/exceptions/mail_exception.php
packages/Mail/trunk/src/exceptions/transport_exception.php
packages/Mail/trunk/src/exceptions/transport_smtp_exception.php
packages/Mail/trunk/src/parts/file.php
packages/Mail/trunk/src/transports/transport_mta.php
packages/Mail/trunk/src/transports/transport_smtp.php
packages/Mail/trunk/tests/composer_test.php
packages/Mail/trunk/tests/parts/file_part_test.php
Modified: packages/Mail/trunk/ChangeLog
===================================================================
--- packages/Mail/trunk/ChangeLog 2006-01-13 10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/ChangeLog 2006-01-13 11:06:24 UTC (rev 1816)
@@ -2,6 +2,8 @@
- renamed ezcMailText::characterSet to charset
- fixed bug #7637: ezcMailComposer encoding
- fixed feature enhancement #7582: New bug: mail - adding multiple part
+- made protected methods of ezcMailSmtpTransport private. They exposed an
interface very
+ unlikely ever to be changed.
1.0beta2 - Wednesday 21 December 2005
Modified: packages/Mail/trunk/src/composer.php
===================================================================
--- packages/Mail/trunk/src/composer.php 2006-01-13 10:22:29 UTC (rev
1815)
+++ packages/Mail/trunk/src/composer.php 2006-01-13 11:06:24 UTC (rev
1816)
@@ -133,7 +133,8 @@
/**
* Adds the file $fileName to the list of attachments.
*
- * @throws ezcMailException if $fileName is not found or can't be read.
+ * @throws ezcBaseFileNotFoundException if $fileName does not exists.
+ * @throws ezcBaseFilePermissionProblem if $fileName could not be read.
* @param string $fileName
* @return void
*/
@@ -145,7 +146,14 @@
}
else
{
- throw new ezcMailException( "Can't locate or read the file:
$fileName", ezcMailException::INVALID_FILE );
+ if( file_exists( $fileName ) )
+ {
+ throw new ezcBaseFilePermissionException( $fileName,
ezcBaseFileException::READ );
+ }
+ else
+ {
+ throw new ezcBaseFileNotFoundException( $fileName );
+ }
}
}
@@ -154,7 +162,7 @@
*
* This method must be called before the message is sent.
*
- * @throws ezcMailException if an inline file is not found or can't be
read.
+ * @throws ezcBaseFileNotFoundException if any of the attachment files can
not be found.
* @return void
*/
public function build()
@@ -247,9 +255,15 @@
}
else
{
+ if( file_exists( $fileName ) )
+ {
+ throw new ezcBaseFilePermissionException(
$fileName, ezcBaseFileException::READ );
+ }
+ else
+ {
+ throw new ezcBaseFileNotFoundException( $fileName
);
+ }
// throw
- throw new ezcMailException( "Can't locate or read the
file: $fileName",
-
ezcMailException::INVALID_FILE );
}
}
// update mail, with replaced url's
Modified: packages/Mail/trunk/src/exceptions/mail_exception.php
===================================================================
--- packages/Mail/trunk/src/exceptions/mail_exception.php 2006-01-13
10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/src/exceptions/mail_exception.php 2006-01-13
11:06:24 UTC (rev 1816)
@@ -16,22 +16,16 @@
*/
class ezcMailException extends Exception
{
-
/**
- * Defines the various errors that might occure in the mail package.
- */
- const INVALID_FILE = 1;
-
- /**
* Constructs a new ezcMailException with error message $message and
* error code $code.
*
* @param string $message
* @param int $code
*/
- public function __construct( $message, $code )
+ public function __construct( $message )
{
- parent::__construct( $message, $code );
+ parent::__construct( $message, 0 );
}
}
?>
Modified: packages/Mail/trunk/src/exceptions/transport_exception.php
===================================================================
--- packages/Mail/trunk/src/exceptions/transport_exception.php 2006-01-13
10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/src/exceptions/transport_exception.php 2006-01-13
11:06:24 UTC (rev 1816)
@@ -15,26 +15,17 @@
* @package Mail
* @version //autogen//
*/
-class ezcMailTransportException extends Exception
+class ezcMailTransportException extends ezcBaseException
{
- private $additionalInfo;
-
/**
- * Constructs an ezcMailTransportException with the highlevel error
- * message $message and the lowlevel errormessage $additionalInfo.
+ * Constructs an ezcMailTransportException with low level information
$driverInfo.
*
* @param string $message
* @param string $additionalInfo
*/
- public function __construct( $message = '', $additionalInfo = '' )
+ public function __construct( $driverInfo = '' )
{
- parent::__construct( $message, 0 );
- $this->additionalInfo = $additionalInfo;
+ parent::__construct( 'The mail could not be sent. ' . $driverInfo , 0
);
}
-
- public function getAdditionalInfo()
- {
- return $this->additionalInfo;
- }
}
?>
Modified: packages/Mail/trunk/src/exceptions/transport_smtp_exception.php
===================================================================
--- packages/Mail/trunk/src/exceptions/transport_smtp_exception.php
2006-01-13 10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/src/exceptions/transport_smtp_exception.php
2006-01-13 11:06:24 UTC (rev 1816)
@@ -6,6 +6,7 @@
* @version //autogen//
* @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
+ * @access private
*/
/**
@@ -14,28 +15,19 @@
*
* @package Mail
* @version //autogen//
+ * @access private
*/
-class ezcMailTransportSmtpException extends Exception
+class ezcMailTransportSmtpException extends ezcMailException
{
- const CONNECTION_FAILED = 1;
- const STREAM_WRITE_ERROR = 2;
- const STREAM_READ_ERROR = 3;
- const AUTHENTICATION_FAILED = 4;
- const COMMAND_HELO_FAILED = 5;
- const COMMAND_MAIL_FAILED = 6;
- const COMMAND_RCPT_FAILED = 7;
- const COMMAND_DATA_FAILED = 8;
- const COMMAND_QUIT_FAILED = 9;
-
/**
* Constructs an ezcMailTransportSmtpException with the highlevel error
* message $message and the lowlevel errormessage $additionalInfo.
*
* @param int $code
*/
- public function __construct( $message, $code )
+ public function __construct( $message )
{
- parent::__construct( $message, $code );
+ parent::__construct( $message );
}
}
?>
Modified: packages/Mail/trunk/src/parts/file.php
===================================================================
--- packages/Mail/trunk/src/parts/file.php 2006-01-13 10:22:29 UTC (rev
1815)
+++ packages/Mail/trunk/src/parts/file.php 2006-01-13 11:06:24 UTC (rev
1816)
@@ -106,6 +106,7 @@
* Sets the property $name to $value.
*
* @throws ezcBasePropertyNotFoundException if the property does not exist.
+ * @throws ezcBaseFileNotFoundException when setting the property with an
invalid filename.
* @param string $name
* @param mixed $value
* @return void
@@ -123,7 +124,7 @@
}
else
{
- throw new ezcMailException( "Can't locate or read the
file: $value", ezcMailException::INVALID_FILE );
+ throw new ezcBaseFileNotFoundException( $value );
}
break;
// case 'encoding':
Modified: packages/Mail/trunk/src/transports/transport_mta.php
===================================================================
--- packages/Mail/trunk/src/transports/transport_mta.php 2006-01-13
10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/src/transports/transport_mta.php 2006-01-13
11:06:24 UTC (rev 1816)
@@ -41,7 +41,7 @@
$mail->subject, $mail->generateBody(),
$mail->generateHeaders() );
if ( $success === false )
{
- throw new ezcMailException();
+ throw new ezcMailTransportException();
}
}
}
Modified: packages/Mail/trunk/src/transports/transport_smtp.php
===================================================================
--- packages/Mail/trunk/src/transports/transport_smtp.php 2006-01-13
10:22:29 UTC (rev 1815)
+++ packages/Mail/trunk/src/transports/transport_smtp.php 2006-01-13
11:06:24 UTC (rev 1816)
@@ -212,16 +212,11 @@
*/
public function send( ezcMail $mail )
{
- if ( ( $mail instanceof ezcMail ) == false )
- {
- throw new ezcMailTransportException( "Parameter mail has the
incorrect type" );
- }
-
// sanity check the e-mail
// need at least one recepient
if ( count( $mail->to ) < 1 )
{
- throw new ezcMailTransportException( "Can't send e-mail with no
'to' recipients" );
+ throw new ezcMailTransportException( "Can not send e-mail with no
'to' recipients." );
}
try
@@ -268,30 +263,7 @@
}
catch ( ezcMailTransportSmtpException $e )
{
- $message = '';
- switch ( $e->getCode() )
- {
- case ezcMailTransportSmtpException::CONNECTION_FAILED:
- $message = 'Could not connect to the SMTP server.';
- break;
- case ezcMailTransportSmtpException::STREAM_WRITE_ERROR:
- $message = 'Could not write to the SMTP server. The
connection was lost.';
- break;
- case ezcMailTransportSmtpException::STREAM_READ_ERROR:
- $message = 'Could not read from the SMTP server. The
connection was lost.';
- break;
- case ezcMailTransportSmtpException::AUTHENTICATION_FAILED:
- $message = 'Authentication failed. The username/password
combination was wrong.';
- break;
- case ezcMailTransportSmtpException::COMMAND_HELO_FAILED:
- case ezcMailTransportSmtpException::COMMAND_MAIL_FAILED:
- case ezcMailTransportSmtpException::COMMAND_RCPT_FAILED:
- case ezcMailTransportSmtpException::COMMAND_DATA_FAILED:
- case ezcMailTransportSmtpException::COMMAND_QUIT_FAILED: //
impossible, but still covered
- $message = "The SMTP server did not accept data.";
- break;
- }
- throw new ezcMailTransportException( $message, $e->getMessage() );
+ throw new ezcMailTransportException( $e->getMessage() );
}
try
{
@@ -311,7 +283,7 @@
* or if the login failed.
* @return void
*/
- protected function connect( )
+ private function connect( )
{
// FIXME: The @ should be removed when PHP doesn't throw warnings for
connect problems
$this->connection = @stream_socket_client(
"tcp://{$this->serverHost}:{$this->serverPort}",
@@ -326,8 +298,7 @@
}
else
{
- throw new ezcMailTransportSmtpException( 'Failed to connect to
server.',
-
ezcMailTransportSmtpException::CONNECTION_FAILED );
+ throw new ezcMailTransportSmtpException( 'Failed to connect to the
smtp server: {$this->serverHost}:{$this->serverPort}.' );
}
}
@@ -339,7 +310,7 @@
* @throws ezcMailTransportSmtpException if the HELO/EHLO command or
authentication fails.
* @return void
*/
- protected function login()
+ private function login()
{
if ( $this->doAuthenticate )
{
@@ -351,8 +322,7 @@
}
if ( $this->getReplyCode( $error ) !== '250' )
{
- throw new ezcMailTransportSmtpException( "HELO/EHLO failed
with error: $error.",
-
ezcMailTransportSmtpException::COMMAND_HELO_FAILED );
+ throw new ezcMailTransportSmtpException( "HELO/EHLO failed
with error: $error." );
}
// do authentication
@@ -361,22 +331,19 @@
$this->sendData( 'AUTH LOGIN' );
if ( $this->getReplyCode( $error ) !== '334' )
{
- throw new ezcMailTransportSmtpException( 'SMTP server does not
accept AUTH LOGIN.',
-
ezcMailTransportSmtpException::AUTHENTICATION_FAILED );
+ throw new ezcMailTransportSmtpException( 'SMTP server does not
accept AUTH LOGIN.' );
}
$this->sendData( base64_encode( $this->user ) );
if ( $this->getReplyCode( $error ) !== '334' )
{
- throw new ezcMailTransportSmtpException( "SMTP server does not
accept login: $this->user.",
-
ezcMailTransportSmtpException::AUTHENTICATION_FAILED );
+ throw new ezcMailTransportSmtpException( "SMTP server does not
accept login: {$this->user}." );
}
$this->sendData( base64_encode( $this->password ) );
if ( $this->getReplyCode( $error ) !== '235' )
{
- throw new ezcMailTransportSmtpException( 'SMTP server does not
accept the password.',
-
ezcMailTransportSmtpException::AUTHENTICATION_FAILED );
+ throw new ezcMailTransportSmtpException( 'SMTP server does not
accept the password.' );
}
}
$this->status = self::STATUS_AUTHENTICATED;
@@ -398,8 +365,7 @@
$this->status = self::STATUS_NOT_CONNECTED;
if ( $replyCode )
{
- throw new ezcMailTransportSmtpException( "QUIT failed with
error: $error.",
-
ezcMailTransportSmtpException::AUTHENTICATION_FAILED );
+ throw new ezcMailTransportSmtpException( "QUIT failed with
error: $error." );
}
}
}
@@ -412,7 +378,7 @@
* @param string $email
* $return string
*/
- protected function composeSmtpMailAddress( $email )
+ private function composeSmtpMailAddress( $email )
{
if ( !preg_match( "/<.+>/", $email ) )
{
@@ -432,15 +398,14 @@
* @param string $from
* @return void
*/
- protected function cmdMail( $from )
+ private function cmdMail( $from )
{
if ( self::STATUS_AUTHENTICATED )
{
$this->sendData( 'MAIL FROM:' . $this->composeSmtpMailAddress(
$from ) . '' );
if ( $this->getReplyCode( $error ) !== '250' )
{
- throw new ezcMailTransportSmtpException( "MAIL FROM failed
with error: $error.",
-
ezcMailTransportSmtpException::COMMAND_MAIL_FAILED );
+ throw new ezcMailTransportSmtpException( "MAIL FROM failed
with error: $error." );
}
}
}
@@ -466,8 +431,7 @@
$this->sendData( 'RCPT TO:' . $this->composeSmtpMailAddress(
$email ) );
if ( $this->getReplyCode( $error ) !== '250' )
{
- throw new ezcMailTransportSmtpException( "RCPT TO failed with
error: $error.",
-
ezcMailTransportSmtpException::COMMAND_RCPT_FAILED );
+ throw new ezcMailTransportSmtpException( "RCPT TO failed with
error: $error." );
}
}
}
@@ -478,15 +442,14 @@
* @throws ezcMailTransportException if there is no valid connection or if
the DATA command failed.
* @return void
*/
- protected function cmdData()
+ private function cmdData()
{
if ( self::STATUS_AUTHENTICATED )
{
$this->sendData( 'DATA' );
if ( $this->getReplyCode( $error ) !== '354' )
{
- throw new ezcMailTransportSmtpException( "DATA failed with
error: $error.",
-
ezcMailTransportSmtpException::COMMAND_DATA_FAILED );
+ throw new ezcMailTransportSmtpException( "DATA failed with
error: $error." );
}
}
}
@@ -500,15 +463,14 @@
* @param string $data
* @return void
*/
- protected function sendData( $data )
+ private function sendData( $data )
{
if ( is_resource( $this->connection ) )
{
if ( fwrite( $this->connection, $data . self::CRLF,
strlen( $data ) + strlen( self::CRLF ) ) === false )
{
- throw new ezcMailTransportSmtpException( 'Could not write to
SMTP stream',
-
ezcMailTransportSmtpException::STREAM_WRITE_ERROR );
+ throw new ezcMailTransportSmtpException( 'Could not write to
SMTP stream. It was probably terminated by the host.' );
}
}
}
@@ -519,7 +481,7 @@
* @throws ezcMailTransportSmtpConnection if there is no valid connection.
* @return string
*/
- protected function getData()
+ private function getData()
{
$data = '';
$line = '';
@@ -535,8 +497,7 @@
}
return $data;
}
- throw new ezcMailTransportSmtpException( 'Could not read from SMTP
stream',
-
ezcMailTransportSmtpException::STREAM_READ_ERROR );
+ throw new ezcMailTransportSmtpException( 'Could not read from SMTP
stream. It was probably terminated by the host.' );
}
/**
@@ -549,7 +510,7 @@
* @param string &$line
* @return string
*/
- protected function getReplyCode( &$line )
+ private function getReplyCode( &$line )
{
return substr( trim( $line = $this->getData() ), 0, 3 );
}
Modified: packages/Mail/trunk/tests/composer_test.php
===================================================================
--- packages/Mail/trunk/tests/composer_test.php 2006-01-13 10:22:29 UTC (rev
1815)
+++ packages/Mail/trunk/tests/composer_test.php 2006-01-13 11:06:24 UTC (rev
1816)
@@ -69,13 +69,9 @@
$this->mail->addAttachment( dirname( __FILE__) .
"/does_not_exist.jpg" );
$this->mail->build();
}
- catch ( ezcMailException $e )
+ catch ( ezcBaseFileNotFoundException $e )
{
- if ( $e->getCode() == ezcMailException::INVALID_FILE )
- {
- return;
- }
- $this->fail( "Got exception of wrong type. Expected
ezcMailException::INVALID_FILE\n" );
+ return;
}
$this->fail( "Adding broken attachments did not fail.\n" );
}
@@ -96,13 +92,9 @@
$this->mail->addAttachment( dirname( __FILE__) .
"/parts/data/fly.jpg" );
$this->mail->build();
}
- catch ( ezcMailException $e )
+ catch ( ezcBaseFileNotFoundException $e )
{
- if ( $e->getCode() == ezcMailException::INVALID_FILE )
- {
- return;
- }
- $this->fail( "Got exception of wrong type. Expected
ezcMailException::INVALID_FILE\n" );
+ return;
}
$this->fail( "HTML with broken local links did not cause exception.\n"
);
}
@@ -226,7 +218,7 @@
*/
public function testMailHtmlWithImagesAndFiles()
{
- $this->mail->from = array( new ezcMailAddress( '[EMAIL PROTECTED]',
'Frederik Holljen' ) );
+ $this->mail->from = new ezcMailAddress( '[EMAIL PROTECTED]', 'Frederik
Holljen' );
$this->mail->addTo( new ezcMailAddress( '[EMAIL PROTECTED]', 'Frederik
Holljen' ) );
$this->mail->subject = "HTML message with embeded files and images.";
$this->mail->htmlText = "<html>Some text before the simage: <img
src=\"file://"
@@ -240,7 +232,7 @@
// echo $this->mail->generate();
// echo "\n---------------\n";
// let's try to send the thing
- $transport = new ezcMailTransportMta();
+// $transport = new ezcMailTransportSmtp( "smtp.ez.no" );
// $transport->send( $this->mail );
}
Modified: packages/Mail/trunk/tests/parts/file_part_test.php
===================================================================
--- packages/Mail/trunk/tests/parts/file_part_test.php 2006-01-13 10:22:29 UTC
(rev 1815)
+++ packages/Mail/trunk/tests/parts/file_part_test.php 2006-01-13 11:06:24 UTC
(rev 1816)
@@ -37,9 +37,8 @@
{
$filePart = new ezcMailFile( dirname( __FILE__) .
"/data/fly_not_exit.jpg" );
}
- catch ( ezcMailException $e )
+ catch ( ezcBaseFileNotFoundException $e )
{
- $this->assertEquals( ezcMailException::INVALID_FILE, $e->getCode()
);
return;
}
$this->fail( "Invalid file failed or wrong exception thrown" );
--
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components