#native_company# #native_desc#
#native_cta#

Sending Mail With PHP3

By Tim Perdue
on July 30, 2000

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 ("@"$email2);

    
$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 ($fpfalse);

                    do {

                        
$out fgets ($fp2500);

                        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 ($fptrue);

                    fputs ($fp"HELO $SERVER_NAMEn");

                    
$output fgets ($fp2000);

                    
fputs ($fp"MAIL FROM: <info@" $tld ">n");

                    
$output fgets ($fp2000);

                    
fputs ($fp"RCPT TO: <$email>n");

                    
$output fgets ($fp2000);

                    if (
ereg ("^250"$output)) {

                        
$return[0] = true;

                    } else {

                        
$return[0] = false;

                        
$return[1] = $output;

                    }

                    
fputs ($fp"QUITn");

                    
fclose($fp);

                    if ($return[0] == true) {

                        break;

                    }

                }

            }

        }

    }

    return 
$return;

}

?>