【问题标题】:PHP Email headers? Mime mistakes or something else...help to understand?PHP电子邮件标题?模仿错误或其他什么...帮助理解?
【发布时间】:2014-04-03 13:28:12
【问题描述】:

我是 php 的大三学生。现在我尝试通过 php mail() 发送一些附件,但是当我要阅读邮件时,它仅以文本形式显示(请参阅此处)。附件内容也是空白的。我使用在互联网上找到的邮件表格。我认为错误在于 /r/n 或 /n 之类的东西。对不起床英语。如果有问题,我可以解释更多,等待您的帮助!抱歉发布了太多代码,但我认为有必要了解...

表格

<form action="mailme.php"  method="post" enctype="multipart/form-data">
-+-+-  other inputs and select -+-+-

<input name="upload[]" type="file">
<input name="upload[]" type="file">

...other file attachments like that above
</form>

PHP

Retrive variables and then 
$headers ="From: ".$SenderName." <".$from.">\n";

// boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" .   "
                 boundary=\"{$mime_boundary}\""; 

// multipart boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n"
               . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . 
                 "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n";

// preparing attachments
    for($x=0;$x<count($files);$x++){
            $file = fopen($files[$x],"rb");
            $data = fread($file,filesize($files[$x]));
            fclose($file);
            $data = chunk_split(base64_encode($data));
            $message .= "Content-Type: {\"application/octet-stream\"};\n" .  

                        " name=\"$files[$x]\"\n" . 
            "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
            $message .= "--{$mime_boundary}\n";
    }

    // send

    $success = @mail($to, $subject, $message, $headers); 
    if ($success) { 
            echo "<p>mail sent to $to!</p>"; 
    } else { 
            print_r($files);
echo "<p>mail could not be sent!</p>".$from.'<br>'.$message.'<br>'.$to.'<br>'.$subject.'<br>'.$SenderName; 
    } 

输出

MIME-Version: 1.0
Content-Type: multipart/mixed;

boundary="==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx"

This is a multi-part message in MIME format.

--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Message <b>with<b> some <a href="html">html</a>!

--==Multipart_Boundary_x9351bff6284cb1fc967281cfede5762cx
Content-Type: {"application/octet-stream"};
name=""
Content-Disposition: attachment;
filename=""
Content-Transfer-Encoding: base64

.....BLANK CONTENT! LIKE NO IMAGE WAS ATTACHED......    


--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx

【问题讨论】:

  • 没有想法?发送带有多个附件的电子邮件的另一个教程或其他东西?可能是表单样式不对……
  • $files 来自哪里?
  • 抱歉耽搁了 $files = $_FILES["upload"];
  • 对吗?还是我必须使用其他东西?

标签: php html email mime-types


【解决方案1】:

使用var_dump($files); 显示其中包含的内容。 should contain an array 为每个上传的文件,包括名称、类型、大小、tmp_name 和错误(如果发生错误)

所以你的循环应该是这样的:

for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x]['tmp_name'],"rb");
        //                      ^ path to the uploaded file
        $data = fread($file,filesize($files[$x]['tmp_name']));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" .  

                    " name=\"$files[$x]['name']\"\n" . 
        //                              ^ filename
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]['name']\"\n" . 
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
}

您也可以使用$files[$x]['type'] 作为内容类型,但我不相信上传浏览器提供的内容。

【讨论】:

  • 谢谢我现在试试!
猜你喜欢
  • 2011-08-02
  • 2011-07-08
  • 2014-03-02
  • 2011-11-14
  • 1970-01-01
  • 2021-07-14
  • 2013-04-03
  • 2012-05-16
  • 1970-01-01
相关资源
最近更新 更多