使用PHP通过HTML表单发送电子邮件附件
我在服务器上创建了“uploads”文件夹,用户在HTML表单上上传的文件可以很好地进入该文件夹。 接下来,我尝试从该文件夹中检索它并将其附加到电子邮件中,但这部分不起作用。 以下是我的PHP:使用PHP通过HTML表单发送电子邮件附件,php,html,Php,Html,我在服务器上创建了“uploads”文件夹,用户在HTML表单上上传的文件可以很好地进入该文件夹。 接下来,我尝试从该文件夹中检索它并将其附加到电子邮件中,但这部分不起作用。 以下是我的PHP: <?php $name = $_POST['name']; $email = $_POST['email']; $form_subject = $_POST['form_subject']; $message = $_POST['message']; //File upload //
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form_subject = $_POST['form_subject'];
$message = $_POST['message'];
//File upload
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
//End of file upload
$ip=@$REMOTE_ADDR;
$date=date("l, F j, Y, g:i a");
$to = "vlevsha@gmail.com";
$subject = "New data submitted!";
$body = "Here is the information submitted to
www.polycysticliverdisease.com/html/pld_form.php
from $ip on $date.\n\n
--------------------------------\n\n
name: $name \n\n
email address: $email \n\n
subject: $form_subject \n\n
comment: $message";
//mail($to, $subject, $body);
// Obtain file upload vars
$fileatt = $_FILES['uploadedfile']['tmp_name'];
$fileatt_type = $_FILES['uploadedfile']['type'];
$fileatt_name = $_FILES['uploadedfile']['name'];
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the message
$ok = @mail($to, $subject, $body, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
这是我的HTML文件上载控件:
<input type="submit" name="submit" class="submit" />
<input type="file" name="uploadedfile" class="upload" />
谢谢大家! 我会用一些邮件库——也许吧。自己编程是不值得的(当然,如果你不是为了学习而编程)。使用预构建的邮件程序包,如或。两者都可以轻松处理附件。非常感谢!它工作得很好。我爱速递!但是,当我为我的电子邮件设置正文时,我混合使用了文本和变量:$message->setBody('这是在$date从$ip提交到www.polycysticliverdisease.com/html/pld_form.php的信息。\n\n name:$name\n\n电子邮件地址:$email\n\n subject:$form_subject\n\n comment:$form_message,'text/html');所有变量都声明并分配了适当的值。但是它们和“\n\n”都作为文本的一部分出现在发送的电子邮件中,即它们不被识别为变量。为什么?@vlevsha因为你用的是单引号。使用双引号。谢谢,我甚至在看到你的答案之前就引用了。:)现在,我有一个更严重的问题:谢谢!我选择了斯威夫特梅勒。我就是喜欢!