您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

简单的PHP形式:电子邮件附件(代码高尔夫)

简单的PHP形式:电子邮件附件(代码高尔夫)

只是为了好玩,我以为我会把它搞砸。最终变得比我想象的要复杂,因为我没有完全了解边界部分的工作原理,最终我发现起点和终点“-”很重要,然后就消失了。

<?PHP
    if(isset($_POST['submit']))
    {
        //The form has been submitted, prep a nice thank you message
        $output = '<h1>Thanks for your file and message!</h1>';
        //Set the form flag to no display (cheap way!)
        $flags = 'style="display:none;"';

        //Deal with the email
        $to = 'me@example.com';
        $subject = 'a file for you';

        $message = strip_tags($_POST['message']);
        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time()));

        $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-@R_784_301@: 7bit

$message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-@R_784_301@: base64 
Content-Disposition: attachment

$attachment
--_1_$boundary--";

        mail($to, $subject, $message, $headers);
    }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?PHP echo $output; ?>

<form enctype="multipart/form-data" action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post" <?PHP echo $flags;?>>
<p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

真的很准,而且显然使用内联CSS隐藏表单有点便宜,您几乎可以肯定希望向用户提供更多反馈!此外,我可能会花更多的时间来确定文件的实际Content-Type,而不是欺骗和使用application / octet-stream,但是那部分还是很有趣的。

php 2022/1/1 18:14:21 有518人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