Adding Multiple Attachments in PHPMailer
Asked Answered
R

4

6

I am trying to attach multiple images in the attachments. I have used forearch for every attachment but, its not getting the tempname and name when I use foreach, I'm probably doing something wrong. Below is the code and errors:

Input HTML

<input id="upload-file" class="upload-file" type="file" name="upload-file[]">

var_dump of $_FILES['upload-file']:

array(5) { ["name"]=> array(1) { [0]=> string(47) "WRANGLER_AW13_GIRLONTOP_A4_LANDSCAPE_300dpi.jpg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(24) "C:\xampp\tmp\php41DC.tmp" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(91742) } } 

var_dump for name and temp_name:

Notice: Undefined index: name in C:\xampp\htdocs\hmg\process-email.php on line 66

Notice: Undefined index: tmp_name in C:\xampp\htdocs\hmg\process-email.php on line 67

NULL 
NULL

CODE:

foreach($_FILES['upload-file'] as $file) {         

    $name = $file['name'];
    $path = $file['tmp_name'];
    var_dump($name);
    var_dump($path);

    //And attach it using attachment method of PHPmailer.

    $mail->addattachment($path,$name);
}
Rijeka answered 13/3, 2015 at 12:20 Comment(3)
Be warned that none of the submitted answers handle uploads safely, as per the PHP docs. Base your code on the file upload examples provided with PHPMailer, which handle files correctly.Alleen
My PHP is realy rusty in the meantime, but how is this example anymore safe? It just creates a temp file, which we already have? Even if there is some improvement I do not see, I would call it error handling, but not safe. Speaking about security, we need to make sure, the temp files can not be executed, because of server missconfiguration, preventing a spam relay (captcha or something like that), etc... @AlleenDefoliate
It’s safer because it guarantees that the temp file was created by PHP and was not injected from some other source. Look at the docs for move_uploaded_file for more info; there’s a whole chapter in the PHP docs about this.Alleen
D
7

Welcome to the evil side of PHP. The $_FILES is not that, what a developer would expect.

//wrong code
$img1 = $_FILES['upload-file'][0]['tmp_name'];
$img2 = $_FILES['upload-file'][1]['tmp_name'];

//working code
$img1 = $_FILES['upload-file']['tmp_name'][0];
$img2 = $_FILES['upload-file']['tmp_name'][1];

So you need something like

$totalFiles = count($_FILES['upload-file']['tmp_name']);
for ($i = 0; $i < $totalFiles; $i++) {
   $name = $_FILES['upload-file']['name'][$i];
   $path = $_FILES['upload-file']['tmp_name'][$i];
   $mail->addattachment($path,$name);
}

Here is some example from the PHPMailer repository.

Defoliate answered 13/3, 2015 at 13:23 Comment(0)
R
2

thanks for all the answers. I am sure all of your approaches would work just fine, but I decided to solve it myself. This code solved the problem

$validAttachments = array();

foreach($_FILES['upload-file']['name'] as $index => $fileName) {
    $filePath = $_FILES['upload-file']['tmp_name'][$index];
    $validAttachments[] = array($filePath, $fileName);              
}

foreach($validAttachments as $attachment) {
    $mail->AddAttachment($attachment[0], $attachment[1]);
}

I hope anyone who has the same problem gets some help from here ...

Rijeka answered 14/3, 2015 at 13:24 Comment(0)
B
1

Most of the solutions here are based on forms.

So i came up with a simple solution if you want to attach all files from a particular directory.

$file_to_attach_directory = 'files/';
if ($handle = opendir($file_to_attach_directory)) {
    try {
        while (false !== ($entry = readdir($handle))) {
            $attachment_location = $file_to_attach_directory. $entry;
            $mail->addAttachment($attachment_location);
        }
        closedir($handle);
        // Send Mail
        if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        } else {
            echo "Message sent!";
        }
    } catch (Exception $e) {
        var_dump($e);
    }
}
Bikaner answered 24/6, 2016 at 7:1 Comment(0)
A
0
$i = '0';
foreach($_FILES['upload-file'] as $file) {
$name = $file['name'][$i];
$path = $file['tmp_name'][$i];
var_dump($name);
var_dump($path);
$mail->addattachment($path,$name);
$i++;
}
Angry answered 13/3, 2015 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.