学院首页 软件应用 编程开发 创意设计 认证培训 软件论坛
ASP ASP.NET PHP JSP SQL MYSQL Java VB

您的位置:学院 >> 编程开发 >> PHP >> 能发普通文本、HTML及带附件的EMAIL类


能发普通文本、HTML及带附件的EMAIL类



/*******************************************************************************
Name: Email
Description: This class is used for sending emails.
These emails can be
Plain Text, HTML, or Both. Other uses include file
Attachments and email Templates(from a file).
Testing:
test_email.php3:

$mail->setTo("myEmail@yo.com");
$mail->send();


Changelog:
Date Name Description
----------- ----------- ------------------------------------------------
10/21/1999 R.Chambers created
*******************************************************************************/
/*******************************************************************************
Issues:
no error reporting
can only send HTML with TEXT
can only send attachements with HTML and TEXT
*******************************************************************************/
/*******************************************************************************
Function Listing:
setTo($inAddress)
setCC($inAddress)
setBCC($inAddress)
setFrom($inAddress)
setSubject($inSubject)
setText($inText)
setHTML($inHTML)
setAttachments($inAttachments)
checkEmail($inAddress)
loadTemplate($inFileLocation,$inHash,$inFormat)
getRandomBoundary($offset)
getContentType()
formatTextHeader()
formatHTMLHeader()
formatAttachmentHeader($inFileLocation)
send()
*******************************************************************************/

