socket发送邮件(php)

      脚本天地 2005-7-11 16:28
<?
class pmail
{
var $smtpaddress; //smtp主机 
var $smtpport; //端口 一般为25
var $username; //SMTP登录有户名
var $password; //认证密码
var $bodycharset; //邮件正文编码

var $subjecttext; //主题
var $bodytext; //邮件正文(纯文本格式)
var $fromaddress; //发信人Email
var $priorityint=3; //邮件等级,1为加急,3为普通,5为低级
var $intformat=0;

var $fp; //sock句柄
var $result; //返回结果
var $command; //发送命令
var $toaddress; //tomail地址列表,有,号分开的
var $attachment; //附件内容
var $attachmenttext; //附件正文件
var $boundary;
var $mailbody; //最终发送内容
/*
类型:构造方法
参数:无
作用:初始化
*/
function pmail() {
$this->smtpaddress = "";
$this->smtpport = 25;
$this->username = "";
$this->password = "";
$this->bodycharset = "gb2312";
$this->bodytext = "";

$this->subjecttext = "无标题";
$this->bodytext = "";
$this->fromaddress = "";
$this->priorityint = 3; 
$this->attachmenttext = "";

$this->intformat = 0;
$this->fp = "";
$this->result = "";
$this->command = "";
$this->toaddress = "";
$this->attachment = "";
$this->boundary = uniqid( "");
$this->mailbody = "";
}

/*
类型:公有属性
参数:smtp服务器地址
作用:检查SMTP主机地址
*/
function serverport($smtpport) {
if (emtpy($smtpport)) exit("请指定SMTP主机端口");
$this->$smtpport = $smtpport;
}

/*
类型:公有属性
参数:smtp服务器地址
作用:检查SMTP主机地址
*/
function serveraddress($smtpaddress) {
if (empty($smtpaddress)) exit("请指定SMTP主机地址");
$this->smtpaddress = $smtpaddress;
}

/*
类型:公有属性
参数:$username
作用:指定SMTP验证帐号
*/
function mailserverusername($username) {
if (empty($username)) exit("请指定SMTP验证帐号");
$this->username = base64_encode($username);
}

/*
类型:公有属性
参数:$password
作用:指定SMTP验证密码
*/
function mailserverpassword($password) {
if (empty($password)) exit("请指定SMTP验证密码");
$this->password = base64_encode($password);
}

/*
类型:公有属性
参数:$bodycharset
作用:指定正文编码
*/
function charset($bodycharset) {
if (empty($bodycharset)) exit("请指定正文编码");
$this->bodycharset = $bodycharset;
}

/*
类型:公有属性
参数:$subjecttext;
作用:指定主题
*/
function subject($subjecttext) {
if (empty($subjecttext)) exit("请指定主题");
$this->subjecttext = $subjecttext;
}

/*
类型:公有属性
参数:$bodytext
作用:指定bodytext
*/
function body($bodytext) {
$this->bodytext .= $bodytext;
}

/*
类型:公有属性
参数:$bodytext
作用:指定bodytext
*/
function format($formatint) {
$this->intformat = $formatint;
}

/*
类型:公有属性
参数:$fromaddress
作用:指定fromaddress
*/
function from($fromaddress) {
if (empty($fromaddress)) exit("请指定发信人地址");
$this->fromaddress = $fromaddress;
}

/*
类型:公有属性
参数:$priorityint
作用:指定邮件等级 1为加急,3为普通,5为低级
*/
function priority($priorityint) {
if (!is_int($priorityint)) exit("邮件等级请使有整数:1为加急,3为普通,5为低级");
$this->priorityint = $priorityint;
}

/*
类型:私有方法
参数:无
作用:设置邮箱内容
*/
function setmailbody() {
if (!empty($this->attachment)) { //如果附件不为空,此邮箱为有附件邮件
$endstr = "--" . $this->boundary ."--\n"; //结尾分隔
if (!ereg($endstr,$this->attachment)) $this->attachment .= $endstr; //完成结尾分隔
$this->mailbody = $this->attachment;
}
else { //无附件
$this->mailbody = $this->bodytext;
}
}

/*
类型:私有方法
参数:$to 收信人地址
作用:根据收信人地址不同,重新设置邮件头
*/
function setmailheader($to) {
$this->mailheader  = "From:". $this->fromaddress ."\n";
if (!empty($this->smtpaddress)) { //使用smtp发送,邮头加上主题级收信人地址
$this->mailheader .= "To:". $to ."\n";
$this->mailheader .= "Subject:". $this->subjecttext ."\n";
}
$this->mailheader .= "X-Mailer: Ieay.com Socket SendMail\n";
$this->mailheader .= "X-Priority: ". $this->priorityint ."\n";

if (!empty($this->attachment)) {
$this->mailheader .= "Content-Type: multipart/mixed;";
$this->mailheader .= "boundary=\"". $this->boundary . "\"\n";
$this->mailheader .="\n\n"; //重设邮件头

/*处理正文件*/
if (!empty($this->bodytext)) { //如果正文不为空
if (empty($this->attachmenttext)) { //如果没指定带附件的邮箱正文,为了群发,不重复
$this->attachmenttext = "--" . $this->boundary . "\n";
if ($this->intformat==0) //文本
$this->attachmenttext .= "Content-Type: text/plain; charset=\"". $this->bodycharset ."\"\n";//编码
else
$this->attachmenttext .= "Content-Type: text/html; charset=\"". $this->bodycharset ."\"\n";//编码
$this->attachmenttext .= "Content-Transfer-Encoding: 8bit\n\n";
$this->attachmenttext .= $this->bodytext."\n";
$this->attachment = $this->attachmenttext . $this->attachment;
}
}
}
else {
if ($this->intformat==0) //文本
$this->mailheader .= "Content-Type: text/plain; charset=\"". $this->bodycharset ."\"\n";//编码
else
$this->mailheader .= "Content-Type: text/html; charset=\"". $this->bodycharset ."\"\n";//编码
$this->mailheader .= "Content-Transfer-Encoding: 8bit\n\n";
}
}

/*
类型:公有方法
参数:$mailtoaddress 收信人地址列表,多个有,号分开
作用:加入收信人地址
*/
function addrecipient($toaddress) {
if (empty($this->toaddress))
$this->toaddress = $toaddress;
else
$this->toaddress .= "," . $toaddress;
}

/*
类型:公有方法
参数: $srcfilename '原文件名(带路径)
$objfilename '发送后文件名(不带路径
$contenttype '文件头类型
作用:加入附件,调有此方法,将重设邮件正文件格式
*/
function addattachment($srcfilename,$objfilename,$contenttype="application/octet-stream") {
$fp = fopen($srcfilename, "r");
if (!$fp) exit("没找到附件");
$readcontent = fread($fp, filesize($srcfilename));
$readcontent = base64_encode($readcontent); 
$readcontent = chunk_split($readcontent); //获得附件内容

$this->attachment .= "--" . $this->boundary ."\n";
$this->attachment .= "Content-Type: ". $contenttype .";";
$this->attachment .= "filename=\"". $objfilename . "\"\n";
$this->attachment .= "Content-Transfer-Encoding: base64\n";
$this->attachment .= "Content-Disposition: attachment;";
$this->attachment .= "filename=\"". $objfilename . "\"\n\n";
$this->attachment .= $readcontent ."\n";
}

/*
类型:私有方法
参数:无
作用:使用smtp服务器发送邮件
*/

function smtpsend() {
$arytoaddress = explode(",",$this->toaddress); //分开收信人地址到数组
for ($k=0; $k<count($arytoaddress); $k++) { //循环发送
$this->fp = fsockopen($this->smtpaddress,$this->smtpport); //连接SMTP主机

if (!$this->fp) exit("初始化失败,请检查您的网络连接和参数"); //检查句柄

set_socket_blocking($this->fp,true);
$this->result = fgets($this->fp,1024); //获得一行响应信息

$this->command = "EHLO HELO\r\n"; //指定命令
fputs($this->fp,$this->command); //发送命令,与服务器打招呼
$this->result .= fgets($this->fp,1024);
for ($i=1; $i<5; $i++) {
if (!feof($this->fp)) $this->result .= fgets($this->fp,1024);
}

$this->command = "AUTH LOGIN\r\n";
fputs($this->fp,$this->command); //发送登陆命令
$this->result = fgets($this->fp,1024);

$this->command = $this->username."\r\n";
fputs($this->fp,$this->command); //发送有户名
$this->result = fgets($this->fp,1024);

$this->command = $this->password."\r\n"; //发送密码
fputs($this->fp,$this->command);
$this->result = fgets($this->fp,1024);

if (!ereg("^235", $this->result)) exit($this->result . "<BR>用户名,密码不正确"); //如果返回235 为成功
$this->command =  "MAIL FROM:".$this->fromaddress."\r\n"; //发送人地址
fputs($this->fp,$this->command);
$this->result = fgets($this->fp,1024);

if (!ereg("250", $this->result)) exit($this->result . "<BR>发信人地址不正确或此stmp服务器有问题");
$this->command = "RCPT TO:". $arytoaddress[$k] ."\r\n";
fputs($this->fp,$this->command); //发送收信人地址
$this->result = fgets($this->fp,1024);

$this->command =  "DATA\r\n"; //发送信息
fputs($this->fp,$this->command);
$this->result = fgets($this->fp,1024);

$this->setmailheader($arytoaddress[$k]); //重设邮件头
$this->setmailbody(); //内容

$this->mailbody = $this->mailheader . $this->mailbody;

$this->command = $this->mailbody."\r\n.\r\n"; //发送邮件内容
fputs($this->fp,$this->command);
$this->result = fgets($this->fp,1024);

$this->command = "QUIT\r\n";
fputs($this->fp,$this->command); //发送退出命令
}
}

/*
类型:私有方法
参数:无
作用:使用mail函数发送邮件
*/
function nosmtpsend() {
$arytoaddress = explode(",",$this->toaddress); //分开收信人地址到数组
for ($k=0; $k<count($arytoaddress); $k++) { //循环发送
$this->setmailheader($arytoaddress[$k]); //重设邮件头
$this->setmailbody(); //内容
mail($arytoaddress[$k],$this->subjecttext,$this->mailbody,$this->mailheader);
}
}

/*
类型:公有方法
参数:无
作用:发送邮件
*/
function send() {
if (empty($this->fromaddress)) exit("请指定发信人地址");
if (empty($this->toaddress)) exit("请指定收信人地址,多个地址,请有逗号分开");
if (!empty($this->smtpaddress))
$this->smtpsend();
else
$this->nosmtpsend();
}
}
?>

#################################################################

$pmail= new pmail();
$pmail->serveraddress("smtp.163.com");
$pmail->mailserverusername("xxxxx");//用户名
$pmail->mailserverpassword("xxxxx");//密码
$pmail->format(0);
$pmail->from("koocyton@gmail.com");

$pmail->subject(
				

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
POST['title']); $pmail->body(

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
POST['content']); //$pmail->priority(intval(

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
POST['priority'])); $pmail->addrecipient(

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
POST['getman']); //加入地址 $attachment_num = count(

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
FILES['file']['name']); //附件数量 for ($i=0; $i<$attachment_num; $i++) { $filename=

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
FILES['file']['tmp_name'][$i]; //服务器上临时文件 $objfilename=

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
FILES['file']['name'][$i]; //原文件名 $contenttype=

socket发送邮件(php)

      脚本天地 2005-7-11 16:28
{logcontent}
标签集:TAGS:
回复Comments() 点击Count()
FILES['file']['type'][$i]; //文件类型 if (!empty($filename)) $pmail->addattachment($filename,$objfilename,$contenttype); } $pmail->send();
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}