send email with attachment in php without saving the file to webserver
Asked Answered
D

3

8

I am creating a simple form that let user "upload" a file, and a comments box. after the user choose the file(can be image or pdf) and click submit, i am not gonna store the file into my web server, the file will be inserted into an email and send to me.

my question is: how can i attach the file without storing it in any place.

I don't want to use third party module.

Update:

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

I got an error:

Filename cannot be empty in C:\dir\orders\upload.php on line 24

line 24 is $content = file_get_contents($attachment);

Despoliation answered 3/8, 2012 at 15:12 Comment(3)
Upload it, attach it, send it, delete it.Fluent
how about, save it temporary and then delete itMarla
PHP stores the uploaded file in a tmp directory before your script even begins so you won't be able to avoid saving the file to the filesystem.Clupeoid
B
4

You have already stored it when you accept the file-upload from PHP.

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

PHP will automagically delete it for you when your script ends.

Barragan answered 3/8, 2012 at 15:15 Comment(3)
but when i use $attachment = $_FILES["OrderList"]["tmp_name"]; $content = file_get_contents($attachment); $content = chunk_split(base64_encode($content)); i got an error: Filename cannot be empty in file_get_contentsDespoliation
Did you echo $attachment? What did it say? Are you running your code snippet from the script that is posted to when you upload?Barragan
i got it fixed by adding the tmp_dir in php.ini, the latest php(newer than 5.3) doesn't set the default tmp_dir. thanks a lot.Despoliation
L
7

Today i ran into similar situation and found a solution , so here it is

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 );
}
Lesson answered 3/8, 2012 at 15:12 Comment(2)
Yes you are right..! This is easiest way. If you want to retain original file name then you would change rename as below: rename($_FILES["file"]["name"],$newname);Claudy
@Claudy not that way, $_FILES["file"]["name"] stores the nice name without path. rather: $path=implode("/",explode("/",$_FILES["file"]["tmp_name"],-1)); if($path!="") $path.="/"; $newname=$path.$_FILES["file"]["name"]; rename($_FILES["file"]["tmp_name"],$newname);Parkman
B
4

You have already stored it when you accept the file-upload from PHP.

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

PHP will automagically delete it for you when your script ends.

Barragan answered 3/8, 2012 at 15:15 Comment(3)
but when i use $attachment = $_FILES["OrderList"]["tmp_name"]; $content = file_get_contents($attachment); $content = chunk_split(base64_encode($content)); i got an error: Filename cannot be empty in file_get_contentsDespoliation
Did you echo $attachment? What did it say? Are you running your code snippet from the script that is posted to when you upload?Barragan
i got it fixed by adding the tmp_dir in php.ini, the latest php(newer than 5.3) doesn't set the default tmp_dir. thanks a lot.Despoliation
S
0

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

PHP manual for PUT'ing files

Sharla answered 3/8, 2012 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.