Page 1 of 1

Test aborts upon failure

Posted: Thu Nov 27, 2008 8:02 am
by hknight
I want to test if SMTP credentials are valid or not.
I am not trying to send an email, I only want to know if the the account information provided is good or not.

The problem is I get a fatal error upon failure!

If someone provides valid account information then "NULL" is returned. If they provide invalid information then a I get a fatal error.

I want "TRUE" to be returned if the account is valid and "FALSE" to be returned if the account is invalid.

Code: Select all

<?php
require_once 'Swift/Connection/SMTP.php';
require_once 'Swift/Authenticator/LOGIN.php';
require_once 'Swift.php';
 
if ( isset( $_POST[ 'f' ] ) ) {
    $f = $_POST[ 'f' ];
    if ( isset( $f['smtp_user'] ) && !empty( $f['smtp_user'] ) ) {
        
    } else {
        $f['smtp_server'] = '';
    }
} else {
    $f['smtp_server'] = 'smtp.example.com';
    $f['smtp_port'] = '25';
    $f['smtp_user'] = 'admin@example.com';
    $f['smtp_password'] = '';
}
 
if ( isset( $_POST[ 'cmd' ] ) ) {
    $cmd = $_POST[ 'cmd' ];
} else {
    $cmd = '';
}
 
if ( $cmd == 'check' ) {
    $smtp_conn = &  new  Swift_Connection_SMTP( $f['smtp_server'] );
    
    if ( isset( $f['smtp_user'] ) && !empty( $f['smtp_user'] ) ) {
        $smtp_conn->setUsername( $f['smtp_user'] );
    }
    if ( isset( $f['smtp_password'] ) && !empty( $f['smtp_password'] ) ) {
        $smtp_conn->setPassword( $f['smtp_password'] );
    }
 
    if ( isset( $f['smtp_port'] ) && !empty( $f['smtp_port'] ) ) {
        $smtp_conn->setPort( $f['smtp_port'] );
    }
    
    $smtp_conn->attachAuthenticator(new Swift_Authenticator_LOGIN());
        
    $swift =& new Swift( $smtp_conn );
    echo '<p style="width: 350px; padding: 20px; border: 3px solid #cc5555">';
    var_dump( $swift->connect() );
    echo '</p>';
 
}
 
?>
<html>
<head>
</head>
<body>
<div style="padding-top: 20px;">
<form name="categories" action="./" method="post">
<input type="hidden" name="cmd" value="check"/> 
<table cellspacing="1" cellpadding="2" border="0" width="450" class="admin_table" summary="settings">
<tr>
    <td class="text_normal_9pt_left">SMTP Server:</td>
 
    <td class="text_normal_9pt_left"><input type="text" name="f[smtp_server]" value="<?php if (!empty($f['smtp_server'])) { echo( $f['smtp_server'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
    <td class="text_normal_9pt_left">SMTP Server Port:</td>
 
    <td class="text_normal_9pt_left"><input type="text" name="f[smtp_port]" value="<?php if (!empty($f['smtp_port'])) { echo( $f['smtp_port'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
    <td class="text_normal_9pt_left">SMTP Username:</td>
    <td class="text_normal_9pt_left"><input type="text" name="f[smtp_user]" value="<?php if (!empty($f['smtp_user'])) { echo( $f['smtp_user'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
<tr>
    <td class="text_normal_9pt_left">SMTP Password:</td>
    <td class="text_normal_9pt_left"><input type="password" name="f[smtp_password]" value="<?php if (!empty($f['smtp_password'])) { echo( $f['smtp_password'] ); };?>" maxlength="255" style="width: 250px;"/></td>
</tr>
 
<tr>
    <td class="text_normal_9pt_left"></td>
    <td class="text_normal_9pt_left"><input type="submit" name="check" value="Check" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>

Re: Test aborts upon failure

Posted: Thu Nov 27, 2008 2:54 pm
by John Cartwright
I havn't used Swift for awhile, but is a fatal erorr thrown or an Exception?

If you are using PHP4 exceptions do not exist yet, however if it's PHP5 then it will be much easier.

If an exception was infact thrown, you can wrap your authentication check within a try/catch to recover frmo the exception.

Code: Select all

 
try 
{
   //your swift authentication stuff goes here 
} 
catch (Name_Of_The_Exception $e) 
{
   echo 'Invalid login';
}
If your stuck with PHP4 and are getting an actual fatal error, you will have to play with set_error_handler()'

It would be helpful to see the error message.e

Re: Test aborts upon failure

Posted: Fri Nov 28, 2008 4:07 am
by hknight
Thank you! Your idea does the trick :-)