PHP online mail send class, sent via SMTP

Source: Internet
Author: User
Tags mail php online require requires domain email account
?
/*
* Mail Send Class
* Author: More than a rookie
* Contact Email: KINGERQ at MSN DOT com
* Date Created: 2005-08-19
* Note: This category for the modified, forget the source of
*/
Class Smail {
Your SMTP server vendor, which can be a domain name or IP address
var $smtp = "smtp.163.com";
SMTP requires authentication setting of 1 does not require an authentication value of 0, now most of the SMTP service provider 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 Smail ($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 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:". $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 $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 $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 $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 $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 $lastmessage". __line__;

To:
$lastact = "RCPT to: $to". "\ r \ n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!=) return "error message $lastmessage". __line__;

DATA
$lastact = "data\r\n";
Fputs ($fp, $lastact);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0,3)!= 354) return "error message $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";

Fputs ($fp, $lastace);
Fclose ($FP);
return 0;
}
}
/* Send sample
$SM = new Smail ("Username @163.com", "Password", "smtp.163.com");
$end = $sm->send ("Destination mailbox", "Source Mailbox", "This is the title", "This is the Mail content");
if ($end) echo $end;
else echo "Send success!" ";
*/
?>

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.