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

核能气质少年 提交于 2019-12-05 23:05:17

问题


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);


回答1:


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.




回答2:


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 );
}



回答3:


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



来源:https://stackoverflow.com/questions/11798457/send-email-with-attachment-in-php-without-saving-the-file-to-webserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!