仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2135|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网页设计php写的发送附件的法式(二)

[复制链接]
柔情似水 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-16 00:26:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
毕业设计作品自己个人还是觉得比较满意的,尽管有些功能考虑的不全面,也没有很好的实现。   

<?php
  class html_mime_mail{
   
  var $headers;
  var $body;
  var $multipart;
  var $mime;
  var $html;
  var $html_text;
  var $html_images = array();
  var $cids = array();
  var $do_html;
  var $parts = array();
   
  /***************************************
  ** Constructor function. Sets the headers
  ** if supplied.
  ***************************************/
  function html_mime_mail($headers = ''){
  $this->headers = $headers;
  }
   
  /***************************************
  ** Adds a html part to the mail.
  ** Also replaces image names with
  ** content-id's.
  ***************************************/
  function add_html($html, $text){
  $this->do_html = 1;
  $this->html = $html;
  $this->html_text = $text;
  if(is_array($this->html_images) AND count($this->html_images) > 0){
  for($i=0; $i<count($this->html_images); $i++){
  $this->html = ereg_replace($this->html_images[$i]['name'], 'cid:'.$this->html_images[$i]['cid'], $this->html);
  }
  }
  }
   
  /***************************************
  ** Builds html part of email.
  ***************************************/
  function build_html($orig_boundary){
  $sec_boundary = '=_'.md5(uniqid(time()));
  $thr_boundary = '=_'.md5(uniqid(time()));
   
  if(!is_array($this->html_images)){
  $this->multipart.= '--'.$orig_boundary."\r\n";
  $this->multipart.= 'Content-Type: multipart/alternative; boundary="'.$sec_boundary."\"\r\n\r\n\r\n";
   
  $this->multipart.= '--'.$sec_boundary."\r\n";
  $this->multipart.= 'Content-Type: text/plain'."\r\n";
  $this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
  $this->multipart.= $this->html_text."\r\n\r\n";
   
  $this->multipart.= '--'.$sec_boundary."\r\n";
  $this->multipart.= 'Content-Type: text/html'."\r\n";
  $this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
  $this->multipart.= $this->html."\r\n\r\n";
  $this->multipart.= '--'.$sec_boundary."--\r\n\r\n";
  }else{
  $this->multipart.= '--'.$orig_boundary."\r\n";
  $this->multipart.= 'Content-Type: multipart/related; boundary="'.$sec_boundary."\"\r\n\r\n\r\n";
   
  $this->multipart.= '--'.$sec_boundary."\r\n";
  $this->multipart.= 'Content-Type: multipart/alternative; boundary="'.$thr_boundary."\"\r\n\r\n\r\n";
   
  $this->multipart.= '--'.$thr_boundary."\r\n";
  $this->multipart.= 'Content-Type: text/plain'."\r\n";
  $this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
  $this->multipart.= $this->html_text."\r\n\r\n";
   
  $this->multipart.= '--'.$thr_boundary."\r\n";
  $this->multipart.= 'Content-Type: text/html'."\r\n";
  $this->multipart.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
  $this->multipart.= $this->html."\r\n\r\n";
  $this->multipart.= '--'.$thr_boundary."--\r\n\r\n";
   
  for($i=0; $i<count($this->html_images); $i++){
  $this->multipart.= '--'.$sec_boundary."\r\n";
  $this->build_html_image($i);
  }
   
  $this->multipart.= "--".$sec_boundary."--\r\n\r\n";
  }
  }
  /***************************************
  ** Adds an image to the list of embedded
  ** images.
  ***************************************/
  function add_html_image($file, $name = '', $c_type='application/octet-stream'){
  $this->html_images[] = array( 'body' => $file,
  'name' => $name,
  'c_type' => $c_type,
  'cid' => md5(uniqid(time())) );
  }
   
   
  /***************************************
  ** Adds a file to the list of attachments.
  ***************************************/
  function add_attachment($file, $name = '', $c_type='application/octet-stream'){
  $this->parts[] = array( 'body' => $file,
  'name' => $name,
  'c_type' => $c_type );
  }
   
  /***************************************
  ** Builds an embedded image part of an
  ** html mail.
  ***************************************/
  function build_html_image($i){
  $this->multipart.= 'Content-Type: '.$this->html_images[$i]['c_type'];
   
  if($this->html_images[$i]['name'] != '') $this->multipart .= '; name="'.$this->html_images[$i]['name']."\"\r\n";
  else $this->multipart .= "\r\n";
   
  $this->multipart.= 'Content-ID: <'.$this->html_images[$i]['cid'].">\r\n";
  $this->multipart.= 'Content-Transfer-Encoding: base64'."\r\n\r\n";
  $this->multipart.= chunk_split(base64_encode($this->html_images[$i]['body']))."\r\n";
  }
   
  /***************************************
  ** Builds a single part of a multipart
  ** message.
  ***************************************/
  function build_part($i){
  $message_part = '';
  $message_part.= 'Content-Type: '.$this->parts[$i]['c_type'];
  if($this->parts[$i]['name'] != '')
  $message_part .= '; name="'.$this->parts[$i]['name']."\"\r\n";
  else
  $message_part .= "\r\n";
   
  // Determine content encoding.
  if($this->parts[$i]['c_type'] == 'text/plain'){
  $message_part.= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n";
  $message_part.= $this->parts[$i]['body']."\r\n";
  }else{
  $message_part.= 'Content-Transfer-Encoding: base64'."\r\n";
  $message_part.= 'Content-Disposition: attachment; filename="'.$this->parts[$i]['name']."\"\r\n\r\n";
  $message_part.= chunk_split(base64_encode($this->parts[$i]['body']))."\r\n";
  }
   
  return $message_part;
  }
   
  /***************************************
  ** Builds the multipart message from the
  ** list ($this->parts).
  ***************************************/
  function build_message(){
  $boundary = '=_'.md5(uniqid(time()));
   
  $this->headers.= "MIME-Version: 1.0\r\n";
  $this->headers.= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\r\n";
  $this->multipart = '';
  $this->multipart.= "This is a MIME encoded message.\r\nCreated by html_mime_mail.class.\r\nSee http://www.heyes-computing.net/scripts/ for a copy.\r\n\r\n";
   
  if(isset($this->do_html) AND $this->do_html == 1) $this->build_html($boundary);
  if(isset($this->body) AND $this->body != '') $this->parts[] = array('body' => $this->body, 'name' => '', 'c_type' => 'text/plain');
   
  for($i=(count($this->parts)-1); $i>=0; $i--){
  $this->multipart.= '--'.$boundary."\r\n".$this->build_part($i);
  }
   
  $this->mime = $this->multipart."--".$boundary."--\r\n";
  }
   
  /***************************************
  ** Sends the mail.
  ***************************************/
  function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){
   
  if($to_name != '') $to = '"'.$to_name.'" <'.$to_addr.'>';
  else $to = $to_addr;
  if($from_name != '') $from = '"'.$from_name.'" <'.$from_addr.'>';
  else $from = $from_addr;
  $this->headers.= 'From: '.$from."\r\n";
  //$this->headers.= $headers;
  mail($to, $subject, $this->mime, $this->headers);
  }
   
  /***************************************
  ** Use this method to deliver using direct
  ** smtp connection. Relies upon Manuel Lemos'
  ** smtp mail delivery class available at:
  ** http://phpclasses.upperdesign.com
  **
  ** void smtp_send( string *Name* of smtp object,
  ** string From address,
  ** array To addresses,
  ** array Headers,
  ** string The body)
  ***************************************/
  function smtp_send($smtp_obj, $from_addr, $to_addr){
  global $$smtp_obj;
  $smtp_obj = $$smtp_obj;
   
  if(substr($this->headers, -2) == "\r\n") $this->headers = substr($this->headers,0,-2);
  $this->headers = explode("\r\n", $this->headers);
   
  $smtp_obj->sendmessage($from_addr, $to_addr, $this->headers, $this->mime);
  }
   
  } // End of class.
  ?>
   
  
