热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php发送邮箱实例代码-PHP源码

ec(2);php教程发送邮箱实例代码classpop3{       public$server"pop3.126.com";服务器名       public$server_port110;

php教程 发送邮箱实例代码


class pop3 {


public $server="pop3.126.com";//服务器名
public $server_port=110;//服务器端口
public $timeout=30;//超过多少时间就算连接失败
public $cOnnection=0;//保持与主机的连接
public $state="DISCONNECTED";//保存当前的状态
public $debug=0;//是否显示错误信息
public $err_str="";//服务器返回的错误信息
public $err_no;//服务器返回的错误号
public $respones;//保存服务器返回的信息
public $apop;//说明需要使用加密方式进行密码验证
public $messages;//邮件数
public $size;//邮件的总大小
public $mail_list;//保存各个邮件的大小及在服务器上的序列号
public $head=array();//邮件头的内容数组
public $body=array();//邮件体的内容数组


function POP3($server,$server_port,$timeout)
{
$this->server=$server;
$this->server_port=$server_port;
$this->timeout=$timeout;
$this->debug=TRUE;
}

function open()
{
if($this->server=="")
{
$this->err_str="无效的主机名!";
return FALSE;
}
if($this->debug) echo "正在打开 $this->server,$this->server_port,$this->timeout";
if(!$this->cOnnection=@fsockopen($this->server,$this->server_port,$err_no,$err_str,$this->timeout))
{
$this->err_str="连接到POP服务器失败,错误信息:".$err_str."错误号:".$err_no;
return FALSE;
}
else
{
$this->getresponse();
if($this->debug)
$this->outdebug($this->response);
if(substr($this->respones,0,3)!="+OK")
{
$this->err_str="服务器返回无效信息:".$this->respones."请检查pop服务器是否正确";
return FALSE;
}
$this->state="AUTHORIZATION";
return TRUE;
}
}

function getresponse()
{
for($this->respones;;)
{
if(feof($this->connection))
return FALSE;
$this->respones.=fgets($this->connection,100);
$length=strlen($this->respones);
if($length>=2&&substr($this->respones,$length-2,2)=="rn")
{
$this->respOnes=strtok($this->respones,"rn");
return TRUE;
}
}
}

function outdebug($message)
{
echo htmlspecialchars($message)."n";
}

function command($command,$length,$code)
{
if($this->cOnnection==0)
{
$this->outdebug("没有连接到任何服务器,请检查网络连接!");
return FALSE;
}
if($this->debug)
$this->outdebug(">>> $command");
if(!fput($this->connection,"$command rn"))
{
$this->outdebug("'无法发送命令'.$command");
return FALSE;
}
else
{
$this->getresponse();
if($this->debug)
$this->outdebug($this->respones);
if(substr($this->respones,0,$length)!=$code)
{
$this->outdebug("$command.'命令服务器返回信息无效'.$this->response");
return FALSE;
}
else
return TRUE;
}
}

function login($user,$pass)
{
if($this->state!="AUTHORIZATION")
{
$this->outdebug("没有连接到任何服务器或状态不对!");
return FALSE;
}
if(!$this->apop)
{
if(!$this->command("USER $user",3,"+OK")) return FALSE;
if(!$this->command("PASS $pass",3,"+OK")) return FALSE;
}
else
{
if(!$this->command(" APOP $user".md5($this->greeting.$pass),3,"+OK")) return FALSE;
}
$this->state="TRANSACTION";
return TRUE;
}

function stat_sum()
{
if($this->state!="TRANSACTION")
{
$this->outdebug("还没有连接服务器或没有成功登陆");
return FALSE;
}
if(!$this->command("STAT",3,"+OK"))
return FALSE;
else
{
$this->respOnes=strtok($this->respones," ");
$this->messages=strtok(" ");
$this->size=strtok(" ");
return TRUE;
}
}

function listmail($mess=null,$uni_id=null)
{
if($this->state!="TRANSACTION")
{
$this->outdebug("还没有连接服务器或没有成功登陆");
return FALSE;
}
if($uni_id)
$command="UIDL";
else
$command="LIST";
if($mess)
$command.=$mess;
if(!$this->command($command,3,"+OK"))
return FALSE;
else
{
$i=0;
$this->mail_list=array();
$this->getresponse();
while($this->respones!=".")
{
$i++;
if($this->debug)
$this->outdebug($this->respones);
if(uni_id)
{
$this->mail_list[$i][num]=strtok($this->respones," ");
$this->mail_list[$i][size]=strtok(" ");
}
else
{
$this->mail_list[$i][num]=intval(strtok($this->respones," "));
$this->mail_list[$i][size]=intval(strtok(" "));
}
$this->getresponse();
}
return TRUE;
}
}

function getmail($num,$line=-1)
{
if($this->state!="TRANSACTION")
{
$this->outdebug("还没有连接服务器或没有成功登陆");
return FALSE;
}
if($line<0)
$command="RETR $num";
else
$command="TOP $num $line";
if(!$this->command($command,3,"+OK"))
return FALSE;
else
{
$this->getresponse();
$is_head=TRUE;
while($this->respones!=".")
{
if($this->debug)
$this->outdebug($this->respones);
if(substr($this->respones,0,1)!=".")
{
$this->respOnes=substr($this->respones,1,strlen($this->respones)-1);
}
if(trim($this->respones)=="")
$is_head=FALSE;
if($is_head)
$this->head[]=$this->respones;
else
$this->body[]=$this->respones;
$this->getresponse();
}
return TRUE;
}
}

function dele($num)
{
if($this->state!="TRANSACTION")
{
$this->outdebug(",不能删除远程信件,还没有连接服务器或没有成功登陆");
return FALSE;
}
if(!num)
{
$this->outdebug("删除的邮件参数不对");
return FALSE;
}
if($this->command("DELE $num",3,"+OK")) return TRUE;
else return FALSE;
}

function close()
{
if($this->connection!=0)
{
if($this->state=="TRANSACTION")
$this->command("QUIT",3,"+OK");
fclose($this->connection);
$this->cOnnection==0;
$this->state="DISCONNECTED";
}
}
}

//发送邮件类调用方法

$host="pop3.126.com";
$user=" ";
$pass=" ";
$rec=new pop3($host,110,20);
if(!$rec->open()) die($rec->err_str);
echo "open";
if(!$rec->login($user,$pass)) die($rec->err_str);
echo "login";
if(!$rec->stat()) die($rec->err_str);
echo "共有".$rec->messages."封信件,共".$rec->size."字节大小";
if($rec->messages>0)
{
if(!$rec->listmail()) die($rec->err_str);
echo "有以下信件:";
for($i=1;$i<=count($rec->mail_list);$i++)
{
echo "信件".$rec->mail_list[$i][num]."大小".$rec->mail[$i][size]."";
}
$rec->getmail(1);
echo "邮件头的内容:";
for($i=0;$ihead;$i++)
echo htmlspecialchars($rec->head[$i])."n";
for($i=0;$ibody;$i++)
echo htmlspecialchars($rec->body[$i])."n";
}
$rec->close();

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
author-avatar
帅帅考拉_955
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有