dtwye28880 2017-06-30 17:32
浏览 77

如何重命名上传的文件并将其作为电子邮件附件(PHP)发送?

I have a php code(example.php) which extracts the email id(sender@example.com) from the following form, gets the uploaded file and sends an email to a recipient@example.com with the uploaded file as attachment and the extracted email id as the sender id. Then it redirects the user to a success.html page.

The html form is as below:

<form action="http://example.com/example.php" enctype="multipart/form-data" method="POST">
<label>Your Email <input name="sender_email" type="email" /> </label></p>
                <label>Attachment <input name="my_file" type="file" /></label> <label><input name="button" type="submit" value="Submit" /></label></p>
        </form>
        <p>
            &nbsp;</p>

The php for the form is:

<?php
if($_POST && isset($_FILES['my_file']))
{
$recipient_email    = 'recipient@example.com';
//Capture POST data from HTML form and Sanitize them, 
$from_email = filter_var($_POST["sender_email"], 
FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
$subject        = SUBMISSION_PG;
$message        = filter_var($_POST["message"], 
FILTER_SANITIZE_STRING); //message
//Get uploaded file data
$file_tmp_name    = $_FILES['my_file']['tmp_name'];
$file_name        = $_FILES['my_file']['name'];
$file_size        = $_FILES['my_file']['size'];
$file_type        = $_FILES['my_file']['type'];
$file_error       = $_FILES['my_file']['error'];
if($file_error > 0)
{
die('Upload error or No files uploaded');
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("sanwebe");
//header
$headers = "MIME-Version: 1.0
"; 
$headers .= "From:".$from_email."
"; 
$headers .= "Reply-To: ".$from_email."" . "
";
$headers .= "Content-Type: multipart/mixed; boundary = 
$boundary

"; 
//plain text 
$body = "--$boundary
";
$body .= "Content-Type: text/plain; charset=ISO-8859-1
";
$body .= "Content-Transfer-Encoding: base64

"; 
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundary
";
$body .="Content-Type: $file_type; name=".$file_name."
";
$body .="Content-Disposition: attachment; 
filename=".$file_name."
";
$body .="Content-Transfer-Encoding: base64
";
$body .="X-Attachment-Id: ".rand(1000,99999)."

"; 
$body .= $encoded_content;
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{       
header("Location:http://example.com/success.html");
}else{
die('Could not send mail! Please check your PHP mail 
configuration.');  
}
}
?>

I need to rename the uploaded filename to sender@example.com.extension then send the uploaded file as attachment.

I'd be thankful to anyone who can solve me this issue.

</div>
  • 写回答

1条回答 默认 最新

  • dongpu42006096 2017-06-30 18:03
    关注

    As found in this answer: https://stackoverflow.com/a/31428803/2311317

    On this line:

    $body .="Content-Type: $file_type; name=".$file_name."
    ";
    

    Change to

    $body .= "Content-Type: application/octet-stream; name=\"".$file_name."\"
    ";
    

    Dont forget to escape your double quotes in those lines and others where you place PHP variables.

    like this occurance:

    name=\"".$file_name."\"
    "
    

    Notice the extra quotes and escaped quotes.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)