php - 从字符串创建文件并附加到电子邮件 [英] php - Create file from string and attach to email

查看:115
本文介绍了php - 从字符串创建文件并附加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以向我发送反馈。当他们这样做时,系统中的各种数据被发布到我的服务器上的php文件。然后,php文件处理该数据,并通过mail()向我发送与该数据的电子邮件。现在,所有的数据只是组成一个HTML电子邮件。



我想做的是采取一些发布的数据,将它们转换成文件,并添加他们作为电子邮件的附件。我知道如何添加文件作为附件与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