我用我的反馈页面mail()
功能。上传文件的PHP邮件
有3个字段:名称,邮件和消息。
想要添加新字段 - 文件,能够上传文件并将它们发送到我的电子邮件。
一些限制:
.zip
和.rar
文件只允许- 文件不能超过
200kb
大小。
如何做到这一点,防止安全漏洞?
我用我的反馈页面mail()
功能。上传文件的PHP邮件
有3个字段:名称,邮件和消息。
想要添加新字段 - 文件,能够上传文件并将它们发送到我的电子邮件。
一些限制:
.zip
和.rar
文件只允许200kb
大小。如何做到这一点,防止安全漏洞?
要了解有关文件上传,请参阅Handling file uploads PHP手册
要发送E-带附件的邮件,使用PHP类像Swiftmailer而不是mail()
是一个好主意。
+1或'Zend_Mail' :) http://framework.zend.com/manual/en/zend.mail.html – robertbasic 2010-09-01 10:13:01
@robertbasic:+1暗示的Zend_Mail :) – Phliplip 2010-09-01 10:15:13
一些简单的解决方案?包括一个简单的文件上传大型图书馆不是一个好的解决方案我只需要一个小网站上的一个页面。 – James 2010-09-01 10:46:51
您可以使用此功能:
function mail_file($to, $subject, $messagehtml, $from, $fileatt, $replyto="") {
// handles mime type for better receiving
$ext = strrchr($fileatt , '.');
$ftype = "";
if ($ext == ".doc") $ftype = "application/msword";
if ($ext == ".jpg") $ftype = "image/jpeg";
if ($ext == ".gif") $ftype = "image/gif";
if ($ext == ".zip") $ftype = "application/zip";
if ($ext == ".pdf") $ftype = "application/pdf";
if ($ftype=="") $ftype = "application/octet-stream";
// read file into $data var
$file = fopen($fileatt, "rb");
$data = fread($file, filesize($fileatt));
fclose($file);
// split the file into chunks for attaching
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
// build the headers for attachment and html
$h = "From: $from\r\n";
if ($replyto) $h .= "Reply-To: ".$replyto."\r\n";
$h .= "MIME-Version: 1.0\r\n";
$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$h .= "This is a multi-part message in MIME format.\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-type:text/html; charset=iso-8859-1\r\n";
$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$h .= $messagehtml."\r\n\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
$h .= "Content-Transfer-Encoding: base64\r\n";
$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
$h .= $content."\r\n\r\n";
$h .= "--".$uid."--";
// send mail
return mail($to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h)) ;
}
http://www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/
你有没有考虑寻找到Zend框架?我首先开始只使用它的Zend_Mail类。爱上它,现在我在我所有的网站上都使用了ZF。使用ZF可以验证输入 - 比如你的文件,处理上传,将其连接到邮件,并在代码短短的几行发送。 – Phliplip 2010-09-01 09:53:53