php并无“组件”概念
发送“附件”时附件内容被嵌入邮件的头中

解决方案 »

  1.   

    那发送文件如何在PHP中实现?
      

  2.   

    如何发送附件(转)    
     我经常听到这样一个问题:"我有一个从网站发来的合同。我如何给通过表单发送的电子邮件增加一个附件呢?" 
    首先我要说的是要做到这个没有什么简单的办法。你要很好的理解PHP或
    其它的服务器端的脚本语言。当然你还要一个真正支持PHP的网站的账号。如果满足了这个前提,在你读完了本章后就可以用PHP发送带附件的电子邮件了。 1. 附件是如何工作的 如果你曾经在PHP的手册中搜索过"附件"函数,那么结果可能是什么都没有(至少在我写本文的时间还没有)。后来你就要花很多时间来了解这方面的知识。 你也许会想当你给某个人发送一封带附件的电子邮件时,附件是和邮件一起放到收件人的信箱里的(比如,如果你给他/她发了一个PNG的图片文件,他/她的信箱里会包含一个txt文件(电子邮件)和一个.png文件(附件))。但这不是它的工作原理。当你加入一个附件时,你的邮件程序把附件转换成纯文本文件,并在你写的内容(实际的电子邮件)后面插入这个文本块。这个,当你把所有的东西发出来后,收件人的信箱里只有一个纯文本文件--一个同时包含附件和实际电子邮件内容的文件。 下面是一个带附件(一个HTML文件)电子邮件的例子。我已经标注了其中重要的几行: Return-Path: <[email protected]
    Date: Mon, 22 May 2000 19:17:29 +0000 
    From: Someone <[email protected]
    To: Person <[email protected]
    Message-id: <[email protected]
    Content-type: multipart/mixed; boundary="396d983d6b89a" 
    Subject: Here's the subject --396d983d6b89a 
    Content-type: text/plain; charset=iso-8859-1 
    Content-transfer-encoding: 8bit This is the body of the email. --396d983d6b89a 
    Content-type: text/html; name=attachment.html 
    Content-disposition: inline; filename=attachment.html 
    Content-transfer-encoding: 8bit <html> 
    <head> 
    <title>The attachment</title> 
    </head> 
    <body> 
    <h2>This is the attached HTML file</h2> 
    </body> 
    </html> --396d983d6b89a-- 前面的7行是邮件的头,其中值得注意的是Content-type头部分。这个头告诉邮件程序电子邮件是由一个以上的部分组成的。不含附件的邮件只有一个部分:消息本身。带附件的电子通常至少由两部分组成:消息和附件。这样,带两个附件的邮件由三部分组成:消息,第一个附件和第二个附件。 带附件的电子邮件的不同部分之间用分界线来分隔。分界线在Content-type头中定义。邮件的每个新部分以两个连字号(--)和分界线开始。最后一个分界线后也有两个连字号,表示这个邮件中没有其它的部分了。 在每个分界线后有一些行,用来告诉邮件程序这个部分的内容的类型。比如,看看上面例子中第一个分界线后面的两行--以Content-type: text/plain开头的行。这些行说明后面的部分是ISO-8859-1字符集的纯文本。跟在第二个分界线后的行告诉邮件程序现在的部分是一个HTML文件,它的名字是"attachment.html"。 Content-disposition这持告诉邮件程序如果可能就以内嵌的方式显示附件。现在新的邮件程序会在消息后显示HTML的内容。如果Content-disposition被设为attachment,那么邮件程序就不会显示HTML文件的内容,而是显示一个连接到文件的图标(或其它的类似的东西)。收件人要看附件的内容,必须点击这个图标。一般情况下,如果附件是一些文本(包含HTML),Content-disposition会被设为inline,这是因为现在大部分邮件程序能够不借助其它浏览器而直接显示附件(文本)的内容。如果附件不是文本(比如图片或其它类似的内容),Content-disposition就设为attachment。 2. 用PHP生成带附件的电子邮件 这里一个例子,告诉你如果把一个定义好的HTML文件加为邮件的附件: <?php 
    # 我们首先写实际的消息内容 
    $emailBody = "This is text that goes into the body of the email."; # 然后我们要作为附件的HTML文件 
    $attachment = "<html> 
    <head> 
    <title>The attached file</title> 
    </head> 
    <body> 
    <h2>This is the attached HTML file</h2> 
    </body> 
    </html>"; # 建立在邮件中分隔不同部分的分界线。 
    # 基本上,分界线可以是任意的字符串。 
    # 但是重要的一点是确定一个写邮件的人 
    # 这会随意写出的字符串,所以我们用 
    # uniqid函数来产生一个随机的字符串。 
    $boundary = uniqid( ""); # 现在我们要建立邮件头。不要忘了插入 
    # Content-type头来说明这个邮件包含一个或更多的附件。 
    $headers = "From: [email protected] 
    Content-type: multipart/mixed; boundary=\"$boundary\""; # 好,现在我们已经有了邮件的所有内容。 
    # 下一件事是修改邮件的主体。 
    $emailBody = "--$boundary 
    Content-type: text/plain; charset=iso-8859-1 
    Content-transfer-encoding: 8bit $emailBody --$boundary 
    Content-type: text/html; name=attachment.html 
    Content-disposition: inline; filename=attachment.html 
    Content-transfer-encoding: 8bit $attachment --$boundary--"; # 现在可以把邮件发出去了 
    mail( "[email protected]", "The subject", $emailBody, $headers); 
    ?> 3. 把用户上传的文件作为附件 你也许会觉得上面的例子难以理解,但下面...。在下面的例子中事情更难了,因为我们要用一个表单让用户上传他们的文件,并把这个文件作为我们要发的邮件的附件。麻烦的是我们不能预先知道文件的MIME类型。在前面的例子中,我们已经知道该它是一个HTML文件,所以给这个附件设置Content-type头是很简单的。在下面的例子中,MIME类型可能是任意的,因为用户可能会上传一个HTML文件,一个PNG文件,一个vCard文件,或者其它的东西。让我们来看看例子: <?php 
    # 现在我们来生成表单。在产生可以上传文件的表单时, 
    # 不要忘了把<form>标签的"enctype"属性设为"multipart/form-data". 
    echo "<form action='$PHP_SELF' enctype='multipart/form-data' method='post'>\n"; 
    echo "<input type='text' name='from'><br>\n"; 
    echo "<input type='text' name='to'><br>\n"; 
    echo "<input type='text' name='subject'><br>\n"; 
    echo "<input type='file' name='attachment'><br>\n"; 
    echo "<textarea name='body'></textarea><br>\n"; 
    echo "<input type='submit' name='send' value='Send'>\n"; 
    echo "</form>\n"; # 如果用户已经按了"Send"按钮" 
    if ($send) { 
    # 定义分界线 
    $boundary = uniqid( ""); # 生成邮件头 
    $headers = "From: $from 
    Content-type: multipart/mixed; boundary=\"$boundary\""; # 确定上传文件的MIME类型 
    if ($attachment_type) $mimeType = $attachment_type; 
    # 如果浏览器没有指定文件的MIME类型, 
    # 我们可以把它设为"application/unknown". 
    else $mimeType = "application/unknown"; # 确定文件的名字 
    $fileName = $attachment_name; # 打开文件 
    $fp = fopen($attachment, "r"); 
    # 把整个文件读入一个变量 
    $read = fread($fp, filesize($attachment)); # 好,现在变量$read中保存的是包含整个文件内容的文本块。 
    # 现在我们要把这个文本块转换成邮件程序可以读懂的格式 
    # 我们用base64方法把它编码 
    $read = base64_encode($read); # 现在我们有一个用base64方法编码的长字符串。 
    # 下一件事是要把这个长字符串切成由每行76个字符组成的小块 
    $read = chunk_split($read); # 现在我们可以建立邮件的主体 
    $body = "--$boundary 
    Content-type: text/plain; charset=iso-8859-1 
    Content-transfer-encoding: 8bit $body --$boundary 
    Content-type: $mimeType; name=$fileName 
    Content-disposition: attachment; filename=$fileName 
    Content-transfer-encoding: base64 $read --$boundary--"; # 发送邮件 
    mail($to, $subject, $body, $headers); 

    ?> 
    这就是全部内容。如果你不能很好地理解上面的例子,我的建议是给你自己发送几个带附件的邮件,然后仔细研究邮件的源代码。
     
     
      

  3.   

    一个小巧的发邮件的函数(可带多个任意类型附件) 
    加入日期: 2000年5月13日 
    代码标题: 一个小巧的发邮件的函数(可带多个任意类型附件) 
    提交人: Danke 
    代码作者: D. Cheetah Xie  
    作者主页: http://psyche.pku.edu.cn/  
      ->点这里看带语法加亮的代码<- 
    // Adapted from PHPLIB for PHP 3.0.7+ 
    // By Danke Xie, Peking University function mail2( $to, $subject, $message, $from, $content_type, $attache="") 

    if ( !empty($from) ) $head = "From: $from\n"; 
    if ( empty($content_type) ) $content_type = "text/plain"; if ( is_array($attache) ) { 
    $boundary = "===" . md5(uniqid("")) . "==="; 
    $head .= "Mime-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"". $boundary ."\"\n\nThis is a multi-part message in MIME format.\n\n"; 
    $head .= "--" . $boundary . "\n"; 
    $head .= "Content-Type: $content_type\n"; 
    $head .= "\n" . $message . "\n\n"; while ( list( $key, $val ) = each($attache) ) { 
    $fd = fopen( "./$val", "r" ) or die("unable to open file ./$val"); 
    $contents = chunk_split(base64_encode( fread( $fd, filesize( "./$val" ) ) )); 
    fclose( $fd ); 
    $head .= "--" . $boundary . "\n"; 
    $head .= "Content-Type: application/octet-stream; name=\"" . basename($val) . "\"\nContent-Transfer-Encoding: BASE64\nContent-Disposition: attachment; filename=\"" . basename($val) . "\"\n"; 
    $head .= "\n" . $contents . "\n\n"; 

    $head .= "--" . $boundary . "--\n\n"; 
    }else{ 
    if ( !empty($content_type) ) { 
    $head .= "Content-Type: $content_type\n"; 
    $head .= "\n" . $message . "\n"; 


    return mail( "$to", "$subject", "", $head ); 
    } // Calling sample 
    mail2("[email protected]","Hello",$message,"[email protected]", 
    "",array("a.txt","b.pdf"));