问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/5480383/php-create-file-from-string-and-attach-to-email