Reputation: 13
Ok, I've gotten the attachment working in the email and it's being sent correctly thanks to this bit that I found here
What this is getting me is an email with an attachment, but none of the other form data is being injected into the email. I'm sure it's a problem with the syntax that I'm using, but I con't for the life of me figure out what I'm doing wrong. I've looked at about a billion other form examples that I've found here (and other less helpful nooks and crannies of the web)
Also, before the downvotes pour in, I realize I haven't done any sort of validation yet, and I'm not forwarding the user to a success URL after the message has been sent. I'm just trying to get the script working the way I think it should before I start going nuts over validating it.
Here is the html for my form:
<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>
And here is the php from 'mail.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 = "[email protected]";
$to = "[email protected]";
$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.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]);
}
}
}
?>
Upvotes: 1
Views: 1161
Reputation: 29314
That's because $_POST["submit"]
is never set - your button doesn't have a name.
Upvotes: 2