class Email
{
//---Global Variables
var $mailTo = ""; // array of To addresses
var $mailCC = ""; // copied recipients
var $mailBCC = ""; // hidden recipients
var $mailFrom = ""; // from address
var $mailSubject = ""; // email subject
var $mailText = ""; // plain text message
var $mailHTML = ""; // html message
var $mailAttachments = ""; // array of attachments

/*******************************************************************************
Function: setTo($inAddress)
Description: sets the email To address
Arguments: $inAddress as string
separate multiple values with comma
Returns: true if set
*******************************************************************************/
function setTo($inAddress){
//--split addresses at commas
$addressArray = explode( ",",$inAddress);
//--loop through each address and exit on error
for($i=0;$i if($this->checkEmail($addressArray[$i])==false) return false;
}
//--all values are OK so implode array into string
$this->mailTo = implode($addressArray, ",");
return true;
}
/*******************************************************************************
Function: setCC($inAddress)
Description: sets the email cc address
Arguments: $inAddress as string
separate multiple values with comma
Returns: true if set
*******************************************************************************/
function setCC($inAddress){
//--split addresses at commas
$addressArray = explode( ",",$inAddress);
//--loop through each address and exit on error
for($i=0;$i if($this->checkEmail($addressArray[$i])==false) return false;
}
//--all values are OK so implode array into string
$this->mailCC = implode($addressArray, ",");
return true;
}
/*******************************************************************************
Function: setBCC($inAddress)
Description: sets the email bcc address
Arguments: $inAddress as string
separate multiple values with comma
Returns: true if set
*******************************************************************************/
function setBCC($inAddress){
//--split addresses at commas
$addressArray = explode( ",",$inAddress);
//--loop through each address and exit on error
for($i=0;$i if($this->checkEmail($addressArray[$i])==false) return false;
}
//--all values are OK so implode array into string
$this->mailBCC = implode($addressArray, ",");
return true;
}
/*******************************************************************************
Function: setFrom($inAddress)
Description: sets the email FROM address
Arguments: $inAddress as string (takes single email address)
Returns: true if set
*******************************************************************************/
function setFrom($inAddress){
if($this->checkEmail($inAddress)){
$this->mailFrom = $inAddress;
return true;
}
return false;
}
/*******************************************************************************
Function: setSubject($inSubject)
Description: sets the email subject
Arguments: $inSubject as string
Returns: true if set
*******************************************************************************/
function setSubject($inSubject){
if(strlen(trim($inSubject)) > 0){
$this->mailSubject = ereg_replace( "n", "",$inSubject);
return true;
}
return false;
}
/*******************************************************************************
Function: setText($inText)
Description: sets the email text
Arguments: $inText as string
Returns: true if set
*******************************************************************************/
function setText($inText){
if(strlen(trim($inText)) > 0){
$this->mailText = $inText;
return true;
}
return false;
}
/*******************************************************************************
Function: setHTML($inHTML)
Description: sets the email HMTL
Arguments: $inHTML as string
Returns: true if set
*******************************************************************************/
function setHTML($inHTML){
if(strlen(trim($inHTML)) > 0){
$this->mailHTML = $inHTML;
return true;
}
return false;
}
/*******************************************************************************
Function: setAttachments($inAttachments)
Description: stores the Attachment string
Arguments: $inAttachments as string with directory included
separate multiple values with comma
Returns: true if stored
*******************************************************************************/
function setAttachments($inAttachments){
if(strlen(trim($inAttachments)) > 0){
$this->mailAttachments = $inAttachments;
return true;
}
return false;
}
/*******************************************************************************
Function: checkEmail($inAddress)
Description: checks for valid email
Arguments: $inAddress as string
Returns: true if valid
*******************************************************************************/
function checkEmail($inAddress){
return (ereg( "^[^@ ]+@([a-zA-Z0-9-]+.)+([a-zA-Z0-9-]{2}|net|com|gov|mil|org|edu|int)$",$inAddress));
}
/*******************************************************************************
Function: loadTemplate($inFileLocation,$inHash,$inFormat)
Description: reads in a template file and replaces hash values
Arguments: $inFileLocation as string with relative directory
$inHash as Hash with populated values
$inFormat as string either "text" or "html"
Returns: true if loaded
*******************************************************************************/
function loadTemplate($inFileLocation,$inHash,$inFormat){
/*
template files have lines such as:
Dear '!UserName',
Your address is '!UserAddress'
*/
//--specify template delimeters
$templateDelim = "'";
$templateNameStart = "!";
//--set out string
$templateLineOut = "";
//--open template file
if($templateFile = fopen($inFileLocation, "r")){
//--loop through file, line by line
while(!feof($templateFile)){
//--get 1000 chars or (line break internal to fgets)
$templateLine = fgets($templateFile,1000);
//--split line into array of hashNames and regular sentences
$templateLineArray = explode($templateDelim,$templateLine);
//--loop through array
for( $i=0; $i //--look for $templateNameStart at position 0
if(strcspn($templateLineArray[$i],$templateNameStart)==0){
//--get hashName after $templateNameStart
$hashName = substr($templateLineArray[$i],1);
//--replace hashName with acual value in $inHash
//--(string) casts all values as "strings"
$templateLineArray[$i] = ereg_replace($hashName,(string)$inHash[$hashName],$hashName);
}
}
//--output array as string and add to out string
$templateLineOut .= implode($templateLineArray, "");
}
//--close file
fclose($templateFile);
//--set Mail body to proper format
if( strtoupper($inFormat)== "TEXT" ) return($this->setText($templateLineOut));
else if( strtoupper($inFormat)== "HTML" ) return($this->setHTML($templateLineOut));
}
return false;
}
/*******************************************************************************
Function: getRandomBoundary($offset)
Description: returns a random boundary
Arguments: $offset as integer - used for multiple calls
Returns: string
*******************************************************************************/
function getRandomBoundary($offset = 0){
//--seed random number generator
srand(time()+$offset);
//--return md5 32 bits plus 4 dashes to make 38 chars
return ( "----".(md5(rand())));
}
/*******************************************************************************
Function: getContentType($inFileName)
Description: returns content type for the file type
Arguments: $inFileName as file name string (can include path)
Returns: string
*******************************************************************************/
function getContentType($inFileName){
//--strip path
$inFileName = basename($inFileName);
//--check for no extension
if(strrchr($inFileName, ".") == false){
return "application/octet-stream";
}
//--get extension and check cases
$extension = strrchr($inFileName, ".");
switch($extension){
case ".gif": return "image/gif";
case ".gz": return "application/x-gzip";
case ".htm": return "text/html";
case ".html": return "text/html";
case ".jpg": return "image/jpeg";
case ".tar": return "application/x-tar";
case ".txt": return "text/plain";
case ".zip": return "application/zip";
default: return "application/octet-stream";
}
return "application/octet-stream";
}
/*******************************************************************************
Function: formatTextHeader
Description: returns a formated header for text
Arguments: none
Returns: string
*******************************************************************************/
function formatTextHeader(){
$outTextHeader = "";
$outTextHeader .= "Content-Type: text/plain; charset=us-asciin";
$outTextHeader .= "Content-Transfer-Encoding: 7bitnn";
$outTextHeader .= $this->mailText. "n";
return $outTextHeader;
}
/*******************************************************************************
Function: formatHTMLHeader
Description: returns a formated header for HTML
Arguments: none
Returns: string
*******************************************************************************/
function formatHTMLHeader(){
$outHTMLHeader = "";
$outHTMLHeader .= "Content-Type: text/html; charset=us-asciin";
$outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
$outHTMLHeader .= $this->mailHTML. "n";
return $outHTMLHeader;
}
/*******************************************************************************
Function: formatAttachmentHeader($inFileLocation)
Description: returns a formated header for an attachment
Arguments: $inFileLocation as string with relative directory
Returns: string
*******************************************************************************/
function formatAttachmentHeader($inFileLocation){
$outAttachmentHeader = "";
//--get content type based on file extension
$contentType = $this->getContentType($inFileLocation);
//--if content type is TEXT the standard 7bit encoding
if(ereg( "text",$contentType)){
//--format header
$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
$outAttachmentHeader .= "Content-Transfer-Encoding: 7bitn";
$outAttachmentHeader .= "Content-Disposition: attachment;n"; //--other: inline
$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
$textFile = fopen($inFileLocation, "r");
//--loop through file, line by line
while(!feof($textFile)){
//--get 1000 chars or (line break internal to fgets)
$outAttachmentHeader .= fgets($textFile,1000);
}
//--close file
fclose($textFile);
$outAttachmentHeader .= "n";
}
//--NON-TEXT use 64-bit encoding
else{
//--format header
$outAttachmentHeader .= "Content-Type: ".$contentType. ";n";
$outAttachmentHeader .= ' name="'.basename($inFileLocation). '"'. "n";
$outAttachmentHeader .= "Content-Transfer-Encoding: base64n";
$outAttachmentHeader .= "Content-Disposition: attachment;n"; //--other: inline
$outAttachmentHeader .= ' filename="'.basename($inFileLocation). '"'. "nn";
//--call uuencode - output is returned to the return array
exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
//--add each line returned
for ($i = 1; $i<(count($returnArray)); $i++){
$outAttachmentHeader .= $returnArray[$i]. "n";
}
}
return $outAttachmentHeader;
}
/*******************************************************************************
Function: send()
Description: sends the email
Arguments: none
Returns: true if sent
*******************************************************************************/
function send(){
//--set mail header to blank
$mailHeader = "";
//--add CC
if($this->mailCC != "") $mailHeader .= "CC: ".$this->mailCC. "n";
//--add BCC
if($this->mailBCC != "") $mailHeader .= "BCC: ".$this->mailBCC. "n";
//--add From
if($this->mailFrom != "") $mailHeader .= "FROM: ".$this->mailFrom. "n";

//---------------------------MESSAGE TYPE-------------------------------
//--TEXT ONLY
if($this->mailText != "" && $this->mailHTML == "" && $this->mailAttachments == ""){
return mail($this->mailTo,$this->mailSubject,$this->mailText,$mailHeader);
}
//--HTML AND TEXT
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments == ""){
//--get random boundary for content types
$bodyBoundary = $this->getRandomBoundary();
//--format headers
$textHeader = $this->formatTextHeader();
$htmlHeader = $this->formatHTMLHeader();
//--set MIME-Version
$mailHeader .= "MIME-Version: 1.0n";
//--set up main content header with boundary
$mailHeader .= "Content-Type: multipart/alternative;n";
$mailHeader .= ' boundary="'.$bodyBoundary. '"';
$mailHeader .= "nnn";
//--add body and boundaries
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $textHeader;
$mailHeader .= "--".$bodyBoundary. "n";
//--add html and ending boundary
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--send message
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
//--HTML AND TEXT AND ATTACHMENTS
else if($this->mailText != "" && $this->mailHTML != "" && $this->mailAttachments != ""){

//--get random boundary for attachments
$attachmentBoundary = $this->getRandomBoundary();
//--set main header for all parts and boundary
$mailHeader .= "Content-Type: multipart/mixed;n";
$mailHeader .= ' boundary="'.$attachmentBoundary. '"'. "nn";
$mailHeader .= "This is a multi-part message in MIME format.n";
$mailHeader .= "--".$attachmentBoundary. "n";

//--TEXT AND HTML--
//--get random boundary for content types
$bodyBoundary = $this->getRandomBoundary(1);
//--format headers
$textHeader = $this->formatTextHeader();
$htmlHeader = $this->formatHTMLHeader();
//--set MIME-Version
$mailHeader .= "MIME-Version: 1.0n";
//--set up main content header with boundary
$mailHeader .= "Content-Type: multipart/alternative;n";
$mailHeader .= ' boundary="'.$bodyBoundary. '"';
$mailHeader .= "nnn";
//--add body and boundaries
$mailHeader .= "--".$bodyBoundary. "n";
$mailHeader .= $textHeader;
$mailHeader .= "--".$bodyBoundary. "n";
//--add html and ending boundary
$mailHeader .= $htmlHeader;
$mailHeader .= "n--".$bodyBoundary. "--";
//--send message
//--END TEXT AND HTML

//--get array of attachment filenames
$attachmentArray = explode( ",",$this->mailAttachments);
//--loop through each attachment
for($i=0;$i //--attachment separator
$mailHeader .= "n--".$attachmentBoundary. "n";
//--get attachment info
$mailHeader .= $this->formatAttachmentHeader($attachmentArray[$i]);
}
$mailHeader .= "--".$attachmentBoundary. "--";
return mail($this->mailTo,$this->mailSubject, "",$mailHeader);
}
return false;
}
}
?>
技术文章快速查找

