欢迎光临
我们一直在努力

POP3、SMTP邮件收发程序-PHP教程,PHP应用

建站超值云服务器,限时71元/月

<?php
if ($email_inc) return;
$email_inc= “defined”;
define( “smtpport”,25);

class pop3 {
var $subject; // 邮件主题
var $from_email; // 发件人地址
var $from_name; // 发件人姓名
var $to_email; // 收件人地址
var $to_name; // 收件人姓名
var $body; // 邮件内容
var $filename; // 文件名
var $socket; // 当前的 socket
var $line;
var $status;

function pop3_open($server, $port)
{

$this->socket = fsockopen($server, $port);
if ($this->socket <= 0){
return false;
}
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return false;
return true;
}

function pop3_user($user)
{

if ($this->socket < 0){
return false;
}
fputs($this->socket, “user $this->userrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return false;

return true;
}

function pop3_pass( $pass)
{

fputs($this->socket, “pass $passrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

return 1;
}

function pop3_stat()
{

fputs($this->socket, “statrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

if (!eregi( “+ok (.*) (.*)”, $this->line, $regs))
return 0;

return $regs[1];
}

function pop3_list()
{
fputs($this->socket, “listrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

$i = 0;
while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> “.”)
{
$articles[$i] = $this->line;
$i++;
}
$articles[ “count”] = $i;

return $articles;
}

function pop3_retr($nr)
{

fputs($this->socket, “retr $nrrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> “.”)
{
$data[$i] = $this->line;
$i++;
}
$data[ “count”] = $i;

return $data;
}

function pop3_dele( $nr)
{

fputs($this->socket, “dele $nrrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;
return 1;
}

function pop3_quit()
{

fputs($this->socket, “quitrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

return 1;
}
}

class smtp {

var $subject; // string the emails subject
var $fromname; // string senders name (opt)
var $toname; // string recipients name (opt)
var $body; // string body copy
var $attachment; // attachment (optional)
var $attachmenttype;
var $socket;
var $line;
var $status;

function smtp($server = “localhost”,$port = smtpport)
{
return $this->open($server, $port);
}

function smtpmail($fromemail, $fromname, $toemail, $toname, $subject, $body, $attachment=null, $attachmenttype= “text”)
{
$this->subject = $subject;
$this->toname = $toname;

$this->fromname = $fromname;
$this->body = $body;

$this->attachment = $attachment;
$this->attachmenttype = $attachmenttype;

if ($this->helo() == false){
return false;
}
if ($this->mailfrom($fromemail) == false){
return false;
}
if ($this->rcptto($toemail) == false){
return false;
}
if ($this->body() == false){
return false;
}
if ($this->quit() == false){
return false;
}
}

function open($server, $port)
{

$this->socket = fsockopen($server, $port);
if ($this->socket < 0) return false;

$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function helo()
{
if (fputs($this->socket, “helorn”) < 0 ){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function ehlo()
{

/* well, lets use “helo” for now.. until we need the
extra funcs [unk]
*/
if(fputs($this->socket, “helo localhostrn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function mailfrom($fromemail)
{

if (fputs($this->socket, “mail from: <$fromemail>rn”)<0){
return false;
}

$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function rcptto($toemail)
{

if(fputs($this->socket, “rcpt to: <$toemail>rn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;
return true;
}

function body()
{
$filesize = 0;
$attachment = null;
$fp = null;

$buffer = sprintf( “from: %srnto:%srnsubject:%srn”, $this->fromname, $this->toname, $this->subject);

if(fputs($this->socket, “datarn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “3”) return false;

if(fputs($this->socket, $buffer)<0){
return false;
}

if ($this->attachment == null){

if(fputs($this->socket, “mime-version: 1.0rncontent-type: text/plain; charset=iso-8859-1rncontent-transfer-encoding: 7bitrnrn”)<0){
return false;
}
if(fputs($this->socket, “$this->bodyrnrn”)<0){
return false;
}

if(fputs($this->socket, “.rn”)<0){
return false;
}

$this->
pop3、smtp邮件收发程序

 发表时间: 2001年09月28日 阅读次数: 18 推荐给朋友 打印本页

<?php
if ($email_inc) return;
$email_inc= “defined”;
define( “smtpport”,25);

class pop3 {
var $subject; // 邮件主题
var $from_email; // 发件人地址
var $from_name; // 发件人姓名
var $to_email; // 收件人地址
var $to_name; // 收件人姓名
var $body; // 邮件内容
var $filename; // 文件名
var $socket; // 当前的 socket
var $line;
var $status;

function pop3_open($server, $port)
{

$this->socket = fsockopen($server, $port);
if ($this->socket <= 0){
return false;
}
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return false;
return true;
}

function pop3_user($user)
{

if ($this->socket < 0){
return false;
}
fputs($this->socket, “user $this->userrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return false;

return true;
}

function pop3_pass( $pass)
{

fputs($this->socket, “pass $passrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

return 1;
}

function pop3_stat()
{

fputs($this->socket, “statrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

if (!eregi( “+ok (.*) (.*)”, $this->line, $regs))
return 0;

return $regs[1];
}

function pop3_list()
{
fputs($this->socket, “listrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

$i = 0;
while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> “.”)
{
$articles[$i] = $this->line;
$i++;
}
$articles[ “count”] = $i;

return $articles;
}

function pop3_retr($nr)
{

fputs($this->socket, “retr $nrrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

while (substr($this->line = fgets($this->socket, 1024), 0, 1) <> “.”)
{
$data[$i] = $this->line;
$i++;
}
$data[ “count”] = $i;

return $data;
}

function pop3_dele( $nr)
{

fputs($this->socket, “dele $nrrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;
return 1;
}

function pop3_quit()
{

fputs($this->socket, “quitrn”);
$this->line = fgets($this->socket, 1024);
$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “+”) return 0;

return 1;
}
}

class smtp {

var $subject; // string the emails subject
var $fromname; // string senders name (opt)
var $toname; // string recipients name (opt)
var $body; // string body copy
var $attachment; // attachment (optional)
var $attachmenttype;
var $socket;
var $line;
var $status;

function smtp($server = “localhost”,$port = smtpport)
{
return $this->open($server, $port);
}

function smtpmail($fromemail, $fromname, $toemail, $toname, $subject, $body, $attachment=null, $attachmenttype= “text”)
{
$this->subject = $subject;
$this->toname = $toname;

$this->fromname = $fromname;
$this->body = $body;

$this->attachment = $attachment;
$this->attachmenttype = $attachmenttype;

if ($this->helo() == false){
return false;
}
if ($this->mailfrom($fromemail) == false){
return false;
}
if ($this->rcptto($toemail) == false){
return false;
}
if ($this->body() == false){
return false;
}
if ($this->quit() == false){
return false;
}
}

function open($server, $port)
{

$this->socket = fsockopen($server, $port);
if ($this->socket < 0) return false;

$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function helo()
{
if (fputs($this->socket, “helorn”) < 0 ){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function ehlo()
{

/* well, lets use “helo” for now.. until we need the
extra funcs [unk]
*/
if(fputs($this->socket, “helo localhostrn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function mailfrom($fromemail)
{

if (fputs($this->socket, “mail from: <$fromemail>rn”)<0){
return false;
}

$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;

return true;
}

function rcptto($toemail)
{

if(fputs($this->socket, “rcpt to: <$toemail>rn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “2”) return false;
return true;
}

function body()
{
$filesize = 0;
$attachment = null;
$fp = null;

$buffer = sprintf( “from: %srnto:%srnsubject:%srn”, $this->fromname, $this->toname, $this->subject);

if(fputs($this->socket, “datarn”)<0){
return false;
}
$this->line = fgets($this->socket, 1024);

$this->status[ “lastresult”] = substr($this->line, 0, 1);
$this->status[ “lastresulttxt”] = substr($this->line, 0, 1024);

if ($this->status[ “lastresult”] <> “3”) return false;

if(fputs($this->socket, $buffer)<0){
return false;
}

if ($this->attachment == null){

if(fputs($this->socket, “mime-version: 1.0rncontent-type: text/plain; charset=iso-8859-1rncontent-transfer-encoding: 7bitrnrn”)<0){
return false;
}
if(fputs($this->socket, “$this->bodyrnrn”)<0){
return false;
}

if(fputs($this->socket, “.rn”)<0){
return false;
}

$this->

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » POP3、SMTP邮件收发程序-PHP教程,PHP应用
分享到: 更多 (0)