Linux Systems Administrator
Jupitermedia
US-AZ-Tucson

Justtechjobs.com Post A Job | Post A Resume

Sending Mail With PHP3
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 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

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;
}

?>
[ Next Page ]


Comments:
RE: Send Survey Results via Emailindah puspita02/14/05 04:08
RE: Server Errorshabbir11/06/02 06:47
RE: Can i connect to ...Hippo10/21/02 14:23
Send Survey Results via EmailPushpinder Singh10/15/02 13:54
send mailteddy10/15/02 05:20
Can i connect to ...Andrei10/11/02 10:41
RE: harrasmentcaxi09/04/02 08:15
subsequent mail() problemAkhmad D. Sembiring08/31/02 04:25
MINOR ERRORAndrew Patel08/27/02 21:08
Server Error Solution for w2kOtaru-sama08/22/02 11:55
harrasmentsean08/13/02 16:38
harrasmentsean08/13/02 16:36
Server ErrorRauj08/08/02 02:50
RE: Server ErrorCari08/06/02 19:52
RE: attachment on mail() functionRaj08/05/02 19:08
RE: how to send array of values with mail()Os2Al07/23/02 22:16
how to send array of values with mail()luke woollard07/16/02 05:22
'Failed to Connect' ERRORMirza Claudiu Sebastian06/05/02 07:53
Attachments in php3Srikanth05/25/02 15:52
RE: Server ErrorAlex Elderson05/24/02 08:45
this doesn't work ...beer05/23/02 11:13
mail() function warning: mail not....Pejus05/17/02 05:41
Sending email to @AOL.COM addressesCotton05/09/02 12:43
RE: attachment through smtp in php3P P Roday04/13/02 03:35
RE: 'Failed to Connect' ErrorSabrina Markon04/03/02 19:55
RE: attachment through smtp in php3faty04/03/02 11:09
RE: How do I configure the outgoing mail serverdenis kakooza03/19/02 11:36
RE: from address in emaildenis kakooza03/19/02 11:27
Numbering multipart mail partsdenis kakooza03/19/02 10:50
mail problemaasim03/10/02 15:22
from address in emailvenkat02/21/02 00:58
MySQL Connection FailedCHARLES02/19/02 06:35
i download horde & imp but i cant fix webmailraja02/13/02 16:22
HelloAor Chucha01/16/02 11:10
Generating documentation for php code!Ahmed01/15/02 02:20
Some pointers...John Kant01/08/02 14:48
bab use imap_openpham trung dung12/28/01 09:09
is this useful?Mr.Q12/19/01 03:24
RE: disccusion forum code!!!!Craig12/10/01 11:57
Attachment's File NameSteven Chalker12/08/01 08:46
Email FormationMike Discenza11/29/01 15:27
sending '1' e-mail w/ multiple rcpt'sNeal10/29/01 13:57
relaying deniedbubi10/29/01 04:52
How do I configure the outgoing mail serverJon10/26/01 15:39
disccusion forum code!!!!simba10/18/01 10:29
RE: AOL E-mailsMEMEyou10/18/01 08:04
RE: AOL E-mailsAdam10/04/01 14:59
wanttoknowsdf09/20/01 13:25
RE: attachment through smtp in php3homena08/31/01 05:14
HyperlinkGrami08/28/01 01:01
HyperlinkGrami08/28/01 01:00
Sophea: Coding for receive mail from serverSophea Heang08/21/01 04:53
"this"nick08/19/01 22:04
RE: same as in php4?yowen07/30/01 01:13
mail()Alcides07/26/01 08:59
PHP Builder is great!Kudos to you07/24/01 09:33
test phpsatter07/21/01 09:47
Server ErrorTom Connor07/18/01 17:42
Server ErrorTom Connor07/18/01 08:18
Server ErrorTom Connor07/18/01 08:15
RE: AOL E-mailsjuanito07/16/01 02:42
RE: commentsDan Ludwig07/03/01 07:05
Need Help about mail functionsaeed ahmd tariq06/28/01 23:46
RE: AOL E-mailsCyrus Chvala06/23/01 15:58
same as in php4?mej!A06/22/01 13:44
RE: attachment through smtp in php3anwar05/21/01 06:19
how to send image by mailsanjay05/21/01 05:54
attching extra header to datahow could i attach extra header with body 05/11/01 10:38
RE: attachment through smtp in php3Josh04/16/01 10:11
commentsarun04/10/01 02:20
.com & .edu, etc., but what about .tv or .fm?Dan Ludwig04/02/01 14:38
your exampleVictor03/30/01 16:34
Unexpected character appendedBhavesh Goradia03/28/01 07:22
RE: AOL E-mailsTanner03/28/01 03:26
envelope "from" is not the same as message "foddfish03/15/01 07:46
Bad Message destinationbrandon02/25/01 04:03
RE: Dummy, toofallus02/17/01 03:03
RE: 'Failed to Connect' ErrorYiannis Mavroukakis02/12/01 04:46
attachment through smtp in php3sachin shah02/07/01 01:53
Dummy, tooOlli02/02/01 11:24
RE: dummybmpc01/11/01 17:57
dummyEmiel01/08/01 05:31
RE: AOL E-mailsMario Garcio12/14/00 13:47
RE: Warning: Failed to Receive...Mika Ylinen12/10/00 15:27
Warning: Failed to Receive...John10/11/00 18:58
Where is this in "shared"? (AOL???)Peter Huestis08/28/00 12:17
AOL E-mailsSpencer D. Mindlin08/03/00 18:01
RE: 'Failed to Connect' ErrorCédric CHERCHI08/01/00 11:48
'Failed to Connect' ErrorErrol Siegel06/28/00 10:45
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.