Ada*_*vis 24 php email code-golf email-attachments mail-form
想象一下,想要在他们的网站上放置一个允许网站访问者上传文件和简单消息的用户,该消息将立即通过电子邮件发送(即,该文件未存储在服务器上,或者如果是仅暂时)作为文件附件与邮件正文中的注释.
有关详细信息,请访问http://a2zsollution.com/php-secure-e-mail/
实现这一目标的最简单方法是什么?
最简单的方面:
这几乎与以下相反:如何从PHP获取电子邮件及其附件.它几乎可以在使用PHP编译具有多个附件的电子邮件中得到解答,但它实际上并不显示代码.
Mal*_*ats 71
只是为了好玩,我以为我会把它搞砸.它最终变得比我想象的更棘手,因为我没有完全理解边界部分是如何工作的,最终我发现起始和结束' - '是重要的并且它离开了.
<?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-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: 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>
Run Code Online (Sandbox Code Playgroud)
真的非常准确,显然使用内联CSS隐藏表单有点便宜,你几乎肯定想要更多的反馈给用户!另外,我可能会花更多的时间来计算文件的实际Content-Type,而不是作弊和使用application/octet-stream,但这部分非常有趣.
小智 16
这篇http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment的组合
使用php上传文件示例可行.在上传文件示例中,而不是使用move_uploaded_file将其从临时文件夹中移出,您只需打开它:
$attachment = chunk_split(base64_encode(file_get_contents($tmp_file)));
Run Code Online (Sandbox Code Playgroud)
哪里 $tmp_file = $_FILES['userfile']['tmp_name'];
并将其作为附件发送,就像示例的其余部分一样.
所有在一个文件/自包含:
<? if(isset($_POST['submit'])){
//process and email
}else{
//display form
}
?>
Run Code Online (Sandbox Code Playgroud)
基于上面两个可用的例子,我认为这是一个快速的练习来获得你需要的工作.
PS它需要在Apache将其传递给PHP之前上传到某个地方,以便用它做它想做的事情.除非在配置文件中更改,否则默认情况下这是您系统的临时文件夹.
| 归档时间: |
|
| 查看次数: |
116221 次 |
| 最近记录: |