SMTP protocol-php Examples of mail-sending programs

Source: Internet
Author: User
Tags chr ereg mail printf socket

Class Zsmailbox
{
var $fpSocket;
var $strLog;
var $strSMTPServer;
var $strSMTPPort;
var $strFrom;
var $strTo;
function Zsmailbox ()
{
$this->strlog = "";
$this->strsmtpport = "25";
$this->strfrom = "";
$this->strto = "";
}
function DoCommand ($strCommand, $strOKReply)
{
Fputs ($this->fpsocket, $strCommand);
$strReply = fgets ($this->fpsocket, 512);
if (! Ereg ("^ $strOKReply", $strReply))
{
return false;
}
return true;
}
function waitreply ($strOKReply)
{
$strReply = fgets ($this->fpsocket, 512);
$this->strlog. = "Reply: $strReply". "
n ";
if (! Ereg ("^ $strOKReply", $strReply))
{
return false;
}
return true;
}
function SendData ($strSubject, $strContent)
{
$str = "To:". $this->strto. Chr (13). Chr (10);
$str. = "From:". $this->strfrom. Chr (13). Chr (10);
$str. = "Subject:". $strSubject. Chr (13). Chr (10). Chr (13). Chr (10);
$str. = $strContent;
$str. = Chr (13). Chr (10). "." . Chr (13). Chr (10);
Fputs ($this->fpsocket, $STR);
return true;
}
function SendMail ($strSubject, $strContent)
{
if ($this->verifysmtpvariables ())
{
$this->strlog = "";
} else
{
$this->strlog = "Any of the SMTP variables are invaild
n ";
return false;
}
$this->fpsocket = Fsockopen ($this->strsmtpserver, $this->strsmtpport);
if (! $this->fpsocket)
{
$this->strlog. = "Cann ' t open socket
n ";
return false;
}
$this->strlog. = "Open socket succeed
n ";
if (! $this->waitreply ("220"))
{
Fclose ($this->fpsocket);
return false;
}
$strCommand = "HELO". $this->strsmtpserver. "N";
if (! $this->docommand ($strCommand, "250"))
{
$this->strlog. = "HELO error
n ";
Fclose ($this->fpsocket);
return false;
}
$strCommand = "MAIL from: <". $this->strto. ">n";
if (! $this->docommand ($strCommand, "250"))
{
$this->strlog. = "MAIL error
n ";
Fclose ($this->fpsocket);
return false;
}
$strCommand = "RCPT to: <". $this->strto. ">n";
if (! $this->docommand ($strCommand, "250"))
{
$this->strlog. = "Refuse to establish the channel
n ";
Fclose ($this->fpsocket);
return false;
}
$strCommand = "Datan";
if (! $this->docommand ($strCommand, "354"))
{
$this->strlog. = "Refuse to recieve the data
n ";
Fclose ($this->fpsocket);
return false;
}
if (! $this->senddata ($strSubject, $strContent))
{
$this->strlog. = "Send data error
n ";
Fclose ($this->fpsocket);
return false;
}
if (! $this->waitreply ("250"))
{
$this->strlog. = "Send Data unsuccessful
n ";
Fclose ($this->fpsocket);
return false;
}
$strCommand = "QUITN";
if (! $this->docommand ($strCommand, "221"))
{
$this->strlog. = "QUIT error
n ";
Fclose ($this->fpsocket);
return false;
}
Fclose ($this->fpsocket);
return true;
}
function Verifymailaddr ($STRMAILADDR)
{
Return (eregi ("^[_.0-9a-z-]+@") ([0-9a-z][0-9a-z-]+.) +[a-z]{2,3}$ ", $strMailAddr));
}
function Verifywebsiteaddr ($STRWEBSITEADDR)
{
Return (eregi ("^" [_0-9a-z-]+.) +[a-z]{2,3}$ ", $strWebSiteAddr));
}
function Verifysmtpvariables ()
{
if (! $this->verifywebsiteaddr ($this->strsmtpserver))
return false;
if (! isset ($this->strsmtpport))
return false;
if (! $this->verifymailaddr ($this->strfrom))
return false;
if (! $this->verifymailaddr ($this->strto))
return false;
return true;
}
function Setsmtpvariables ($strServer, $strPort, $strFrom, $strTo)
{
if (Isset ($strServer))
$this->strsmtpserver = $strServer;
if (Isset ($strPort))
$this->strsmtpport = $strPort;
if (Isset ($strFrom))
$this->strfrom = $strFrom;
if (Isset ($strTo))
$this->strto = $strTo;
}
}
?>
if (Isset ($strTo) && ($strTo!= ""))
{
if (Isset ($nServer))
{
Switch ($nServer)
{
Case 1: $strSMTPServer = "smtp.sina.com.cn";
$strSMTPPort = "25";
$strTo. = "@sina. com";
Break
Case 2: $strSMTPServer = "Smtp.163.net";
$strSMTPPort = "25";
$strTo. = "@163.net";
Break
Case 3: $strSMTPServer = "Smtp.yeah.net";
$strSMTPPort = "25";
$strTo. = "@yeah. NET";
Break
Case 4: $strSMTPServer = "smtp.netease.com";
$strSMTPPort = "25";
$strTo. = "@netease. com";
Break
Case 5: $strSMTPServer = "smtp.sohu.com";
$strSMTPPort = "25";
$strTo. = "@sohu. com";
Break
Case 6: $strSMTPServer = "Smtp.263.net";
$strSMTPPort = "25";
$strTo. = "@263.net";
Break
Default: $strSMTPServer = "smtp.sina.com.cn";
$strSMTPPort = "25";
$strTo = "guestxyz@sina.com";
Break
}
}
if (! isset ($strFrom) | | ($strFrom = = ""))
{
$strFrom = $strTo;
}
if (! isset ($strSubject) | | ($strSubject = = ""))
{
$strSubject = "No subject";
}
if (! isset ($strContent) | | ($strContent = = ""))
{
$strContent = "No content";
}
$ZSMB = new Zsmailbox;
$ZSMB->setsmtpvariables ($strSMTPServer, $strSMTPPort, $strFrom, $strTo);
if ($ZSMB->sendmail ($strSubject, $strContent))
{
printf ("OK");
} else
{
printf ($zsmb->strlog);
}
}
?>

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.