Server scripting languages like Lasso and Java servlets make sending mail
so complicated that you'd rather avoid it at all costs. Until recently,
I have been relying on a cheap (read "free") perl script from
cgi-resources.com, but
I want to be able to do this from PHP.
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 wound up finding some really cool code by
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
function validateEmail ($email) {
global $SERVER_NAME;
$return = array (false, "");
list ($user, $domain) = split ("@", $email, 2);
$arr = explode (".", $domain);
$count = count ($arr);
## Here starts the modification (E.Soysal)
if (($count> 2) and ($arr[$count - 2]=='com' or $arr[$count - 2]=='org' or
$arr[$count - 2]=='net' or $arr[$count - 2]=='edu' or
$arr[$count - 2]=='mil' or $arr[$count - 2]=='k12')) {
$tld = $arr[$count - 3].".".$arr[$count - 2] . "." . $arr[$count - 1];
} else {
## End of modification
$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])) {
mail($to_email_address,$subject,$message,'FROM: '.$from_email_address);
} 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?