Feb28

原创PHP邮件发送类【原创】-支持单附件发送

Author: leeon  Click: 7288   Date: 2010.02.28 @ 00:05:20 am Category: php
利用PHP自带的mail()函数来发送带有附件的邮件。此代码仅供学习交流使用。转载请注明本站来源。


class Mail {
private $topic;
private $toaddr;
private $fromaddr;
private $cc;
private $content;
private $attach;
private $header;
private $domain;//邮箱域
private $msg;

private $filename;
private $filemime;
private $filestr;

private $boundary;
private $uniqid;

private $eol; //每行末尾所加的换行符类型


function __construct(){
$this->getEOT();//生成结尾换行符
$this->getUniq_id();
$this->header='';
$this->attach='';
$this->cc='';
$this->msg='';


}


public function getFromaddr() {
return $this->fromaddr;
}

public function setFromaddr($fromaddr) {
$this->fromaddr = $fromaddr;
}

public function getTopic() {
return $this->topic;
}


public function getToaddr() {
return $this->toaddr;
}


public function getCc() {
return $this->cc;
}

public function getContent() {
return $this->content;
}


public function getAttach() {
return $this->attach;
}


public function setTopic($topic) {
$this->topic = mb_convert_encoding(trim($topic),'UTF-8','auto');
}

public function setToaddr($toaddr) {
$this->toaddr = trim($toaddr);
}


public function setCc($cc) {
$this->cc = trim($cc);
}


public function setContent($content) {
$this->content = mb_convert_encoding(trim($content),'UTF-8','auto');
}


public function setAttach($attach) {
$this->attach = trim($attach);
}

public function getDomain() {
return $this->domain;
}

public function setDomain($domain) {
$this->domain = $domain;//输入的值为‘@domain.com’
}


/*
* 根据系统类型设置换行符
*/
private function getEOT() {
if (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'WIN' )) {
$this->eol = "\r\n";
} elseif (strtoupper ( substr ( PHP_OS, 0, 3 ) == 'MAC' )) {
$this->eol= "\r";
} else {
$this->eol = "\n";
}
}


private function getBoundary(){

$this->boundary= '--'.substr(md5(time().rand(1000,2000)),0,16);

}

private function getUniq_id(){

$this->uniqid= md5(microtime().time().rand(1,100));

}

private function outputCommonHeader(){
$this->header .= 'From: '.$this->fromaddr.$this->eol;
//$this->header .= 'To: '.$this->toaddr.$this->eol;
//$this->header .= 'Subject: '.$this->topic.$this->eol;
$this->header .= 'Message-ID: <'.$this->uniqid.$this->domain.'>'.$this->eol;
$this->header .= 'MIME-Version: 1.0'.$this->eol;
$this->header .= 'Reply-To: '.$this->fromaddr.$this->eol;
$this->header .= 'Return-Path: '.$this->fromaddr.$this->eol;
$this->header .= 'X-Mailer: Xmail System'.$this->eol;
$this->header .= 'Content-Disposition: inline'.$this->eol;
}

private function mime_content_type ( $f )
{
$temp = trim ( exec ('file -bi ' . escapeshellarg ( $f ) ) ) ;
$temp = preg_replace('/\s+/',' ',$temp);
$temp = explode(' ',$temp);
return $temp[0];
}//判断文件的mime类型


/*
* 只带有抄送
*/
private function mailWithCC(){
$this->header .= 'Cc: '.$this->cc.$this->eol;
$this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
$this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
$this->msg = $this->content;
if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}
/*
* $filedir需要是绝对地址
*/
private function attachmentToBase64($filedir){
$this->filename = basename($filedir);
@$fopen = fopen($filedir,'r');
$str = fread($fopen,filesize($filedir));
$str = base64_encode($str);
$this->filestr = $str;
}


