| ★ wanayoo — archive 1999 http://phpbuilder.com/columns/tim19990221.php3 | Nouvelle recherche | Portail wanayoo |
|
|
|||
|
|
|
|
||
|
|
|
Sending Mail With PHP3Tim Perdue
Yesterday (1999-02-20), I decided to give email a whirl with PHP3. I needed to have a customized "thank you" page after sending the mail - like the rest of geocrawler.com and gotocity.com, the response page had to be co-brandable and customizable. No problem for PHP. I started my research by visiting the Sample Code Archive. I found some awesome code by Peter Kovacs that can take a couple parameters and then fire off the email (it uses sendmail on UNIX). This little bit of code isn't even required if you read the manual, and look for the mail() function, which is built into PHP3, but I'll show it anyway for demo purposes. Peter Kovac's Code
<?php
function send_mail($to_address, $from_address, $subject, $message) {
$path_to_sendmail = "/usr/lib/sendmail";
$fp = popen("$path_to_sendmail -t", "w");
$num = fputs($fp, "To: $to_address\n");
$num += fputs($fp, "From: $from_address\n");
$num += fputs($fp, "Subject: $subject\n\n");
$num += fputs($fp, "$message");
pclose($fp);
if ($num > 0) {
return 1;
} else {
return 0;
}
}
?>
I also wound up finding some really cool code by Jon Stevens that can take an email address and verify that it can actually receive the email. So if you want to get into some really serious form validation, you can (I didn't go that far yet, but will soon). Jon Stevens' Code
<?php
/*
By: Jon S. Stevens jon%40clearink.com
Copyright 1998 Jon S. Stevens, Clear Ink
This code has all the normal disclaimers.
It is free for any use, just keep the credits intact.
It will return an array. $return[0] is whether it was a valid
email or not and $return[1] is the SMTP error message if there
was one.
*/
function validateEmail ( $email )
{
global $SERVER_NAME;
$return = array ( false, "" );
list ( $user, $domain ) = split ( "@", $email, 2 );
$arr = explode ( ".", $domain );
$count = count ( $arr );
$tld = $arr[$count - 2] . "." . $arr[$count - 1];
if ( checkdnsrr ( $tld, "MX" ) )
{
if ( getmxrr ( $tld, $mxhosts, $weight ) )
{
for ( $i = 0; $i < count ( $mxhosts ); $i++ )
{
$fp = fsockopen ( $mxhosts[$i], 25 );
if ( $fp )
{
$s = 0;
$c = 0;
$out = "";
set_socket_blocking ( $fp, false );
do
{
$out = fgets ( $fp, 2500 );
if ( ereg ( "^220", $out ) )
{
$s = 0;
$out = "";
$c++;
}
else if ( ( $c > 0 ) && ( $out == "" ) )
{ break; }
else
{ $s++; }
if ( $s == 9999 ) { break; }
} while ( $out == "" );
set_socket_blocking ( $fp, true );
fputs ( $fp, "HELO $SERVER_NAME\n" );
$output = fgets ( $fp, 2000 );
fputs ( $fp, "MAIL FROM: <info@" . $tld . ">\n" );
$output = fgets ( $fp, 2000 );
fputs ( $fp, "RCPT TO: <$email>\n" );
$output = fgets ( $fp, 2000 );
if ( ereg ( "^250", $output ) ) {
$return[0] = true;
} else {
$return[0] = false;
$return[1] = $output;
}
fputs ( $fp, "QUIT\n" );
fclose( $fp );
if ( $return[0] == true )
{ break; }
}
}
}
}
return $return;
}
?>
Basically, you can receive some parameters from a simple form (you can probably handle creating that. If not - take a look at geocrawler.com and steal the HTML from the mail archive - you have my blessing).
<?PHP
//$to_email_address - received from the "POST"
//$from_email_address - ditto
//$subject
//$message
//
//First, validate the addresses using Jons' function
//
$to_email_array=validateEmail ( $to_email_address );
$from_email_array=validateEmail ( $from_email_address );
//
//Second, test the results and send the message or display
//an error
//
if (( $to_email_array[0]) && ($from_email_array[0])) {
send_mail($to_email_address, $from_email_address, $subject, $message);
} else {
//DISPLAY AN ERROR
echo "error";
echo $to_email_array[1];
echo $from_email_array[1];
}
?>
And you're done... That same little task, were I using $1000 dollar products like Lasso would have required me to install extra software (javamail API + the JDK if I didn't already have it), and would have taken tons of extra code and some Lasso-specific libraries. Care to guess where I stand on the free software movement?
|
||
|
|
|
Sample Code | Columns | Mail Archive | Support | Get Started! | Links | Contribute!
Contact (non support questions) By viewing these pages you agree to the Legal Terms of Service.
Geocrawler.com | GoToCity.com | DirectriCity.com | PHPBuilder.com | The Des Moines City.net |