php - 从字符串创建文件并附加到电子邮件 [英] php - Create file from string and attach to email
问题描述
我想做的是采取一些发布的数据,将它们转换成文件,并添加他们作为电子邮件的附件。我知道如何添加文件作为附件与mail()。我只是想不到首先创建文件的方法。我只想使用fopen()和fwrite(),但是如果两位用户同时发送反馈意见,那么文件会被搞砸,因为每个用户都有相同的名字。我知道php有一个临时文件功能,它生成一个随机名称的文件,但是如何引用该文件来添加到mail()的邮件头。此外,临时文件功能将不起作用,因为我希望每个文件在发送给我时都具有特定的名称。
有关如何实现这一点的任何想法? p>
临时文件使用 tempnam
安全创建。在将其附加到多部分邮件时,如何调用它们不需要与给出实际文件的临时名称有任何关系。如果它在你的'邮件'实现中显示出特定的代码,因为它不需要这样。
实际上,要邮寄,你甚至不需要创建一个文件,只需将其添加到具有适当内容编码和邮件的邮件正文中。边界,你完成了。
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. The php file then handles that data and sends me an email with that data via mail(). Right now, all of the data is just composed into an HTML email.
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(). 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. 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(). 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
. 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.
这篇关于php - 从字符串创建文件并附加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!