php实现的发送带附件邮件类实例
2023-06-13 09:15:46 时间
本文实例讲述了php实现的发送带附件邮件类的方法,是一个非常实用的功能。分享给大家供大家参考。具体方法如下:
emailclass.php类文件如下:
<? classCMailFile{ var$subject; var$addr_to; var$text_body; var$text_encoded; var$mime_headers; var$mime_boundary="--==================_846811060==_"; var$smtp_headers; functionCMailFile($subject,$to,$from,$msg,$filename,$downfilename,$mimetype="application/octet-stream",$mime_filename=false){ $this->subject=$subject; $this->addr_to=$to; $this->smtp_headers=$this->write_smtpheaders($from); $this->text_body=$this->write_body($msg); $this->text_encoded=$this->attach_file($filename,$downfilename,$mimetype,$mime_filename); $this->mime_headers=$this->write_mimeheaders($filename,$mime_filename); } functionattach_file($filename,$downfilename,$mimetype,$mime_filename){ $encoded=$this->encode_file($filename); if($mime_filename)$filename=$mime_filename; $out="--".$this->mime_boundary."\n"; $out=$out."Content-type:".$mimetype.";name=\"$filename\";\n"; $out=$out."Content-Transfer-Encoding:base64\n"; $out=$out."Content-disposition:attachment;filename=\"$downfilename\"\n\n"; $out=$out.$encoded."\n"; $out=$out."--".$this->mime_boundary."--"."\n"; return$out; } functionencode_file($sourcefile){ if(is_readable($sourcefile)){ $fd=fopen($sourcefile,"r"); $contents=fread($fd,filesize($sourcefile)); $encoded=chunk_split(base64_encode($contents)); fclose($fd); } return$encoded; } functionsendfile(){ $headers=$this->smtp_headers.$this->mime_headers; $message=$this->text_body.$this->text_encoded; mail($this->addr_to,$this->subject,$message,$headers); } functionwrite_body($msgtext){ $out="--".$this->mime_boundary."\n"; $out=$out."Content-Type:text/plain;charset=\"us-ascii\"\n\n"; $out=$out.$msgtext."\n"; return$out; } functionwrite_mimeheaders($filename,$mime_filename){ if($mime_filename)$filename=$mime_filename; $out="MIME-version:1.0\n"; $out=$out."Content-type:multipart/mixed;"; $out=$out."boundary=\"$this->mime_boundary\"\n"; $out=$out."Content-transfer-encoding:7BIT\n"; $out=$out."X-attachments:$filename;\n\n"; return$out; } functionwrite_smtpheaders($addr_from){ $out="From:$addr_from\n"; $out=$out."Reply-To:$addr_from\n"; $out=$out."X-Mailer:PHP3\n"; $out=$out."X-Sender:$addr_from\n"; return$out; } } /*用法-例如:mimetype为"image/gif" $mailfile=newCMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype); $mailfile->sendfile(); $subject--主题 $sendto--收信人地址 $replyto--回复地址 $message--信件内容 $filename--附件文件名 $downfilename--下?的文件名 $mimetype--mime类型 */ ?>
Demo示例文件如下:
<?php require_once("emailclass.php"); //发送邮件 //主? $subject="testsendemail"; //收件人 $sendto="abc@163.com"; //?件人 $replyto="cdf@163.com"; //?热 $message="testsendemailcontent"; //附件 $filename="test.jpg"; //附件??e $mimetype="image/jpeg"; $mailfile=newCMailFile($subject,$sendto,$replyto,$message,$filename,$excelname,$mimetype); $mailfile->sendfile(); ?>
相信本文所述对大家php程序设计的学习有一定的借鉴价值。
相关文章
- 微信授权网页扫码登录php,PHP实现微信开放平台扫码登录源码
- php静态延迟方法,phpstatic延迟静态绑定_PHP教程
- PHP连接linux并执行命令,php实现ssh远程连接服务器并操作服务器,PHP跨服务器执行shell脚本
- PHP扩展开发:实现Redis读写数据(php扩展redis)
- 借助 PHP 与 MySQL 轻松实现文件下载(php和mysql下载)
- MySQL验证:PHP实现细节(php验证mysql)
- 内 php 耗时查看Oracle中PHP耗时的小时分解(oracle查询小时)
- PHP操纵Redis实例:快速高效存储数据(php操作redis实例)
- 「Linux下的PHP开发」(linux开发php)
- PHP中使用Redis缓存数据的优势(php使用redis)
- 一步一步教你PHP操作MSSQL的下载方法(php_mssql下载)
- PHP与MSSQL的良好配合,助力数据应用升级(php_mssql)
- PHP图片上传至MySQL:实现快速可靠的数据存储(php图片上传mysql)
- PHP操作Redis队列实现数量控制(redis队列数量php)
- 一个实例告诉你:Kaggle 数据竞赛都有哪些套路
- PHP实现多条件查询实例代码
- php调用dll的实例操作动画与代码分享
- PHP的curl实现get,post和cookie(实例介绍)
- php使用异或实现的加密解密实例
- js判断页面中是否有指定控件的简单实例
- PHP批量检测并去除文件BOM头代码实例
- php函数serialize()与unserialize()用法实例
- 两个php日期控制类实例
- jQuery+PHP实现的掷色子抽奖游戏实例
- PHP中使用sleep函数实现定时任务实例分享
- php实现的Captcha验证码类实例