Search code examples
phpemailphpmaileremail-attachments

How to attach multiple files to two different e-mails using PHPMailer?


I'm using PHPMailer to send two different e-mails to two different recipients. I want to attach multiple files which the user uploaded to both e-mails.

Now the multiple file attachment works fine for the first mail, but not for the second.

With my current code, the files are only attached to the first mail, but none are attached to the second one:

// First e-mail to recipient 1

    $mail = new PHPMailer;
    $mail->setFrom('example@example.com');
    $mail->addAddress('recipient1@example.com');
    $mail->Subject = 'Subject';
    $mail->isHTML(true);
    $mail->Body = '...';

    // Attach multiple files one by one
    for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
    }

    $mail->send(); // I only wrote this once because as it turns out, it sends both of the mails



// Second e-mail to recipient 2

    $mail = new PHPMailer;
    $mail->setFrom('example@example.com');
    $mail->addAddress('recipient2@example.com');
    $mail->Subject = 'Subject';
    $mail->isHTML(true);
    $mail->Body = '...';


    // Attach multiple files one by one
    for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
        $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
        $filename = $_FILES['userfile']['name'][$ct];
        if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
            $mail->addAttachment($uploadfile, $filename);
        } else {
            $msg .= 'Failed to move file to ' . $uploadfile;
        }
    }

I then tried not to copy the whole function to both mails, but only adding

$mail->addAttachment($uploadfile, $filename); 

to the second e-mail. This, however, only adds the first given file and duplicating this line makes the same file get sent twice.

Any ideas how to attach multiple (3 in my case) files to two different e-mails?


Solution

  • I solved the problem like this:

    // First e-mail to recipient 1
    
        $mail = new PHPMailer;
        $mail->setFrom('example@example.com');
        $mail->addAddress('recipient1@example.com');
        $mail->Subject = 'Subject';
        $mail->isHTML(true);
        $mail->Body = '...';
    
        // Attach multiple files one by one
        for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) {
            $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'][$ct]));
            $filename = $_FILES['userfile']['name'][$ct];
            if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
                $mail->addAttachment($uploadfile, $filename);
            } else {
                $msg .= 'Failed to move file to ' . $uploadfile;
            }
        }
    
    
    // Altered e-mail to recipient 2
    
        $mail->ClearAddresses(); // avoid recipient 1 getting this altered mail
        $mail->addAddress('recipient2@example.com');
        $mail->Subject = 'New subject overwriting the first one';
        $mail->Body = 'New body overwriting the first one';
    
    
        $mail->send(); // send both mails
    

    By that, the same mail is basically sent twice, including the attachments, but with some changes being made by overwriting e. g. the subject and body.