php 邮件类
没试过不知道行不行。。。
PHP代码
- <?php
- /**
- *名称:Email.class.php
- *作用:邮件类
- *说明:
- *作者:ningjinfeng@126.com
- *时间:2005-11-18
- *更新:2008-10-15
- **/
- class Email
- {
- /******************************************
- *函数:sendMail
- *作用:发送邮件函数
- *输入:$to:收件人(一个收件人 a@123.com,多个 a@1.com,b@2.com)
- ,$subject:主题,$message:内容,$from:发件人地址,$fromname:发件人名称,$ishtml:是否为html邮件(1/0),$cc:抄送的邮件,$bcc:暗送的邮件
- *输出:true/false
- *用法:$to:可为多个邮件,每个邮件用半角逗号隔开;$from为:somebody@example.com;$to和$subject中不能有换行的字符;
- **
- ******************************************
- *--制作--日期--
- *KuaiYigang@xingmo.com 2004-07-13 15:00
- ******************************************
- *--修改--日期--目的--
- *
- */
- function sendMail($to, $subject, $message, $from, $fromname, $ishtml=1, $FILE='', $cc='', $bcc='', $charset = 'gbk', $type=4)
- {
- //mail方式发送
- if($type == 1)
- {
- $tousers = array();
- foreach(explode(',', $to) as $touser)
- {
- $tousers[] = preg_match('/^(.+?) \<(.+?)\>$/',$touser, $temp) ? ($fromname ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : $temp[2]) : $touser;
- }
- $to = implode(',', $tousers);
- $subject = '=?'.$charset.'?B?'.base64_encode(str_replace("\r", '', str_replace("\n", '', $subject))).'?=';
- $message = chunk_split(base64_encode(str_replace("\r\n.", " \r\n..", str_replace("\n", "\r\n", str_replace("\r", "\n", str_replace("\r\n", "\n", str_replace("\n\r", "\r", $message)))))));
- //$from = $from == '' ? '=?'.$charset.'?B?'.base64_encode($fromname)."?= <$from>" : (preg_match('/^(.+?) \<(.+?)\>$/',$from, $temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : $from);
- $from = preg_match('/^(.+?) \<(.+?)\>$/',$from, $temp) ? '=?'.$charset.'?B?'.base64_encode($temp[1])."?= <$temp[2]>" : '=?'.$charset.'?B?'.base64_encode($fromname)."?= <$from>";
- if ($ishtml == 1)
- {
- if($FILE['name'] <> '')
- {
- //附件
- $file = $FILE['tmp_name'];
- // 定义分界线
- $boundary = uniqid( "");
- $headers = "Content-type: multipart/mixed; boundary= $boundary\r\n";
- $headers .= "From:$from\r\n";
- //确定上传文件的MIME类型
- if($FILE['type'])
- $mimeType = $FILE['type'];
- else
- $mimeType ="application/unknown";
- //文件名
- $fileName = $FILE['name'];
- // 打开文件
- $fp = fopen($file, "r");
- // 把整个文件读入一个变量
- $read = fread($fp, filesize($file));
- //我们用base64方法把它编码
- $read = base64_encode($read);
- //把这个长字符串切成由每行76个字符组成的小块
- $read = chunk_split($read);
- //现在我们可以建立邮件的主体
- $body = "--$boundary
- Content-type: text/plain; charset=iso-8859-1
- Content-transfer-encoding: 8bit
- $message
- --$boundary
- Content-type: $mimeType; name=$fileName
- Content-disposition: attachment; filename=$fileName
- Content-transfer-encoding: base64
- $read
- --$boundary--";
- //发送邮件
- $mail = mail($to, $subject,$body,$headers);
- return $mail;
- }
- else
- {
- /* To send HTML mail, you can set the Content-type header. */
- $headers = "MIME-Version: 1.0\r\n";
- $headers .= "Content-type: text/html; charset=".$charset."\r\n";
- /* additional headers */
- $headers .= "From: ".$from."\r\n";
- $headers .= "Cc: ".$cc."\r\n";
- $headers .= "Bcc: ".$bcc."\r\n";
- $headers .= "Content-Transfer-Encoding: base64\r\n";
- /* and now mail it */
- $mail = @mail($to, $subject, $message, $headers);
- return $mail;
- }
- }
- else
- {
- /*
- $mail = mail($to, $subject, $message,
- "From: ".$from."\r\n"
- ."Reply-To: ".$from."\r\n"
- ."X-Mailer: PHP/" . phpversion());
- return $mail;
- */
- /* To send HTML mail, you can set the Content-type header. */
- $headers = "MIME-Version: 1.0\r\n";
- $headers .= "Content-type: text/plain; charset=".$charset."\r\n";
- /* additional headers */
- $headers .= "From: ".$from."\r\n";
- $headers .= "Cc: ".$cc."\r\n";
- $headers .= "Bcc: ".$bcc."\r\n";
- $headers .= "Content-Transfer-Encoding: base64\r\n";
- /* and now mail it */
- $mail = @mail($to, $subject, $message, $headers);
- return $mail;
- }
- }
- //sendmail
- if($type == 2)
- {
- if ( $from == '')
- {
- $from = '163邮箱帐号';
- }
- if ( $ishtml == 1 )
- {
- $header = "MIME-Version: 1.0\r\n";
- $header .= "Content-type: text/html; charset=gb2312\r\n";
- $header .= "From: ".$from."\r\n";
- $header .= "Subject: ".$subject."\r\n";
- $header .= "Cc: \r\n";
- $header .= "Bcc: \r\n";
- $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";
- }
- else
- {
- $header = "Return-Path: ".$from."\r\n";
- $header .= "From: ".$from."\r\n";
- $header .= "Cc: \r\n";
- $header .= "Bcc: \r\n";
- $header .= "Bcc: X-Mailer: PHP/opendesktopmailer\r\n";
- }
- if ( !@$mailok = popen("/usr/sbin/sendmail ".$to, "w") )
- {
- return false;
- }
- fputs($mailok, $header);
- fputs($mailok, $message);
- $result = pclose($mailok) >> 8 & 0xFF;
- if($result != 0)
- {
- return false;
- }
- return true;
- }
- //SMTP
- if($type == 3)
- {
- $host = 'smtp.163.com';
- $username = '163邮箱帐号';
- $password = '163邮箱密码';
- $res = $this -> sendSmtpMail($to,$subject,$message,$from, $fromname, $host, $username, $password, $ishtml);
- return $res ? true :false;
- }
- //phpmailer
- if($type == 4)
- {
- $res = $this->sendPhpMail($to,$subject,$message,$from, $fromname, $ishtml, $FILE, $cc,$bcc, $charset);
- return $res ? true :false;
- }
- }
- function sendSmtpMail($to,$subject,$message,$from, $fromname, $host, $username, $password, $ishtml=0)
- {
- require '../inc/class.phpmailer.php';
- $mail = new PHPMailer();
- $mail->IsSMTP(); // send via SMTP
- $mail->Host = $host; // SMTP servers
- $mail->SMTPAuth = true; // turn on SMTP authentication
- $mail->Username = $username; // SMTP username
- $mail->Password = $password; // SMTP password
- $mail->From = $from;
- $mail->FromName = $fromname;
- $mail->AddAddress($to);
- $mail->CharSet = 'gbk';
- if ($ishtml == 1)
- {
- $mail->IsHTML(true); // send as HTML
- }
- else
- {
- $mail->IsHTML(false); // send as HTML
- }
- $mail->Subject = $subject;
- $mail->Body = $message;
- return ($mail->Send()) ? true : false; //Success
- }
- //PHPMailer,备用
- function sendPhpMail($to, $subject, $message, $from, $fromname, $ishtml=0, $FILE='', $cc='',$bcc='', $charset='gbk', $method='MAIL')
- {
- require "../inc/class.phpmailer.php";
- $mail = new PHPMailer();
- //发送邮件的方式
- switch($method)
- {
- case "SMTP":
- $mail->isSMTP();
- $mail->Host = 'smtp.163.com';
- $mail->SMTPAuth = true;
- $mail->Username = '163邮箱帐号';
- $mail->Password = '163邮箱密码';
- break;
- case "MAIL":
- $mail->isMail();
- break;
- case "SENDMAIL":
- $mail->isSendmail();
- break;
- case "QMAIL":
- $mail->isQmail();
- break;
- }
- $mail->AddReplyTo($from, $fromname);//回复地址
- $mail->SetFrom($from, $fromname);//发送人E-mail地址,姓名
- //接收E-mail地址
- foreach(explode(',', $to) as $touser)
- {
- $mail->AddAddress(trim($touser));
- }
- //抄送E-mail地址
- if($cc <> '')
- {
- foreach(explode(',', $cc) as $touser)
- {
- $mail->AddCC(trim($touser));
- }
- }
- //暗送E-mail地址
- if($bcc <> '')
- {
- foreach(explode(',', $bcc) as $touser)
- {
- $mail->AddBCC(trim($touser));
- }
- }
- //标题
- //$mail->Subject = stripslashes($subject);
- $mail->Subject = str_replace("\r", '', str_replace("\n", '', $subject));//把换行和回车去掉
- //正文
- $message = eregi_replace("[\]",'',$message);
- if($ishtml == 1)
- {
- $mail->MsgHTML($message);
- }
- else
- {
- $mail->IsHTML(false);
- }
- $mail->CharSet = $charset;
- //$mail->Body = stripslashes($message);
- $mail->AltBody = "Html内容不能正常显示。请使用能够显示html邮件内容的软件查看此邮件。"; // optional, comment out and test
- //todo: 发送附件
- //$mail->AddAttachment("images/phpmailer.gif");
- return ($mail->Send()) ? true : false;
- }
- }
- ?>
本文链接地址:
发布者:9streets于 2009-11-25 发表
| 我要评论(0) | 阅读次数(2690)





