I need to email one or more attachments based on whether a checkbox is checked.
The script I have so far has two issues.
1) if more than one checkbox is checked, only the attachment associated with the first checkbox is sent. That problem could be my assumption that $_POST['check'] is an array and that I can simply assign it to the var $files.
2) the second issue is that the file that does arrive is corrupt and/or invalid.
Thanks in advance for all responses.
<?php
if ($submit) {
echo "Thank you for your interest in... \n";
// email vars
$to = "me@mydomain.com";
$from = "info@mydomain.com";
$subject ="My subject";
$message = "My message";
$headers = "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 = "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";
$message .= "--{$mime_boundary}\n";
// see which boxes are checked
$files = $_POST['check'];
// prepare attachments
$just=count($files);
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],'rb');
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
if($x==($just-1))
{
$message .= "–{$mime_boundary}-\n";
} else
{
$message .= "–{$mime_boundary}\n";
}
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
} else { ?>
<form id="form1" name="form1" method="post" action="resources.php">
<table width="100%" border="0" cellspacing="5" cellpadding="5" class="boldital11">
<tr>
<td colspan="2" align="center"><span class="red">To help you address your business challenges, we offer a wide range of Best Practice white papers, reports and informative bulletins, free for the asking. Simply check the title(s) you'd like to receive.</span></td>
</tr>
<tr>
<td width="50%" valign="top"><input name="check[]" type="checkbox" value="Marketing_Multi_Building.pdf" />"Best Practices in Marketing Multi-Building Systems"<br />
<input name="check[]" type="checkbox" value="Short_Term_Medicare_Rehab.pdf" />"Best Practices for your Short-Term Medicare Rehab Program"<br />
<input name="check[]" type="checkbox" value="Customized_Customer_Service.pdf" />"Tips for Creating a Culture of Customized Customer Service"</td>
<td valign="top"><input name="check[]" type="checkbox" value="Reallocation_of_Beds.pdf" />"The Case for Reallocation of Your Beds"<br />
<input name="check[]" type="checkbox" value="Cultivating_Satisfaction.pdf" />"The Case for Cultivating Satisfaction Among Short-Term Admissions"<br />
<input name="Add_me" type="checkbox" value="Add_me" />I am interested in receiving future marketing reports and census-building tips.
Add me to your distribution list.</td>
</tr>
</table>
<table width="600" border="0" cellspacing="0" cellpadding="5" class="resources">
<tr>
<td colspan="4" align="center"> </td>
</tr>
<tr>
<td colspan="4" align="center">Please provide the following information (all fields requires): <a href="privacy.php">MDU Privacy Policy</a></td>
</tr>
<tr>
<td colspan="4" align="center"><table><tr><td><label>First Name<br />
<input type="text" name="fname" id="fname" size="20" />
</label></td>
<td><label>Last Name<br />
<input type="text" name="lname" id="lname" size="20" />
</label></td>
<td colspan="2"><label> Name of Organization<br />
<input type="text" name="org" id="org" size="20" />
</label></td></tr></table></td>
</tr>
<tr>
<td><label>Number of beds<br />
<input type="text" name="beds" id="beds" size="15" />
</label></td>
<td><label>Number of facilities<br />
<input type="text" name="facilities" id="facilities" size="15" />
</label></td>
<td><label>Telephone<br />
<input type="text" name="phone" id="phone" size="15" />
</label></td>
<td><label>EMail<br />
<input type="text" name="emai" id="emai" size="15" />
</label></td>
</tr>
<tr>
<td colspan="4" align="center"> </td>
</tr>
<tr>
<td colspan="4" align="center"><input name="submit" type="submit" value="Submit Request" /></td>
</tr>
</table>
</form>
</div>
</div>
</div>
<div class="logosmall"><a href="visit.php"><img src="images/logo_sm.gif" width="170" height="43" border="0" /></a>
</div>
<?php include('footer.php'); ?>
<?php } ?></body>
</html>