邮件附件问题 - 为什么我没有正确的文件? [英] mail attachment issue - why do i dont get the file properly?

查看:168
本文介绍了邮件附件问题 - 为什么我没有正确的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个表单,并提供上传文件的选项,并将其发送到我的电子邮件。
,例如用户可以将他的信息放在输入字段中,并添加一个文件,当用户提交文件时,我将把它全部寄给我的邮件。



这是我的代码:

 <?php 

if(isset($ _ FILES) &((bool)$ _FILES){

$ allowedExtensions = array(pdf,doc,docx,gif,jpeg,jpg , RTF, TXT);

$ files = array();
foreach($ _ FILES as $ name => $ file){
$ file_name = $ file ['name'];
$ temp_name = $ file ['tmp_name'];
$ file_type = $ file ['type'];
$ path_parts = pathinfo($ file_name);
$ ext = $ path_parts ['extension'];
如果(!in_array($ ext,$ allowedExtensions)){
die(File $ file_name的扩展名为$ ext,不允许);
}
array_push($ files,$ temp_name);
}

//电子邮件字段:to,from,subject等等
$ to =dorozenman@gmail.com;
$ from = $ _POST ['sender_email'];
$ subject =test attachment;
$ message =here ya go;
$ headers =From:$ from;

//边界
$ semi_rand = md5(time());
$ mime_boundary === Multipart_Boundary_x {$ semi_rand} x;

//附件标题
$ headers。=\\\
MIME-Version:1.0\\\
。 Content-Type:multipart / mixed; \\\
。 boundary = \{$ mime_boundary} \;

// multipart boundary
$ message =这是MIME格式的多部分消息.\\\
\\\
。 - {$ mime_boundary} \\\
。 Content-Type:text / plain; charset = \iso-8859-1\\\\
。 Content-Transfer-Encoding:7bit\\\
\\\
。 $消息。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;

//准备附件
($ x = 0; $ x< count($ files); $ x ++){
$ file = fopen($ files [$ x ], RB);
$ data = fread($ file,filesize($ files [$ x]));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ message。=Content-Type:{\application / octet-stream\}; \\\
。 name = \$ files [$ x] \\\\

Content-Disposition:attachment; \\\
。 filename = \$ files [$ x] \\\\

Content-Transfer-Encoding:base64\\\
\\\
。 $数据。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;
}

//发送

$ ok = @mail($ to,$ subject,$ message,$ headers);
if($ ok){
echo< p>邮件发送到$到!< / p>;
} else {
echo< p>邮件无法发送!< / p>;
}
}

?>

(和文件到达命名为... tmp / phpcVjk4w /)任何建议?

解决方案

更改:

  array_push ,$ temp_name); 

to:

 code> array_push($ files,array('temp_name'=> $ temp_name,'file_name'=> $ file_name)); 

并更改:

 ($ x = 0; $ x< count($ files); $ x ++){
$ file = fopen($ files [$ x] RB);
$ data = fread($ file,filesize($ files [$ x]));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ message。=Content-Type:{\application / octet-stream\}; \\\
。 name = \$ files [$ x] \\\\

Content-Disposition:attachment; \\\
。 filename = \$ files [$ x] \\\\

Content-Transfer-Encoding:base64\\\
\\\
。 $数据。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;
}

To:



<$ ($ x = 0; $ x< count($ files); $ x ++){
$ temp_name = $ files [$ x ] [ 'temp_name']; //临时文件位置
$ file_name = $ files [$ x] ['file_name']; // filename
$ file = fopen($ temp_name,rb);
$ data = fread($ file,filesize($ temp_name));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ message。=Content-Type:{\application / octet-stream\}; \\\
。 name = \$ file_name\\\\

Content-Disposition:attachment; \\\
。 filename = \$ file_name \\\\

Content-Transfer-Encoding:base64\\\
\\\
。 $数据。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;
}

这里的区别是将文件名拖到$ files数组中,临时文件位置。您需要两者,服务器上的文件在哪里,以及上传文件的名称。


i try to create a form with a option to upload files and to get it to my email . for example that a user can put his info in the input fields and to add a file and when the user submit it i will get it all to my mail.

this is my code :

<?php

if(isset($_FILES) && (bool) $_FILES) {

$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");

$files = array();
foreach($_FILES as $name=>$file) {
    $file_name = $file['name']; 
    $temp_name = $file['tmp_name'];
    $file_type = $file['type'];
    $path_parts = pathinfo($file_name);
    $ext = $path_parts['extension'];
    if(!in_array($ext,$allowedExtensions)) {
        die("File $file_name has the extensions $ext which is not allowed");
    }
    array_push($files,$temp_name);
}

// email fields: to, from, subject, and so on
$to = "dorozenman@gmail.com";
$from = $_POST['sender_email']; 
$subject ="test attachment"; 
$message = "here ya go";
$headers = "From: $from";

// boundary 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

// headers for attachment 
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

// multipart boundary 
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
$message .= "--{$mime_boundary}\n";

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

// send

$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
    echo "<p>mail sent to $to!</p>"; 
} else { 
    echo "<p>mail could not be sent!</p>"; 
} 
   }    

   ?>

(and file arrive named... tmp/phpcVjk4w/ ) any suggestions?

解决方案

Change:

array_push($files,$temp_name);

to:

array_push($files, array('temp_name' => $temp_name, 'file_name' => $file_name));

And change:

// preparing attachments
for($x=0;$x<count($files);$x++){
    $file = fopen($files[$x],"rb");
    $data = fread($file,filesize($files[$x]));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

To:

// preparing attachments
for($x=0;$x<count($files);$x++){
    $temp_name = $files[$x]['temp_name']; // temporary file location
    $file_name = $files[$x]['file_name']; // filename
    $file = fopen($temp_name,"rb");
    $data = fread($file,filesize($temp_name));
    fclose($file);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name\"\n" . 
    "Content-Disposition: attachment;\n" . " filename=\"$file_name\"\n" . 
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
    $message .= "--{$mime_boundary}\n";
}

The difference here is pulling the filename into the $files array along with the temporary file location. You need both, where is the file on the server, and what is the name of the file when uploaded.

这篇关于邮件附件问题 - 为什么我没有正确的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