发送html、附件、文本文件、html图片的类
作者:Binggo89 | 来源:互联网 | 2018-04-06 15:26
上传文件email.php、emailclass.php到同一目录下并在此目录下建一子目录temp,权限设为0777,执行email.php即可发送邮件。本emailclass.php功能已相当完全。1.可以发送普通文本;2.可以发送HTML文本;3.可以同时发送文本和附件;4.可以同时发
上传文件email.php、emailclass.php到同一目录下
并在此目录下建一子目录temp,权限设为0777,执行email.php即可发送邮件。
本emailclass.php功能已相当完全。
1.可以发送普通文本;
2.可以发送HTML文本;
3.可以同时发送文本和附件;
4.可以同时发送HTML和附件;
5.可以在发送HTML时在HTML中包含图片一起发送(类中有此功能,但需自行设计发送PHP程序);
6.可以发送给多个人,EMAIL以","分开;
7.可以有抄送人、转发人、也可为多个,以","分开;
如有问题请与William联系:
william.cn@163.com
/*******************************************************************************
emailclass.php
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 $mailImg = ""; //images of html file
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<count($addressArray);$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<count($addressArray);$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<count($addressArray);$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: setHtmlImages($images)
Description: stores the Images string
Arguments: $images as string with directory included
separate multiple values with comma
Returns: true if stored
*******************************************************************************/
function setHtmlImages($images){
if(strlen(trim($images)) > 0){
$this->mailImg = $images;
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<count($templateLineArray);$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 ".php": return "text/html";
case ".shtml": 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=gb2312n";
$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=gb2312n";
/* $outHTMLHeader .= "Content-Type: text/html; charset=us-asciin"; */
$outHTMLHeader .= "Content-Transfer-Encoding: 7bitnn";
$outHTMLHeader .= $this->mailHTML. "n";
return $outHTMLHeader;
}
/*******************************************************************************
Function: formatImgHeader($inFileLocation)
Description: returns a formated header for an Img
Arguments: $inFileLocation as string with relative directory
Returns: string
*******************************************************************************/
function formatImgHeader($inFileLocation){
$outImgHeader = "";
//--get content type based on file extension
$contentType = $this->getContentType($inFileLocation);
//--format header
$outImgHeader .= "Content-Type: ".$contentType. ";n";
$outImgHeader .= ' name="'.basename($inFileLocation). '"'. "n";
$outImgHeader .= "Content-Transfer-Encoding: base64 n";
$outImgHeader .= "Content-ID:<".basename($inFileLocation).">nn";
exec( "uuencode -m $inFileLocation nothing_out",$returnArray);
//--add each line returned
for ($i=1;$i<(count($returnArray));$i++){
$outImgHeader .= $returnArray[$i]. "n";
}
$outImgHeader .= "n";
return $outImgHeader;
}
/*******************************************************************************
Function: formatAttachmentHeader($inFileLocation)
Description: returns a formated header for an Attachment
Arguments: $inFileLocation as string with relative directory
Returns: string <
推荐阅读
-
本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ...
[详细]
蜡笔小新 2023-12-14 21:02:28
-
本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ...
[详细]
蜡笔小新 2023-12-14 20:58:32
-
-
本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ...
[详细]
蜡笔小新 2023-12-14 20:08:28
-
本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ...
[详细]
蜡笔小新 2023-12-14 19:45:47
-
Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ...
[详细]
蜡笔小新 2023-12-14 18:57:09
-
本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ...
[详细]
蜡笔小新 2023-12-14 18:18:21
-
今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ...
[详细]
蜡笔小新 2023-12-14 17:58:15
-
本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ...
[详细]
蜡笔小新 2023-12-14 17:15:19
-
本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ...
[详细]
蜡笔小新 2023-12-14 17:06:58
-
本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ...
[详细]
蜡笔小新 2023-12-14 16:00:02
-
本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ...
[详细]
蜡笔小新 2023-12-14 15:43:50
-
本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ...
[详细]
蜡笔小新 2023-12-14 15:08:18
-
本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ...
[详细]
蜡笔小新 2023-12-14 14:37:52
-
本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ...
[详细]
蜡笔小新 2023-12-14 16:52:33
-
最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ...
[详细]
蜡笔小新 2023-12-14 14:25:50
-