可以说你的马步已经扎的差不多了,接下来就要开始练把势的时候了,如果有条件的话,用笔或者打印一个简易的PHP手册在身上,时不时的摸出来看看,记得,去WC也不能放过(^2^)。
山那边是海 该用户已被删除
沙发
发表于 2015-2-16 02:34:24 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
小女巫 该用户已被删除
板凳
发表于 2015-2-23 19:39:00 | 只看该作者
有时候汉字的空格也能导致页面出错,所以在写代码的时候,要输入空格最好用引文模式。
精灵巫婆 该用户已被删除
地板
发表于 2015-2-27 10:14:45 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
深爱那片海 该用户已被删除
5#
发表于 2015-3-9 00:33:06 | 只看该作者
最后介绍一个代码出错,但是老找不到错误方法,就是 go to wc (囧),出去换换气没准回来就找到错误啦。
admin 该用户已被删除
6#
发表于 2015-3-17 01:10:26 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
若相依 该用户已被删除
7#
发表于 2015-3-20 07:14:13 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
莫相离 该用户已被删除
8#
发表于 2015-3-28 22:50:05 | 只看该作者
不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。
愤怒的大鸟 该用户已被删除
9#
发表于 2015-4-3 13:39:08 | 只看该作者
php里的数组为空的时候是不能拿来遍历的;(这个有点低级啊,不过我刚被这个边界问题墨迹了好长一会)
小妖女 该用户已被删除
10#
发表于 2015-4-4 00:16:02 | 只看该作者
因为blog这样的可以让你接触更多要学的知识,可以接触用到类,模板,js ,ajax
变相怪杰 该用户已被删除
11#
发表于 2015-4-15 15:16:26 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
再现理想 该用户已被删除
12#
发表于 2015-4-15 23:37:27 | 只看该作者
没接触过框架的人,也不用害怕,其实框架就是一种命名规范及插件,学会一个框架其余的框架都很好上手的。
13#
发表于 2015-4-24 11:44:36 | 只看该作者
作为一个合格的coder 编码的规范是必须,命名方面我推崇“驼峰法”,另外就是自己写的代码最好要带注释,不然时间长了,就算是自己的代码估计看起来都费事,更不用说别人拉。
因胸联盟 该用户已被删除
14#
发表于 2015-4-26 11:11:03 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
老尸 该用户已被删除
15#
发表于 2015-4-27 13:32:21 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
飘灵儿 该用户已被删除
16#
发表于 2015-5-5 13:28:50 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
第二个灵魂 该用户已被删除
17#
发表于 2015-6-30 21:14:17 | 只看该作者
,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。
小魔女 该用户已被删除
18#
发表于 2015-7-10 10:49:39 | 只看该作者
至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
海妖 该用户已被删除
19#
发表于 2015-7-12 04:06:36 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-2 01:34

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表