PHP win under the socket way to send mail class _php tips

Source: Internet
Author: User
Tags auth email account

Copy Code code as follows:

<?php
/*
* 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 vendor, which can be a domain name or IP address
var $smtp = "";
SMTP requires authentication setting of 1, no authentication value of 0, now most SMTP service providers to verify, if not clear please contact your SMTP service provider.
var $check = 1;
Name of your email account
var $username = "";
Your email password
var $password = "";
This email must be an email on the sending server
var $s _from = "";
/*
* Function: Letter initialization settings
* $from your letter  Jin 魃?
* $password Your email password
* $SMTP your SMTP server vendor, which can be a domain name or IP address
* $check SMTP requires authentication setting of 1 does not require an authentication value of 0, now most SMTP service providers have to verify
*/
function Zhozphpsmtpsendmail ($from, $password, $smtp, $check = 1) {
if (Preg_match ("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-za-z\d\-]+[^\-") (\.[ ^\-][a-za-z\d\-]*[^\-]) *\. [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 mail
* $to Target Mailbox
* $from Source Mailbox
* $subject Message headers
* $message Mail Content
*/
function Zhoz_send ($to, $from, $subject, $message) {
Connecting to a server
$fp = Fsockopen ($this->smtp, $errno, $ERRSTR, 60);
if (! $fp) return "Join Server failed". __line__;
Set_socket_blocking ($fp, true);
$lastmessage =fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 1: $lastmessage". __line__;
HELO
$yourname = "Yourname";
if ($this->check = = "1") $lastact = "EHLO". $yourname. " \ r \ n ";
else $lastact = "HELO". $yourname. " \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = = fgets ($fp, 512);
if (substr ($lastmessage, 0,3)!=) 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") {
Verify Start
$lastact = "AUTH LOGIN". \ r \ n ";
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). " \ r \ n ";
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). " \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!= "235") return "error message 5: $lastmessage". __line__;
}
From:
$lastact = "MAIL from: <". $this->s_from. ">\r\n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 6: $lastmessage". __line__;
To:
$lastact = "RCPT to: <". $to. " > \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 7: $lastmessage". __line__;
DATA
$lastact = "data\r\n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!= 354) return "error message 8: $lastmessage". __line__;
Handle Subject Head
$head = "Subject: $subject \ r \ n";
$message = $head. " \ r \ n ". $message;
Process from header
$head = "From: $from \ r \ n";
$message = $head. $message;
Handling to headers
$head = "to: $to \ r \ n";
$message = $head. $message;
Plus End string
$message. = "\r\n.\r\n";
Send a message
Fputs ($fp, $message);
$lastact = "quit\r\n";
Fclose ($FP);
Error_log ("Send over------->ok!!", 0);
return true;
}
}
?>
<?php
/*
* 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 vendor, which can be a domain name or IP address
var $smtp = "";
SMTP requires authentication setting of 1, no authentication value of 0, now most SMTP service providers to verify, if not clear please contact your SMTP service provider.
var $check = 1;
Name of your email account
var $username = "";
Your email password
var $password = "";
This email must be an email on the sending server
var $s _from = "";
/*
* Function: Letter initialization settings
* $from the mailbox on your sending server
* $password Your email password
* $SMTP your SMTP server vendor, which can be a domain name or IP address
* $check SMTP requires authentication setting of 1 does not require an authentication value of 0, now most SMTP service providers have to verify
*/
function Zhozphpsmtpsendmail ($from, $password, $smtp, $check = 1) {
if (Preg_match ("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-za-z\d\-]+[^\-") (\.[ ^\-][a-za-z\d\-]*[^\-]) *\. [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 mail
* $to Target Mailbox
* $from Source Mailbox
* $subject Message headers
* $message Mail Content
*/
function Zhoz_send ($to, $from, $subject, $message) {
Connecting to a server
$fp = Fsockopen ($this->smtp, $errno, $ERRSTR, 60);
if (! $fp) return "Join Server failed". __line__;
Set_socket_blocking ($fp, true);
$lastmessage =fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 1: $lastmessage". __line__;
HELO
$yourname = "Yourname";
if ($this->check = = "1") $lastact = "EHLO". $yourname. " \ r \ n ";
else $lastact = "HELO". $yourname. " \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = = fgets ($fp, 512);
if (substr ($lastmessage, 0,3)!=) 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") {
Verify Start
$lastact = "AUTH LOGIN". \ r \ n ";
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). " \ r \ n ";
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). " \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!= "235") return "error message 5: $lastmessage". __line__;
}
From:
$lastact = "MAIL from: <". $this->s_from. ">\r\n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 6: $lastmessage". __line__;
To:
$lastact = "RCPT to: <". $to. " > \ r \ n ";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message 7: $lastmessage". __line__;
DATA
$lastact = "data\r\n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!= 354) return "error message 8: $lastmessage". __line__;
Handle Subject Head
$head = "Subject: $subject \ r \ n";
$message = $head. " \ r \ n ". $message;
Process from header
$head = "From: $from \ r \ n";
$message = $head. $message;
Handling to headers
$head = "to: $to \ r \ n";
$message = $head. $message;
Plus End string
$message. = "\r\n.\r\n";
Send a message
Fputs ($fp, $message);
$lastact = "quit\r\n";
Fclose ($FP);
Error_log ("Send over------->ok!!", 0);
return true;
}
}
?>
Test the same way.
Run Code View Code copy code print about
<?php
/**
* zhozphpsmtpsendmail.php
* Created on 2008/09/02 zhoz.com
* PHP smtp Send mail class
* Instead of mail (): Because it's an error, I don't want to
*/
Here Configure the sender server information, if not changed can also be used normally:
$from = ' zhoz@zhoz.com '; Your email address on the server
$password = ' zhoz '; Your e-mail password
$SMTP = ' smtp.zhoz.com '; Your SMTP server vendor, which can be a domain name or IP address
Class is initialized with the
$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-----------------!
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.