简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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