?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); 

?> 

解决方案 »

  1.   

    /* $message: mailbody 
    $type: been sent mail type 
    $to: the mail to 
    $from: the man who sent mail 
    $title: the project of mail 
    */ 
    //用$type来选择你要发送的格式switch ($type){ 
    case 'html': 
    $header="Content-type:text/html;"; 
    break; 
    default : 
    $header=''; 
    } mail("$to", "$title", $message, "From: $from\nReply-To:$from\n".$header);