栏目导航
软件应用
·操作系统 ·杀毒防黑 ·应用软件
·聊天软件 ·网络软件  
Web开发
·ASP ·JavaScript ·CGI
·JSP ·VbScript ·Web服务器
·PHP ·XML  
开发语言
·VB ·VC ·ASP.NET
·Java ·C/C++ ·Delphi
数据库开发
·MySQL ·SQL/Access ·PowerBuilder
·Oracle ·DB2  
网站设计
·Flash ·Dreamweaver ·HTML/CSS
·Fireworks ·FrontPage  
平面设计
·Photoshop ·CorelDraw ·AutoCAD
·FreeHand ·Illustrator ·3DsMAX
媒体动画
·Director ·Authorware ·Maya
·视频处理    


相关软件 产品库推荐
·笔记本 ·台式机 ·服务器
·数码相机 ·手机 ·GPS
·DV摄像机 ·MP3 ·MP4
·CPU ·硬盘 ·内存
·主板 ·显卡 ·显示器
·打印机 ·投影机 ·路由器

还没人留言,抢个先,哈哈!
对"能发普通文本、HTML及带附件的EMAIL类"的评论 - 快速回贴
内容:
  [完成后可按Ctrl+Enter发布]

百度中 能发普通文本、HTML及带附件的EMAIL类 相关内容
Google搜索中 能发普通文本、HTML及带附件的EMAIL类 相关内容
雅虎中 能发普通文本、HTML及带附件的EMAIL类 相关内容
Sogou搜索中 能发普通文本、HTML及带附件的EMAIL类 相关内容

