“收发”邮件的一个程序

小静小哥_7745

小静小哥_7745

2016-06-21

1050人浏览

原创

程序


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 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 return false;
}
fputs($this->Socket, "USER $this->user\r\n");
$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 $pass\r\n");
$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, "STAT\r\n");
$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, "LIST\r\n");
$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 $nr\r\n");
$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 $nr\r\n");
$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, "QUIT\r\n");
$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 email's subject
var $FromName; // string sender's name (opt)
var $ToName; // string recipient's 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

$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, "helo\r\n") 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, let's use "helo" for now.. Until we need the
extra func's [Unk]
*/
if(fputs($this->Socket, "helo localhost\r\n")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: \r\n")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: \r\n")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: %s\r\nTo:%s\r\nSubject:%s\r\n", $this->FromName, $this->ToName, $this->Subject);

if(fputs($this->Socket, "DATA\r\n")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)return false;
}


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

if(fputs($this->Socket, "MIME-Version: 1.0\r\nContent-Type: text/plain; charset=ISO-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n\r\n")return false;
}
if(fputs($this->Socket, "$this->Body\r\n\r\n")return false;
}

if(fputs($this->Socket, ".\r\n")return false;
}

$this->Line = fgets($this->Socket, 1024);
if (substr($this->Line, 0, 1) "2"){
return false;
}else{
return true;
}
}else{
if(fputs($this->Socket, "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"----=_NextPart_000_01BCFA61.A3697360\"\r\n".
"Content-Transfer-Encoding: 7bit\r\n\r\n".
"This is a multi-part message in MIME format.\r\n".
"\r\n------=_NextPart_000_01BCFA61.A3697360\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: 7bit\r\n".
"\r\n")return false;
}

/* 输出邮件内容 */
if(fputs($this->Socket, "$this->Body\r\n\r\n")return false;
}

if ( fputs($this->Socket, "\r\n------=_NextPart_000_01BCFA61.A3697360\r\n")return false;
}
$FileSize = filesize($this->Attachment);
if ($FileSize == false){
return false;
}
if (($fp = fopen($this->Attachment, "r"))== false) {
return false;
}else{
$Attachment = fread($fp,$FileSize);
}

// 如果没有附件的目录
if (($AttachName = strrchr($this->Attachment, '/')) == false){

$AttachName = $this->Attachment;
}

if( fputs($this->Socket,
"Content-Type: application/octet-stream; \r\nname=\"$AttachName\"\r\n".
"Content-Transfer-Encoding: quoted-printable\r\n".
"Content-Description: $AttachName\r\n".
"Content-Disposition: attachment; \r\n\tfilename=\"$AttachName\"\r\n".
"\r\n")return false;
}

/* 输出附件*/
if( fputs($this->Socket, $Attachment)return false;
}
if ( fputs($this->Socket, "\r\n\r\n------=_NextPart_000_01BCFA61.A3697360--\r\n")return false;
}

if( fputs($this->Socket, ".\r\n")return false;
}

$this->Line = fgets($this->Socket, 1024);
if (substr($this->Line, 0, 1) "2")
return false;

return true;

}
}

function Quit()
{

if(fputs($this->Socket, "QUIT\r\n")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 0;

return 1;
}
function Close()
{
fclose($this->Socket);
}
}
/*

怎样使用这个程序的一个示例

$MailTo = new Smtp();
$MailTo->SmtpMail("Dave@micro-automation.net","Dave Cramer",
"Dave@micro-automation.net","David",
"Test Mail",$MailMessage,"service.tab",0);
$MailTo->Close();
$MailTo=null;

*/
/*
$pop3 = pop3_open("localhost", "110");
if (!$pop3) {
printf("[ERROR] Failed to connect to localhost
\n");
return 0;
}

if (!pop3_user($pop3, "unk")) {
printf("[ERROR] Username failed!
\n");
return 0;
}

if (!pop3_pass($pop3, "secret")) {
printf("[ERROR] PASS failed!
\n");
return 0;
}

$articles = pop3_list($pop3);
if (!$articles) {
printf("[ERROR] LIST failed!
\n");
return 0;
}

for ($i = 1; $i {
printf("i=$i
\n");
$data = pop3_retr($pop3,$i);
if (!$data) {
printf("data goes wrong on '$i'
\n");
return 0;
}

for ($j = 0; $j {
printf("$data[$j]
\n");
}
}
*/
?>




PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

140

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

100

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

80

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

160

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

140

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

120

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

100

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

120

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
热门推荐
/
最新课程
phpStudy极速入门视频教程
phpStudy极速入门视频教程

共6课时 | 54.7万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时 | 134.3万人学习