<?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->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 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, "helo\r\n") < 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 localhost\r\n")<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>\r\n")<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>\r\n")<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: %s\r\nto:%s\r\nsubject:%s\r\n", $this->fromname, $this->toname, $this->subject);
if(fputs($this->socket, "data\r\n")<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.0\r\ncontent-type: text/plain; charset=iso-8859-1\r\ncontent-transfer-encoding: 7bit\r\n\r\n")<0){
return false;
}
if(fputs($this->socket, "$this->body\r\n\r\n")<0){
return false;
}
if(fputs($this->socket, ".\r\n")<0){
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")<0){
return false;
}
/* 输出邮件内容 */
if(fputs($this->socket, "$this->body\r\n\r\n")<0){
return false;
}
if ( fputs($this->socket, "\r\n——=_nextpart_000_01bcfa61.a3697360\r\n")<0){
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")<0){
return false;
}
/* 输出附件*/
if( fputs($this->socket, $attachment)<0){
return false;
}
if ( fputs($this->socket, "\r\n\r\n——=_nextpart_000_01bcfa61.a3697360–\r\n")<0){
return false;
}
if( fputs($this->socket, ".\r\n")<0){
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")<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 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<br>\n");
return 0;
}
if (!pop3_user($pop3, "unk")) {
printf("[error] username failed!<br>\n");
return 0;
}
if (!pop3_pass($pop3, "secret")) {
printf("[error] pass failed!<br>\n");
return 0;
}
$articles = pop3_list($pop3);
if (!$articles) {
printf("[error] list failed!<br>\n");
return 0;
}
for ($i = 1; $i < $articles ["count"] + 1; $i++)
{
printf("i=$i<br>\n");
$data = pop3_retr($pop3,$i);
if (!$data) {
printf("data goes wrong on $i<br>\n");
return 0;
}
for ($j = 0; $j < $data["count"]; $j++)
{
printf("$data[$j]<br>\n");
}
}
*/
?>
