I have a form where a user can send a injury to the website operator and the user can attach some images to this mail. The mail goes to the operator and as a confirmation to the user. I use the library PHPmailer and everything works fine but that the attachment is only attached to one email and not to both.
I have a foreach loop for the fileupload and I save the filepath and name in vars and use the again in a var with addAttachment();
Than I call this variable in my mails.
Ist is Line 181
in my pastebin:
https://pastebin.com/v7jwbZev
I have a $mail->ClearAttachments(); after the first mail sending but it should not be the problem because I fill the $file
before it is called.
I would really appreacheate if some one can help me?
Cheers
You are adding the uploaded files to PHPMailer before the first send (though see my comment on doing it safely). You are then calling clearAttachments()
, which does indeed remove the attachments, so when you call send
the second time, there are no attachments to send.
The easiest fix for this is simply not to call clearAttachments()
at all - then the attachments will remain in pace for the second message. You don't need to call any of the clear*
functions the second time because the instance goes out of scope and so gets destroyed anyway.