简体   繁体   中英

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?

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 eg the subject and body.

You moved the uploaded files from the temporary store when sending your first mail, and so they are no longer there on the second attempt.

move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)

You need to move the uploaded file first, and then use the variable $uploadfile twice.

You should really put all of that in to a single function so you Don't Repeat Yourself as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM