我的表单上有一个输入,允许用户浏览到文件位置。他们的想法是,他们可以将简历附加到他们准备提交的应用程序中。
<label class="description" for="element_5">Upload a File </label>
<div>
<input id="element_5" name="element_5" class="element file" type="file"/>
对于我的文字字段和下拉菜单,我一直在使用以下内容:
$experince = $_POST["experince"];
但是我不希望路径字符串我想要实际的文件。如何获取文件本身以及如何将其附加到电子邮件中?
此外,是否有一种简单的方法可以将附件限制为.DOC / .PDF?
答案 0 :(得分:3)
我最近做了类似的事情。这不是微不足道的。
您的电子邮件必须是多部分Mime消息。您将读入您的文件,对其进行编码(使用base64),然后将其放在正确位置的电子邮件消息字符串中。
这看起来像一个体面的教程(我希望我以前找到过): http://www.texelate.co.uk/blog/send-email-attachment-with-php/
但请注意,该教程有一些逃避问题:
$message . "“nn";
应该是:
$message . "\n\n";
您还可以查看Mail_Mime PEAR包以获取帮助: http://pear.php.net/package/Mail_Mime
答案 1 :(得分:3)
看看
答案 2 :(得分:0)
以下是我发送带附件的邮件的方法。一些代码是我的,其中一些代码来自http://php.net/manual/en/function.mail.php。此代码已经过试用和测试,因此应该可以使用。另请参阅sendmail是否已安装。如果在linux上ubuntu系统尝试sudo apt-get install sendmail来安装它。 关于此代码的最佳部分是它适用于多个文件上传。
文件名:index.php
<?php
if( $_POST || $_FILES )
{
// email fields: to, from, subject, and so on
// Here
$from = "someone@somewhere.com";
$to = "someone_else@somewhere_else.com";
$subject = "Mail with Attachment";
$message = "This is the message body and to it I will append the attachments.";
$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 = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
fixFilesArray($_FILES['attachment']);
foreach ($_FILES['attachment'] as $position => $file)
{
// should output array with indices name, type, tmp_name, error, size
$message .= "--{$mime_boundary}\n";
$fp = @fopen($file['tmp_name'],"rb");
$data = @fread($fp,filesize($file['tmp_name']));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".$file['name']."\"\n"."Content-Description: ".$file['name']."\n" ."Content-Disposition: attachment;\n" . " filename=\"".$file['name']."\";size=".$file['size'].";\n"."Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $from;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return 1; } else { return 0; }
}
//This function will correct file array from $_FILES[[file][position]] to $_FILES[[position][file]] .. Very important
function fixFilesArray(&$files)
{
$names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
foreach ($files as $key => $part) {
// only deal with valid keys and multiple files
$key = (string) $key;
if (isset($names[$key]) && is_array($part)) {
foreach ($part as $position => $value) {
$files[$position][$key] = $value;
}
// remove old key reference
unset($files[$key]);
}
}
}
?>
<html>
<body>
<form method="POST" action="index.php" enctype="multipart/form-data">
<input type="file" name="attachment[]"><br/>
<input type="submit">
</form>
</body>
</html>
顺便说一下,我想为使用sendmail道歉,因为它有点慢。我会尝试发布一个更好的解决方案。
答案 3 :(得分:-1)
这是我迄今为止所做的,但它不起作用
<input id="resumeup" name="resumeup" type="file"/>
<?php $_FILES = $_POST["resumeup"];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
} ?>