相关软件 最新回复帖子:

·AutoCAD打造精致三维鸟笼实例详解
·Photoshop自定义水晶字特效样式
·AutoCAD三维基础实例教程
·PS为黑背景长发美女照片抠图换背
·用Photoshop自制个性摩托车贴花小经验
·轻松几步将美女照片处理为手工素描
·巧用Photoshop画笔轻松绘制创意特效
·用Photoshop通道将模糊肖像照片清晰化
·照片处理:Photoshop修复残破照片
·PS CS2新增形状模糊滤镜绘制花布图案


  相关软件 能发普通文本、HTML及带附件的EMAIL类相关文章
用EMAIL附件传送一个文件(用mail) 目录的递归操作
递归列出所有文件和目录 主页空间管理系统1.0(二)
主页空间管理系统1.0(一) 在PHP中用GD函数输出TTF字体一例
一个简单的留言本程序 用户口令检查(/etc/passwd)
在线竞拍系统的PHP实现框架(二) 一个我自己做的计数器+访客统计
在线竞拍系统的PHP实现框架(一) 判断ICQ是否在线的代码
班级主页的建设---之照片上传 将session封装入一个简单的购物车类之中
分页显示Mysql数据库记录的类 用PHP执行Oracle存储过程示例
pop3邮件收取一例 一个改进的UBB类
php作线形图的函数 用PHP+MYSQL实现论坛里的分级+分页显示