Full title: "multiple attachments in an email sent from form submission"
Hi there,
I have a test form (you can use it if you want to test it) at: http://www.network-analytics.com/downloads2.php
Currently the user selects a document to download, fills in their details and hits submit. They get the file emailed to them, and I get an email telling me their details and which document they downloaded.
However, I'd like the user to be able to select multiple documents (using tick boxes maybe) to be emailed in one mail. There might be 5 documents up there eventually and they could download as many of them as they want in one go.
Would anyone be able to take a nose at the code below and tell me how it might be adapted for multiple attachments? I was thinking maybe to loop the attachment section, but I don't know how to do it, or what variable to use to come out of the loop.
Many thanks - dunstan orchard
The section from the form that currently defines the documents to be emailed is:
<select name="document">
<option value="Network_analytics_white_ paper.txt">Network Analytics white paper.</option>
<option value="NDAP_Charge_brochure.txt">NDAP-Charge brochure.</option>
</select>
The code in the downloads2_done.php page that the form details are passed to is:
<?
//Usage:
//mailattachments((DestinationAddress), (Subject), (Email Body), (File Attachment Info),(Extra Headers));
//"File Attachment Info" is an array:
//$fileattach[] = array("filename" => "/full/path/to/file/on/your/system",_"mimetype" => "mimetype/here");
Function mailattachments( $to, $subject, $body, $mailattach, $extraheaders)
{
// Generate a unique boundary
$mail_boundary = md5(uniqid(time()));
// MIME-compliant headers
$mailheaders = $extraheaders
."MIME-Version: 1.0\r\n"
."Content-type: multipart/mixed;boundary=\"$mail_boundary\"\r\n"
."\r\n"
."This is a multipart MIME message\r\n"
."\r\n";
// Body. The part that gets displayed as the message:
$mailbody = "--$mail_boundary\r\n"
."Content-type: text/plain;charset=us-ascii\r\n"
."Content-transfer-encoding: 8bit\r\n"
."\r\n"
.$body
."\r\n";
// Now, do the attachments
$fp = fopen($mailattach, "r");
$file = fread($fp, filesize($mailattach));
$file = base64_encode($file); // BASE64-encoded. Text. Nice.
$file = chunk_split($file); // Now in handy bite-sized 76-char chunks.
$filename = basename($mailattach);
$mailbody .= "--$mail_boundary\r\n"
."Content-type: text/plain; name=".$filename."\r\n"
."Content-transfer-encoding: base64\r\n"
."\r\n"
.$file
."\r\n"
."\r\n";
// End of mail
$mailbody .= "--$mail_boundary--";
mail($to, $subject, $mailbody, $mailheaders);
}
?>
</head>
<body>
<?
if (!($Title && $Name && $Job && $Company && $Address1 && $Town && $Postcode && $Email))
{
echo "<h1>Sorry</h1><p><em>All</em> the fields on that form are compulsory and we're afraid you're left at least one of them blank.</p><p>Please use your browser's Back button and complete the form in full.</p>";
}
else
{
echo "<h1>Thankyou</h1><p>The documents you requested:</p><ol><li>$document</li></ol><p>have been e-mailed to you.</p>";
mailattachments("$Email", "Your downloads from Network Analytics", "Please find attached the documents you requested at www.network-analytics.com/downloads2.php.\nIf you have any questions please do not hesitate to contact us.\n\nYour details:\n\n$Title $Name\n$Job\n$Company\n$Address1\n$Address2\n$Town\n$Postcode\n\n$Email\n\nComments:\n$Comments","downloads/$document", "From: marketing@orchardsweb.com\n");
mail("dunstan@orchardsweb.com", "$Name has downloaded some files from the web site", "The files they asked for, and their details are shown below:\n\n$Title $Name\n$Job\n$Company\n$Address1\n$Address2\n$Town\n$Postcode\n\n$Email\n\nComments:\n$Comments\n\nT
he documents requested were:\n$document","From: $Email\n");
}
?>