如何使用PHPMailer将多个文件附加到两个不同的电子邮件中?

问题描述 投票:0回答:2

我正在使用PHPMailer向两个不同的收件人发送两封不同的电子邮件。我想附加用户上传到两封电子邮件的多个文件。

现在,多文件附件适用于第一封邮件,但不适用于第二封邮件。

使用我当前的代码,文件仅附加到第一个邮件,但没有附加到第二个邮件:

// First e-mail to recipient 1

    $mail = new PHPMailer;
    $mail->setFrom('[email protected]');
    $mail->addAddress('[email protected]');
    $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('[email protected]');
    $mail->addAddress('[email protected]');
    $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->addAttachment($uploadfile, $filename); 

到第二封电子邮件。但是,这仅添加第一个给定文件并复制此行使得同一文件被发送两次。

任何想法如何将多个(在我的情况下为3)文件附加到两个不同的电子邮件?

php email phpmailer email-attachments
2个回答
1
投票

我解决了这个问题:

// First e-mail to recipient 1

    $mail = new PHPMailer;
    $mail->setFrom('[email protected]');
    $mail->addAddress('[email protected]');
    $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('[email protected]');
    $mail->Subject = 'New subject overwriting the first one';
    $mail->Body = 'New body overwriting the first one';


    $mail->send(); // send both mails

这样,相同的邮件基本上被发送了两次,包括附件,但是通过覆盖e进行了一些更改。 G。主体和身体。


0
投票

您在发送第一封邮件时从临时存储中移动了上载的文件,因此在第二次尝试时它们不再存在。

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

您需要先移动上传的文件,然后使用变量$uploadfile两次。

你应该把所有这些都放在一个功能中,这样你就不要重复自己了。

© www.soinside.com 2019 - 2024. All rights reserved.