Home >Backend Development >PHP Tutorial >Sending emails via Socket under php win_PHP tutorial

Sending emails via Socket under php win_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:44:23839browse

Copy code The code is as follows:

/*
* php smtp send email Scoket class
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* @author zhoz
* v 1.72 update 2008/09/26 zhoz.Com
*/
class ZhozPhpSmtpSendMail {
//Your SMTP server provider, which can be a domain name or IP address
var $smtp = "";
//SMTP requires authentication, set the value to 1, and does not require authentication, the value is 0. Now most SMTP service providers require verification. If you are not sure, please contact your smtp service provider connect.
var $check = 1;
//Your email account name
var $username = "";
//Your email password
var $password = "";
//This email must be an email on the sending server
var $s_from = "";
/*
* Function: Send email initialization settings
* $from your sending email衿Is this your company?
* $password Your email password
* $smtp Your SMTP server provider, which can be a domain name or IP address
* $check SMTP requires authentication. Set the value to 1 No The authentication value is required to be 0. Most SMTP service providers now require verification
*/
function ZhozPhpSmtpSendMail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match(" /^[^d-_][w-]*[^-_]@[^-][a-zA-Zd-]+[^-](.[^-][a-zA-Zd-] *[^-])*.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this-> check = $check;
$this->s_from = $from;
}
}
/*
* Function: Send email
* $to target email address
* $from source email address
* $subject email title
* $message email content
*/
function zhoz_send ($to, $from, $subject, $message) {
// Connect to server
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "Failed to connect to server".__LINE__;
set_socket_blocking ($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "Error message 1:$lastmessage".__LINE__ ;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."rn";
else $lastact="HELO ".$yourname."rn";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr( $lastmessage,0,3) != 220 ) return "Error message 2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (emptyempty($lastmessage)) )
break;
}
//Authentication
if ($this-> ;check=="1") {
//Verification starts
$lastact="AUTH LOGIN"."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "Error message 3:$lastmessage".__LINE__;
//User name
$lastact= base64_encode($this->username)."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage, 0,3) != 334) return "Error message 4:$lastmessage".__LINE__;
//User password
$lastact=base64_encode($this->password)."rn";
fputs( $fp, $lastat);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "Error message 5:$ lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">rn";
fputs( $fp , $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "Error message 6:$lastmessage".__LINE__;
//TO:
$lastat="RCPT TO: <". $to ."> rn";
fputs( $fp, $lastat);
$lastmessage = fgets ($ fp,512);
if (substr($lastmessage,0,3) != 250) return "Error message 7:$lastmessage".__LINE__;
//DATA
$lastact="DATArn" ;
fputs($fp, $lastat);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "Error message 8 :$lastmessage".__LINE__;
//Processing Subject header
$head="Subject: $subjectrn";
$message = $head."rn".$message;
//Processing From header
$head="From: $fromrn";
$message = $head.$message;
//Processing To header
$head="To: $torn";
$message = $head.$message;
//Add the end string
$message .= "rn.rn";
//Send message
fputs($fp, $message) ;
$lastact="QUITrn";
fclose($fp);
error_log("send over------->ok!!", 0);
return true ;
}
}
?>
/*
* php smtp send mail Scoket class
* ZhozPhpSmtpSendMail.php
* Created on 2008/09/02 zhoz.com
* @author zhoz
* v 1.72 update 2008/09/26 zhoz.Com
*/
class ZhozPhpSmtpSendMail {
//Your SMTP server Supplier, which can be a domain name or IP address
var $smtp = "";
//SMTP requires authentication. Set the value to 1, and if authentication is not required, set the value to 0. Most SMTP service providers now require it. Verify. If you are not sure, please contact your SMTP service provider.
var $check = 1;
//Your email account name
var $username = "";
//Your email password
var $password = "";
//This email must be an email on the sending server
var $s_from = "";
/*
* Function: Sending initialization settings
* $from on your sending server Email
* $password Your email password
* $smtp Your SMTP server provider, which can be a domain name or IP address
* $check SMTP requires authentication. Set the value to 1. No authentication is required. The value is 0. Most SMTP service providers now require verification
*/
function ZhozPhpSmtpSendMail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[ ^d-_][w-]*[^-_]@[^-][a-zA-Zd-]+[^-](.[^-][a-zA-Zd-]*[^ -])*.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this->check = $ check;
$this->s_from = $from;
}
}
/*
* Function: Send email
* $to target email address
* $from Source email address
* $subject email title
* $message email content
*/
function zhoz_send ($to, $from, $subject, $message) {
//Connect to server
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "Failed to connect to server".__LINE__;
set_socket_blocking($fp , true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "Error message 1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."rn";
else $ lastact="HELO ".$yourname."rn";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage, 0,3) != 220 ) return "Error message 2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr( $lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//Authentication
if ($this->check= ="1") {
//Verification starts
$lastact="AUTH LOGIN"."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp ,512);
if (substr($lastmessage,0,3) != 334) return "Error message 3:$lastmessage".__LINE__;
//User name
$lastat=base64_encode($ this->username)."rn";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3 ) != 334) return "Error message 4:$lastmessage".__LINE__;
//User password
$lastact=base64_encode($this->password)."rn";
fputs( $ fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "Error message 5:$lastmessage". __LINE__;
}
//FROM:
$lastat="MAIL FROM: <". $this->s_from . ">rn";
fputs( $fp, $lastat );
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "Error message 6:$lastmessage".__LINE__;
/ /TO:
$lastat="RCPT TO: <". $to ."> rn";
fputs( $fp, $lalastact);
$lastmessage = fgets ($fp,512 );
if (substr($lastmessage,0,3) != 250) return "Error message 7:$lastmessage".__LINE__;
//DATA
$lastact="DATArn";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "Error message 8:$lastmessage ".__LINE__;
//Processing the Subject header
$head="Subject: $subjectrn";
$message = $head."rn".$message;
//Processing the From header
$head="From: $fromrn";
$message = $head.$message;
//Processing To header
$head="To: $torn";
$message = $head.$message;
//Add the end string
$message .= "rn.rn";
//Send message
fputs($fp, $message);
$lastat="QUITrn";
fclose($fp);
error_log("send over------->ok!!", 0);
return true;
}
}
?>
The test method is the same.
Run the code to view the code copy the code and print about 🎜>$from = 'zhoz@zhoz.com'; // Your email address on the sending server
$password = 'zhoz'; // Your email password
$smtp = 'smtp.zhoz. com'; // Your SMTP server provider, which can be a domain name or IP address
// Class initialization
$zhoz_obj = new ZhozPhpSmtpSendMail($from, $password, $smtp);
// Send Start
$status = $zhoz_obj->zhoz_send("zhoz008@126.com", "zhoz@zhoz.com", "subject is zhoz", "body is zhoz.com");
if( $status) {
echo $status;
echo "no---------";
} else {
echo "send ok--------- --------! ";
die;
}
// The result is of course: send ok-----------------!
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320576.htmlTechArticleCopy the code code as follows: ?php /* * php smtp send email Scoket class * ZhozPhpSmtpSendMail.php * Created on 2008 /09/02 zhoz.com * @author zhoz * v 1.72 update 2008/09/26 zhoz.Com */...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn