Send email with attachment in php without saving file on web server

I am creating a simple form that allows the user to “upload” a file and a comment field. after the user selects a file (maybe an image or a PDF) and click "Submit", I am not going to store the file on my web server, the file will be inserted into the email and sent to me.

My question is: how can I attach a file without saving it anywhere.

I do not want to use a third-party module.

Update:

$attachment = $_FILES["OrderList"]["tmp_name"]; $content = file_get_contents($attachment); $content = chunk_split(base64_encode($content)); 

There was an error:

The file name cannot be empty in C: \ dir \ orders \ upload.php on line 24

line 24 $content = file_get_contents($attachment);

+6
source share
3 answers

You already saved it when you accept the file download from PHP.

Just use it when it is stored in the tmp folder.

PHP will automatically delete it for you when the script ends.

+3
source

Today I faced a similar situation and found a solution, so here

 if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br>"; } else { $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); $newname= $_FILES["file"]["tmp_name"].".".$extension; rename($_FILES["file"]["tmp_name"],$newname); $attachments = array( $newname ); wp_mail('emailto send', 'title', 'message','',$attachments ); } 
+3
source

By downloading the file, it is saved on your server in the temp folder. The exception is the PUT method, in which case the file is sent directly to php: // input. After that, you can use any PHP mailing list you prefer.

PHP Guide for PUT Files

0
source

Source: https://habr.com/ru/post/922060/


All Articles