PHP Developers Network
http://forums.devnetwork.net/

Need help with Multiple Attachments
http://forums.devnetwork.net/viewtopic.php?f=52&t=129862
Page 1 of 1

Author:  deniseu [ Tue May 24, 2011 8:12 pm ]
Post subject:  Need help with Multiple Attachments

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:
Syntax: [ Download ] [ Hide ]
$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'));
}
 

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:
Syntax: [ Download ] [ Hide ]
$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]));

    }

  }

}

Here is my form fields:
Syntax: [ Download ] [ Hide ]
<tr>
    <td width="5">&nbsp;</td>
    <td><input type="file" name="user_uploaded_file[]" />
</tr>
<tr>
    <td width="5">&nbsp;</td>
    <td><input type="file" name="user_uploaded_file[]" />
</tr>


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

Author:  deniseu [ Wed May 25, 2011 7:01 pm ]
Post subject:  Re: Need help with Multiple Attachments

Anyone know of a tutorial for multiple attachments????

Author:  Jasper_1 [ Mon Aug 22, 2011 1:23 pm ]
Post subject:  Re: Need help with Multiple Attachments

Why didn't you look at the developers' website?
To add multiple attachments you need to call attach several times. Simple... :banghead:

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/