PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 21, 2017 12:28 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue May 24, 2011 8:12 pm 
Offline
Forum Newbie

Joined: Wed May 26, 2010 11:50 am
Posts: 3
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


Top
 Profile  
 
PostPosted: Wed May 25, 2011 7:01 pm 
Offline
Forum Newbie

Joined: Wed May 26, 2010 11:50 am
Posts: 3
Anyone know of a tutorial for multiple attachments????


Top
 Profile  
 
PostPosted: Mon Aug 22, 2011 1:23 pm 
Offline
Forum Newbie

Joined: Mon Aug 22, 2011 1:17 pm
Posts: 1
Why didn't you look at the developers' website? http://swiftmailer.org/docs/messages.html
To add multiple attachments you need to call attach several times. Simple... :banghead:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group