发送html、附件、文本文件、html图片的类
作者:Remember_Mi | 来源:互联网 | 2014-07-17 01:13
上传文件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 <
推荐阅读
-
本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ...
[详细]
蜡笔小新 2023-12-14 22:40:22
-
本文介绍了如何通过修改织梦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
-
本文详细介绍了Java中final关键字的含义和用法。final关键字可以修饰非抽象类、非抽象类成员方法和变量。final类不能被继承,final类中的方法默认是final的。final方法不能被子类的方法覆盖,但可以被继承。final成员变量表示常量,只能被赋值一次,赋值后值不再改变。文章还讨论了final类和final方法的应用场景,以及使用final方法的两个原因:锁定方法防止修改和提高执行效率。 ...
[详细]
蜡笔小新 2023-12-14 18:07:46
-
本文介绍了求解gcdexgcd斐蜀定理的迭代法和递归法,并解释了exgcd的概念和应用。exgcd是指对于不完全为0的非负整数a和b,gcd(a,b)表示a和b的最大公约数,必然存在整数对x和y,使得gcd(a,b)=ax+by。此外,本文还给出了相应的代码示例。 ...
[详细]
蜡笔小新 2023-12-14 17:48:30
-
本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ...
[详细]
蜡笔小新 2023-12-14 17:43:56
-
本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ...
[详细]
蜡笔小新 2023-12-14 17:15:38
-
本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ...
[详细]
蜡笔小新 2023-12-14 17:08:34
-
电销机器人作为一种人工智能技术载体,可以帮助企业提升电销效率并节省人工成本。然而,电销机器人市场缺乏统一的市场准入标准,产品品质良莠不齐。创业者在代理或购买电销机器人时应注意谨防用录音冒充真人语音通话以及宣传技术与实际效果不符的情况。选择电销机器人时需要考察公司资质和产品品质,尤其要关注语音识别率。 ...
[详细]
蜡笔小新 2023-12-14 16:31:48
-
这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ...
[详细]
蜡笔小新 2023-12-14 16:19:10
-
本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ...
[详细]
蜡笔小新 2023-12-14 16:17:05
-
Remember_Mi
这个家伙很懒,什么也没留下!