好的,我已经让附件在电子邮件中工作并且由于我在这里找到的这一点,它被正确发送了
这给我的是一封带有附件的电子邮件,但没有其他表单数据被注入电子邮件。我确信这是我使用的语法问题,但我不知道我做错了什么。我已经查看了我在这里找到的大约十亿其他表单示例(以及其他不太有用的角落和缝隙)
此外,在downvotes涌入之前,我意识到我还没有做任何类型的验证,并且在消息发送后我没有将用户转发到成功URL。我只是想让脚本按照我认为应该的方式工作,然后再开始疯狂地验证它。
这是我的表单的html:
<form class="pure-form pure-form-stacked" enctype="multipart/form-data"
method="POST" action="mail.php">
<fieldset>
<legend>About You</legend>
<div class="pure-g">
<div class="pure-u-1 pure-u-md-1-3">
<label for="firstname">First Name</label>
<input id="firstname" name="firstname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="lastname">Last Name</label>
<input id="lastname" name="lastname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="email">E-Mail</label>
<input id="email" name="email" type="email" required>
</div>
<div class="pure-u-1 pure-u-md-2-3">
<label for="street">Street Address</label>
<input id="street" name="street" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="city">City</label>
<input id="city" name="city" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="zip">Zip</label>
<input id="zip" name="zip" type="text">
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" placeholder="xxx-xxx-xxxx" required>
</div>
</div>
<legend>About Your Company</legend>
<div class="pure-g" >
<div class="pure-u-1 pure-u-md-1-3">
<label for="companyname">Company Name</label>
<input id="companyname" name="companyname" type="text" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="companysize"># of Employees?</label>
<input id="companysize" name="companysize" type="number" required>
</div>
<div class="pure-u-1 pure-u-md-1-3">
<label for="companywebsite">Website</label>
<input id="companywebsite" name="companywebsite" type="url" required placeholder="eg www.mysite.com">
</div>
<div class="pure-u-1 pure-u-md-1-1">
<label for="companylogo">Upload your Logo</label>
<input id="companylogo" type="file" name="attachment[]" >
</div>
</div>
<button type="submit" class="pure-button pure-button-primary">Submit</button>
</fieldset>
这里是来自'mail。php'的php
<?php
if(isset($_POST['submit']))
{ //check form inputs
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$street = $_POST['street'];
$city = $_POST['city'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$companyname = $_POST['companyname'];
$companysize = $_POST['companysize'];
$companywebsite = $_POST['companywebsite'];
}
if( $_POST || $_FILES )
{
// email fields: to, from, subject, and so on
// Here
$from = "host@notareal.com";
$to = "setmeup@notareal.com";
$subject = "Setup Request from Free Setup Form";
$message = $firstname.$lastname.$email.$street.$city.$zip.$phone.$companyname.$companysize.$companywebsite;
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "nMIME-Version: 1.0n" . "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: 7bitnn" . $message . "nn";
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: base64nn" . $data . "nn";
}
$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]);
}
}
}
?>
这是因为$_POST["submit"]
从未设置-您的按钮没有名称。