简体   繁体   English

php-从字符串创建文件并附加到电子邮件

[英]php - Create file from string and attach to email

I have an application in which users can send me feedback. 我有一个用户可以向我发送反馈的应用程序。 When they do this, various data from their system is posted to a php file on my server. 当他们这样做时,来自他们系统的各种数据将发布到我服务器上的php文件中。 The php file then handles that data and sends me an email with that data via mail(). 然后,php文件处理该数据,并通过mail()向我发送包含该数据的电子邮件。 Right now, all of the data is just composed into an HTML email. 现在,所有数据都只是组成了HTML电子邮件。

What I want to do is take some of the posted data, convert them to files, and add them as attachments to the email. 我要做的是获取一些发布的数据,将其转换为文件,并将其作为附件添加到电子邮件中。 I know how to add files as attachments with mail(). 我知道如何使用mail()将文件添加为附件。 I just can't think of a way to create the files first. 我只是想不出一种先创建文件的方法。 I thought of just using fopen() and fwrite(), but then what if two users are sending feedback at the same time, the files would get all messed up since they would have the same name for each user. 我想到了只使用fopen()和fwrite(),但是如果两个用户同时发送反馈该怎么办,文件将变得一团糟,因为每个用户的名字都相同。 I know php has a temp file function which generates a file with a random name, but then how would I reference that file to add to my mail headers for mail(). 我知道php有一个临时文件功能,该文件会生成一个带有随机名称的文件,但是我将如何引用该文件以添加到我的mail()邮件头中。 Also, the temp file function won't work because I want each file to have a specific name when sent to me. 另外,临时文件功能不起作用,因为我希望每个文件在发送给我时都有一个特定的名称。

Any ideas on how to accomplish this? 关于如何做到这一点的任何想法?

Temporary files are created safely with tempnam . 使用tempnam安全地创建临时文件。 How you are going to call them while attaching them to a multi-part mail does not need to have anything to do with the temporary name you give the actual file. 将它们附加到多部分邮件时如何调用它们并不需要与您赋予实际文件的临时名称有任何关系。 If it does in your 'mail' implementation, show that specific code, 'cause it doesn't need to be like that. 如果它在您的“邮件”实现中显示,则显示该特定代码,因为它不需要那样。

Actually, to mail it, you don't even need to create a file, just add it as a specific part to the body of the mail with proper content-encoding & boundaries, and you're done. 实际上,要发送该邮件,您甚至不需要创建文件,只需将其作为特定部分添加到邮件正文中,并使用适当的内容编码和边界就可以完成。

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

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