Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Thursday, June 5, 2008

Sending an email from PHP (on Windows too.)

Yey, finally got something that really works.
It's not my stuff, but I was able to found it after a long time search!
Here is the link to sourceForge.net, to the page that hosts php Mailer for PHP 5 for Windows, and looks like for Unix too. If the link does not work then search for the word phpMailer.

The easiest way to install the stuff is to do as follows (example on Windows, with assumption that you have your php installed in the c:\php directory.)

> mkdir c:\php\mailer
Unzip phpMailer_v2.1.zip into c:\php\mailer
> notepad.exe c:\php\php.ini
Add at the very end:
include_path=".;c:\php\mailer"

Youre done!

Now, example:
>
<?php
require_once( "class.phpmailer.php" ) ;

$mail = new PHPMailer() ;
$mail->IsSMTP() ;
$mail->Host = "smtp.myserver.com:587" ;
$mail->SMTPAuth = true ;
$mail->Username = "username" ;
$mail->Password = "password" ;
$mail->From = "me@myserver.com" ;
$mail->FromName = "It is me!" ;
$mail->AddAddress( "testaddress@theirserver.com", "Poor Receiver" ) ;
$mail->AddReplyTo( "me@myserver.com", "do-not-reply" ) ;
$mail->AddAttachment( "c:\\mydog.jpg", "Isn't she cute?" ) ;
$mail->Subject = "Sorry for this test email!" ;
$mail->IsHTML( true ) ;
$mail->Body = file_get_contents( "c:\\myemail.html" ) ;
$mail->AltBody = "non-HTML mail client? Where are you from?" ;

if( !$mail->Send())
printf( "Sending Error: " . $mail->ErrorInfo . "\n" ) ;
else
printf( "Message has been sent successfully.\n" ) ;
?>

Saturday, May 24, 2008

Running a command for each value comming back from an ODBC query in PHP

I have a need to read an ODBC datasource and run a query on a particual table. For each unique value of the table, I need to run a command in the shell.

Here is anexample:


<?php
$values = Array() ;
$command = "echo" ; /* for the example purposes the command is echo */
$columnName = "state" ; /* just for this example */

$hdbc = odbc_connect ( "dsn-name", NULL, NULL ) ;
if ( $hdbc != NULL )
{
$hstmt = odbc_prepare(
$hdbc
, "select distinct " . $columnName . " from tablename"
) ;
if ( $hstmt != NULL )
if ( odbc_execute( $hstmt ))
{
while ( odbc_fetch_row( $hstmt ))
$values[] = odbc_result( $hstmt, 1 ) ;
}
else
printf( "*** Unable to execute\n" ) ;
else
printf( "*** Unable to prepare the query.\n" ) ;

odbc_close( $hdbc ) ;
}
else
printf( "*** Unable to connect\n" ) ;

foreach ( $values as $value )
system( $command . " \"" . $value . "\"" ) ;
?>

Tuesday, May 13, 2008

Setting up registry to run .php scripts from command line

I wanted to have a script in php that I could run in the following way:

c:\tmp> myscript.php argument1 "second argument"

This is what I came up to:

a) In the file explorer (not internet! file!) double click on your .php script, select the 'Select the program from a list', Browse to php.exe, mark 'always use the selected program to open this kind of file', and accept.

This, will create a HKEY_CLASS_ROOT/.php entry with the value php_auto_file.
It will also create the HKEY_CLASS_ROOT/php_auto_file/shell/open/command with the default entry: "c:\php\php.exe" "%1"
(the path to the php.exe file depends on where you have installed php in!)

This kind of registration will allow you now running your scripts from the command line but it WON'T allow you to pass any arguments.

b) That's why you have to the HKEY_CLASS_ROOT/php_auto_file/shell/open/command entry to the following: "c:\php\php.exe" "%1" %*

Here you go. Now you can prepare your myscript.php to look like this, and test the execution:
print_r( $argv ) ;
?>

myscript.php argument1 "second argument"

Result:

Array
(
[0] => c:\tmp\myscript.php
[1] => argument1
[2] => second argument
)