Reputation: 23
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('[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;
}
}
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?
Upvotes: 0
Views: 123
Reputation: 23
I solved the problem like this:
// 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
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.
Upvotes: 1
Reputation: 2350
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.
Upvotes: 0