在PHP中使用MIME的电子邮件文件附件


Email file attachments using MIME in PHP

我正在尝试使用PHP发送带有附加文件的电子邮件。我在http://webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment并进行了以下更改:

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/txt; name="<?php echo $_POST[log_file]?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

无论是原始脚本还是我修改后的脚本似乎都没有在我的电子邮件中附加任何文件。任何人都知道我做错了什么。

注意:我附在表单上的文件都是文本文件(各种文件扩展名,如.log、.txt等)。


好吧,我现在已经证明了我认为这个脚本的MIME编码部分不起作用。我在上传目录中放置了一个文本文件,并将脚本更改为:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_SERVER['DOCUMENT_ROOT']. '/upload/test.txt' ?>" 
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

当查看收到的电子邮件源时,我会在内容类型下看到完整的路径和文档名称:text/plain;name=,但没有附件,无论是编码的还是其他的。

有人知道我是怎么做到的吗

我试过以下几种:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_FILES['file']['name']; ?>"
Content-Transfer-Encoding: base64  
Content-Disposition: attachment

这在电子邮件源中显示了文件名,但没有附件,有趣的是:

--PHP-mixed-<?php echo $random_hash; ?>   
Content-Type: text/plain; name="<?php echo $_FILES['file']['temp_name']; ?>"
Content-Transfer-Encoding: base64  
Content-Disposition: attachment 

返回一个空文件名,但仍然没有附件,我只能假设在执行此操作时临时文件已被删除?

OK通过将临时文件移动到服务器上的另一个位置,然后对该文件进行编码,最终实现了这一点。

不知道为什么我不能在不保存临时文件的情况下对其进行编码,但至少我现在可以工作了。

感谢所有花时间回复我的人,你们帮了我很多。