Problem with Multiple Attachments

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
electric_bigfoot
Forum Newbie
Posts: 2
Joined: Sun Mar 23, 2008 2:58 pm
Location: Washington D.C.

Problem with Multiple Attachments

Post by electric_bigfoot »

I've followed the setup process for "Sending an e-mail from a form with Swift" to a T and it works beautifully! Being a guy who has very very little PHP experience (I refer to it as "the language with the dollar signs everywhere") I had very little trouble customizing the areas I needed to in order for the form mailer to do what I wanted it to do... EXCEPT! ... sending mutliple attachments. :( I can get it to send ONE attachment just fine and I was ecstatic about that but I need it to send three. I will say I searched and searched these forums and found this:

viewtopic.php?f=52&t=67221&st=0&sk=t&sd ... s&start=15

post but I'll be honest, i really don't know what it's telling me to do. If anyone can help me understand this or show me a way to make the "Sending an e-mail from a form with Swift" work for multiple attachments I'd greatly appreciate it. here is the sections of code that I've deduced (great commenting with the code by the way) is dealing with the file attachments:

Code: Select all

 <input type="file" name="attachment"  /><input type="file" name="attachment"  /><input type="file" name="attachment"  /> 

Code: Select all

 
//Check if an attachment was uploaded
$file_path = false;
$file_name = false;
$file_type = false;
if (!empty($_FILES["attachment"]["tmp_name"]))
{
    if ($_FILES["attachment"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $file_path = $_FILES["attachment"]["tmp_name"];
    $file_name = $_FILES["attachment"]["name"];
    $file_type = $_FILES["attachment"]["type"];
}
 
 
//second part of php 
 
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
 
I hope I've asked the question correctly and shown you the code properly. Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Problem with Multiple Attachments

Post by Chris Corbyn »

The very repetitive way....

Code: Select all

<input type="file" name="attachment_1"  />
<input type="file" name="attachment_2"  />
<input type="file" name="attachment_3"  />

Code: Select all

//Somewhere to put information about multiple attachments
$attachments = array();
 
//Check if attachment_1 was uploaded
if (!empty($_FILES["attachment_1"]["tmp_name"]))
{
    if ($_FILES["attachment_1"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $attachments[] = array(
      'path' => $_FILES["attachment_1"]["tmp_name"],
      'name' => $_FILES["attachment_1"]["name"],
      'type' => $_FILES["attachment_1"]["type"]
    );
}
 
//Check if attachment_2 was uploaded
if (!empty($_FILES["attachment_2"]["tmp_name"]))
{
    if ($_FILES["attachment_2"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $attachments[] = array(
      'path' => $_FILES["attachment_2"]["tmp_name"],
      'name' => $_FILES["attachment_2"]["name"],
      'type' => $_FILES["attachment_2"]["type"]
    );
}
 
//Check if attachment_3 was uploaded
if (!empty($_FILES["attachment_3"]["tmp_name"]))
{
    if ($_FILES["attachment_3"]["error"])
    {
        //Redirect if the upload has failed
        header("Location: ./form.php?error=upload_failed");
        exit();
    }
    $attachments[] = array(
      'path' => $_FILES["attachment_3"]["tmp_name"],
      'name' => $_FILES["attachment_3"]["name"],
      'type' => $_FILES["attachment_3"]["type"]
    );
}
Then change the code which adds the attachment:

Code: Select all

//If an attachment was sent, attach it
//if ($file_path && $file_name && $file_type)
//{
//    $message->attach(
//        new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
//}
 
foreach ($attachments as $info)
{
    $message->attach(
        new Swift_Message_Attachment(new Swift_File($info['path']), $info['name'], $info['type']));
}
A more flexible and less-repetitive approach is to send the attachments as an array from the form:

Code: Select all

<input type="file" name="attachment[]"  />
<input type="file" name="attachment[]"  />
<input type="file" name="attachment[]"  />
However, I forget how the $_FILES array gets broken up in that scenario off the top of my head so I can't really show the code very quickly.
electric_bigfoot
Forum Newbie
Posts: 2
Joined: Sun Mar 23, 2008 2:58 pm
Location: Washington D.C.

Re: Problem with Multiple Attachments

Post by electric_bigfoot »

Cool, man thanks so much for the quick reply. I'm going to try this out right now. Thanks so much again. You're awesome!




update: Just checked it out and it works like a charm. Thanks so much, Chris. I will definitely be giving props to you and Swift Mailer all over the place!
Post Reply