/*
* 只带有附件
*/
private function mailWithAttach(){
$this->attachmentToBase64($this->attach);
$this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
$this->msg .= $this->eol.$this->boundary.$this->eol;
$this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
$this->msg .= 'Content-Disposition: inline'.$this->eol;
$this->msg .= $this->eol.$this->content.$this->eol;
$this->msg .= $this->boundary.$this->eol;
$this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
$this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
$this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
$this->msg .= $this->eol.$this->filestr.$this->eol;
$this->msg .= $this->eol.$this->boundary.'--';

if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}

/*
* 带有附件和抄送
*/
private function mailAll(){

$this->attachmentToBase64($this->attach);
$this->header .= 'Cc: '.$this->cc.$this->eol;
$this->header .= 'Content-type: multipart/mixed; boundary="'.str_replace('--','',$this->boundary).'"'.$this->eol;
$this->msg .= $this->eol.$this->boundary.$this->eol;
$this->msg .= 'Content-Type: text/html; charset=utf-8'.$this->eol;
$this->msg .= 'Content-Disposition: inline'.$this->eol;
$this->msg .= $this->eol.$this->content.$this->eol;
$this->msg .= $this->boundary.$this->eol;
$this->msg .= 'Content-Type: '.$this->mime_content_type($this->attach).$this->eol;
$this->msg .= 'Content-Disposition: attachment; filename="'.$this->filename.'"'.$this->eol;
$this->msg .= 'Content-Transfer-Encoding: base64'.$this->eol;
$this->msg .= $this->eol.$this->filestr.$this->eol;
$this->msg .= $this->eol.$this->boundary.'--';

if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}

}
/*
* 不带抄送和附件
*/
private function mailSimple(){
$this->header .= 'Content-type: text/html; charset=UTF-8'.$this->eol;
$this->header .= 'Content-Transfer-Encoding: 8bit'.$this->eol;
$this->msg = $this->content;
if(mail($this->toaddr,$this->topic,$this->msg,$this->header)){

return 1;
}else{
return 0;
}
}

public function send(){

if(empty($this->attach)&&empty($this->cc)){
$this->outputCommonHeader();
return $this->mailSimple();

}else if(empty($this->attach)){
$this->outputCommonHeader();
return $this->mailWithCC();

}else if(empty($this->cc)){
$this->outputCommonHeader();
$this->getBoundary(); //有附件就生成boundary
return $this->mailWithAttach();

}else if(!empty($this->toaddr)&&!empty($this->topic)&&!empty($this->cc)&&!empty($this->content)&&!empty($this->attach)){
$this->outputCommonHeader();
$this->getBoundary(); //有附件就生成boundary
return $this->mailAll();
}
}
}
?>


示例代码,有些变量需要上下文环境:

$m = new Mail();
$m->setToaddr($this->temp['receipt_address']);
$m->setTopic($this->temp['mail_title']);
$m->setContent($this->temp['mail_content']);
$m->setFromaddr($_SESSION['user']['name'].' <'.$_SESSION['user']['name'].'@'.SystemDomain.'>');
$m->setDomain('@'.SystemDomain);
$m->setCc($this->temp['cc_address']);
$m->setAttach(PATH.'/temp/'.$this->temp['attachment_file']);
$m->send();



TAG:   php 附件

    评论
    • 提交

    分类

    标签

    归档

    最新评论

    Abyss在00:04:28评论了
    Linux中ramdisk,tmpfs,ramfs的介绍与性能测试
    shallwe99在10:21:17评论了
    【原创】如何在微信小程序开发中正确的使用vant ui组件
    默一在09:04:53评论了
    Berkeley DB 由浅入深【转自架构师杨建】
    Memory在14:09:22评论了
    【原创】最佳PHP框架选择(phalcon,yaf,laravel,thinkphp,yii)
    leo在17:57:04评论了
    shell中使用while循环ssh的注意事项

    我看过的书

    链接

    其他

    访问本站种子 本站平均热度:8823 c° 本站链接数:1 个 本站标签数:464 个 本站被评论次数:94 次