简体   繁体   English

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

[英]Email file attachments using MIME in PHP

I'm trying to use PHP to send an email with an attached file. 我正在尝试使用PHP发送带有附件的电子邮件。 I've followed the excellent tutorial at http://webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment and made the following changes: 我已经按照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 

Neither the original script or my amended script seems to attach any files to my emails. 原始脚本或我的修改脚本似乎都没有将任何文件附加到我的电子邮件中。 Anyone have any idea what I'm doing wrong. 任何人都知道我在做什么错。

Note: the files I'm attaching to my form are all text files (various file extensions eg .log, .txt etc). 注意:我附加到表单的文件都是文本文件(各种文件扩展名,例如.log,.txt等)。


OK, I've now proven I think that the MIME encoding part of this script isnt working. 好的,我现在证明我认为此脚本的MIME编码部分无法正常工作。 I've placed a text file in my upload directory and changed the script to read: 我已经在上载目录中放置了一个文本文件,并将脚本更改为:

--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 

When looking at the email source on receipt I see the full path and document name under Content-Type: text/plain; 当查看收据上的电子邮件来源时,我在Content-Type下看到完整的路径和文档名称:text / plain; name=, but theres no attachment, encoded or otherwise. name =,但没有附件,无论编码还是其他形式。

Anyone know how I get this to work? 有人知道我该如何工作吗?

I've tried the following: 我尝试了以下方法:

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

This presents the file name in the email source, but no attachment, interestingly: 有趣的是,这显示了电子邮件源中的文件名,但没有附件:

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

Returns an empty file name and still not attachement, I can only assume the temp file has bee deleted by the time I execute this? 返回一个空文件名并且仍然没有附件,我只能假定在执行此操作时临时文件已被删除?

OK finally got this working by moving the temp file to another location on my server, then encoding that file. 确定最终通过将临时文件移动到服务器上的另一个位置,然后对该文件进行编码来使此工作正常。

Not sure why I can't encode the temp file without saving it, but at least I got it working now. 不知道为什么不保存就不能对临时文件进行编码,但是至少我现在能使它工作。

Thanks to everyone who took the time to reply to this, you help me a lot. 感谢所有花时间回答此问题的人,您对我有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM