| PHP implementation of the SMTP send mail function, with the SMTP authentication function, with detailed comments, suitable for the study of SMTP letter friend reference. The complete code is as follows.
". $lb," "," "MAIL from Error:"); 6, send rcpt to. Expect to return $SMTP [] = Array ("RCPT to: <". $to. " > ". $lb," "," RCPT to Error: "); 7, send data, expect to return 354 $SMTP [] = Array ("Data". $LB, "354", "Data error:"); 8.0. Send from $smtp [] = Array ("From:" $deliver. $lb, "", ""); 8.2. Send to $SMTP [] = Array ("To:" $to. $lb, "", ""); 8.1. Send title $SMTP [] = Array ("Subject:". $subject. $lb, "", "" "); 8.3. Send Other header contents foreach ($hdr as $h) {$SMTP [] = Array ($h. $lb, "", "");} 8.4. Send a blank line, end the header to send $smtp [] = Array ($lb, "", ""); 8.5. Send the message subject if ($bdy) {foreach ($bdy as $b) {$SMTP [] = Array (Base64_encode ($b. $lb). $lb, "", "");}} 9, send "." Indicates the end of the letter, expecting to return a $smtp [] = Array ("."). $LB, "+", "DATA (end) Error:"); 10. Send quit, exit, expect to return 221 $smtp [] = Array ("Quit". $lb, "221", "Quit Error:"); Open the SMTP server port $fp = @fsockopen ($smtp _host, 25); if (! $fp) echo "Error:Cannot conect to ". $smtp _host." "; while ($result = @fgets ($fp, 1024x768)) {if (substr ($result, 3,1) = = "") {Break;}} $result _str= ""; Send command/data in SMTP array foreach ($SMTP as $req) {//Send information @fputs ($fp, $req [0]); If you need to receive server return information, the IF ($req [1]) {//Receive information while ($result = @fget S ($fp, 1024x768)) {if (substr ($result, 3,1) = = "") {break;}}; if (!strstr ($req [1],substr ($result, 0,3)) {$result _str.= $req [2]. $result. " "; }}}//Close connection @fclose ($FP); return $result _str;} Call Example #echo send_mail ($_post["email"], $tosubject, $_post["ErrorText"]); Echo send_mail (' mail_001@126.com ', ' Scripting Academy, Welcome everyone's presence ', ' the Programmer's House _bbs.it-home.org '); echo time ();? > |