PHP联系人表单w/文件上传附件
我有一个简单的PHP联系人表单(),我想在其中添加一个文件上传选项,这样客户端就可以附加一个重要的文档,并使用PHP的mail函数将其发送给我 表单本身运行良好,但我似乎无法获得上传附件、将附件临时存储在服务器上并作为电子邮件的一部分发送给我的代码。以下是我正在使用的代码:PHP联系人表单w/文件上传附件,php,file-upload,mime,Php,File Upload,Mime,我有一个简单的PHP联系人表单(),我想在其中添加一个文件上传选项,这样客户端就可以附加一个重要的文档,并使用PHP的mail函数将其发送给我 表单本身运行良好,但我似乎无法获得上传附件、将附件临时存储在服务器上并作为电子邮件的一部分发送给我的代码。以下是我正在使用的代码: <?php if ($_POST['test'] != '') { echo 'Unfortunately, by filling out the hidden field, you have been id
<?php
if ($_POST['test'] != '') {
echo 'Unfortunately, by filling out the hidden field, you have been identified as a potential spambot and your message has been terminated.';
} else {
//Validate the name:
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
echo "You forgot to enter your name.<br>";
}
//Validate the phone:
if (!empty($_POST['phone'])) {
$phone = $_POST['phone'];
} else {
echo "You forgot to enter your phone number.<br>";
}
//Validate the e-mail:
if (!empty($_POST['email'])) {
$email = $_POST['email'];
} else {
echo "You forgot to enter your e-mail.<br>";
}
//Validate the message:
if (!empty($_POST['message'])) {
$message = $_POST['message'];
} else {
echo "You forgot to enter a message.";
}
if (!empty($_POST['name']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['message'])) {
// Obtain file upload variables:
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment_name = $_FILES['attachment']['name'];
if (file($attachment)) {
// Read the file to be attached ('rb' = read binary):
$file = fopen($attachment,'rb');
$data = fread($file,filesize($attachment));
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: {$attachment_type};\n" .
" name=\"{$attachment_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$attachment_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
$body = "$name\n$phone\n$email\n\n$message";
mail("*@*.com", "Starcrest Escrow, Inc. Website - Real Property Sale", $body, $headers);
header("Location: confirm.html");
}
}
?>
当前运行此脚本时,它会将我转发到确认页面,但似乎根本不会生成电子邮件。我做错了什么?
<?php
if (isset($_POST['txtEmail'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xyz@abc.com";
$email_subject = "Subject";
$email_from = "abc@xyz.com";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['txtName']) || !isset($_POST['txtEmail']) || !isset($_POST['txtAddress']) || !isset($_POST['txtContact']) || !isset($_POST['txtUpload'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['txtName']; // required
$email = $_POST['txtEmail']; // required
$address = $_POST['txtAddress']; // required
$contact = $_POST['txtContact']; // not required
$upload = $_POST['txtUpload']; // required
$email_message = "Form Details are below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message.= "Full Name: " . clean_string($name) . "\n\n";
$email_message.= "Address: " . clean_string($address) . "\n\n";
$email_message.= "Email ID: " . clean_string($email) . "\n\n";
$email_message.= "Contact No.: " . clean_string($contact) . "\n\n";
$email_message.= "File: " . clean_string($upload) . "\n\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
感谢您联系我们。我们将很快与您联系。
您是否收到任何服务器错误?$data中是否包含附件的内容?我意识到我的HTML表单中没有指向正确的脚本-哎呀!现在已经修复了,它会发送包含所有信息的电子邮件。但是,附件在邮件底部显示为文本数据。例如,一个PDF文件变成了许多行“JVBERi0xLjMKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29k”。知道发生了什么吗?谢谢对这种情况正在发生:$file=fopen($attachment,'rb')代码>&$data=chunk_split(base64_encode($data))代码>我很抱歉这么密集,但为什么这些行会导致它显示为这样,而不是PDF附件?我是否遗漏了代码中的一个重要步骤?不要尝试自己构建MIME附件……这是一项复杂的工作:……您是否在询问如何将此代码添加到OP的问题中??蝙蝠棒用于短代码段,四个空格用于多行,请参阅。但也要阅读指南;)(基本上:解释你的代码,如果你是从某处复制的,请提及代码的来源)