" class="title" title="home again woohoo">Home ?> pages bg right



SMTP Auth

Well…

Sometimes i’ve been in the need to test my mail server auth feature (smtp).

Doing it using plain password is a little insecure. So you need to use some encoding. Most usual is base 64.

The way for computing the auth string to send the mailserver, is joining both values (user and password) using a “0″ char as glue (not the number 0, the character 0).

Even easier (and lazier), using this script :

<?

# Use the script as php auth.php user password
$user  = $argv[1];
$pass  = $argv[2];
$unenc = implode("\000",array($user,$pass));

echo "AUTH LOGIN " . base64_encode("$unenc") . "\n";
echo "AUTH PLAIN " . base64_encode("\000$unenc") . "\n";

?>

Using this script, and trying an user/pass combo, would give :

# php auth.php user1@domain.com password1
AUTH LOGIN dXNlcjFAZG9tYWluLmNvbQBwYXNzd29yZDE=
AUTH PLAIN AHVzZXIxQGRvbWFpbi5jb20AcGFzc3dvcmQx

This, of course, is using command line.



How to know what killled our script

Many times, working on servers we don’t own, we might find our script just died and said nothing… (one might expect a Wilhelm scream at least).

Well… Easy solution. Just enable display_errors on the server.. what ? you can’t ? ok… Then, use ini_set to enable it.

That should do it. But wait… what if you don’t want all the world know your script is screwing something up ? Or what if your script died just because, and you need to know right away ? (i.e. A constantly running process)

Well, then you can register a shutdown function (Php 4, and 5). What is it ? Exactly what the name says. Whenever the script “shutdowns” , this function will execute.

<?
  $res = register_shutdown_function('shutdown');

  function shutdown()
  {
      if ($error = @error_get_last())
      if (isset($error['type']) && ($error['type'] == E_ERROR || $error['type'] == E_PARSE || $error['type'] == E_COMPILE_ERROR))
      echo "Fatal Error, ending program:\n\nType    : {$error['type']}\nMessage : {$error['message']}: \nFile    : {$error['file']}:\nLine    : {$error['line']}\n");
  }
?>

That’s it.

A simple shutdown function, that will check for the last error (assuming the script died because of one) and if that error was fatal, print which error, what message it gave, and in which program it happened.

Then you can forward to your database… mail…. sms…. or just ignore it.

Note : When this function is called, the script already sent headers…

Page 4 of 5«12345»