Need help with Multiple Attachments
Posted: Tue May 24, 2011 8:12 pm
Hi
Some time ago I made a form (using swiftmailer) which before been sent via email converts to a pdf and sends as an attachment and it also allowed people to be able to attach a file from their own computers as well.
I am trying to allow users to attach muliple files instead of just one. I have searched the forum and tried several things but still can't get it to work. The form was working attaching the dynamic pdf and a second pdf from the users computer but when I try and adjust it to allow multiple attachments it all seems to work, the email comes through showing attachments and I can open the dynamic pdf but I can't open the others, Acrobat says it cannot open because the file may be the wrong file type or be damaged. Because I can open the dynamic pdf but not the others I assume the pdf's are not be uploaded to the tmp directory.
I am still fairly new to php and swiftmailer so I am hoping someone can help.
Here is my original code:
My form just has an input field called user_uploaded_file
After looking around here is the code I am trying to use to allow multiple attachments:
Here is my form fields:
I'm not sure if something is missing or not. I have been trying for days to figure this out but I can't find specific instructions for allowing multiple attachments and have read and tried lots of stuff from posts on this site. The code from the original one may just need to be changed slightly but I was unsure how. If any one can help it would be greatly appreciated.
Thanks in Advance
Some time ago I made a form (using swiftmailer) which before been sent via email converts to a pdf and sends as an attachment and it also allowed people to be able to attach a file from their own computers as well.
I am trying to allow users to attach muliple files instead of just one. I have searched the forum and tried several things but still can't get it to work. The form was working attaching the dynamic pdf and a second pdf from the users computer but when I try and adjust it to allow multiple attachments it all seems to work, the email comes through showing attachments and I can open the dynamic pdf but I can't open the others, Acrobat says it cannot open because the file may be the wrong file type or be damaged. Because I can open the dynamic pdf but not the others I assume the pdf's are not be uploaded to the tmp directory.
I am still fairly new to php and swiftmailer so I am hoping someone can help.
Here is my original code:
Code: Select all
$attachment_data = array();
//Now check if there's an attachment they've sent
if (!empty($_FILES["user_uploaded_file"]["tmp_name"]))
{
//If an attachment was sent, but there was an error, redirect
if ($_FILES["user_uploaded_file"]["error"] != 0)
{
header("Location: form.php?error=attachment_failed");
exit();
}
else $attachment_data = $_FILES["user_uploaded_file"];
}
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('Energy Rating / Section J Report Order Request Form') // Message subject
->setTo(array('me@myemail.com.au' => 'Insight Energy')) // Array of people to send to
->setFrom(array($post->email => $post->name)) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'report_request.pdf', 'application/pdf')); // Attach the generated PDF from earlier
//Attach any files if they were sent
// PHP stores files in a temporary location and cleans up itself, so we'll just read the temporary file
if (!empty($attachment_data))
{
$attachment_str = file_get_contents($attachment_data["tmp_name"]);
$message->attach(Swift_Attachment::newInstance($attachment_str, $attachment_data["name"], $attachment_data["type"]) ->setFilename('plan.pdf'));
}
After looking around here is the code I am trying to use to allow multiple attachments:
Code: Select all
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('Energy Rating / Section J Report Order Request Form') // Message subject
->setTo(array('me@myemail' => 'Insight Energy')) // Array of people to send to
->setFrom(array($post->email => $post->name)) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'report_request.pdf', 'application/pdf')); // Attach the generated PDF from earlier
//Attach any files if they were sent
if (!empty($_FILES["user_uploaded_file"])) {
//Each element in $_FILES becomes a multidimensional array
foreach ($_FILES["user_uploaded_file"]["tmp_name"] as $key => $value) {
if (!$_FILES["user_uploaded_file"]["error"][$key]) {
$message->attach(Swift_Attachment::newInstance(($value), $_FILES["user_uploaded_file"]["name"][$key], $_FILES["user_uploaded_file"]["type"][$key]));
}
}
}Code: Select all
<tr>
<td width="5"> </td>
<td><input type="file" name="user_uploaded_file[]" />
</tr>
<tr>
<td width="5"> </td>
<td><input type="file" name="user_uploaded_file[]" />
</tr>Thanks in Advance