I have only been able to attach one pdf to an email at a time. Could someone please help me to figure out how to add multiple attachments to email using PHPMailer? I initially tried just using multiple add attachment statements following each other.
$file1Name = $_FILES['myfile1']['name'];
$file1Path = $_FILES['myfile1']['tmp_name'];
$file2Name = $_FILES['myfile2']['name'];
$file2Path = $_FILES['myfile2']['tmp_name'];
$file3Name = $_FILES['myfile3']['name'];
$file3Path = $_FILES['myfile3']['tmp_name'];
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
$mail->SMTPAuth = true;
$mail->Username = 'email@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// . . .
$mail->Body = $mail_body;
$mail->addAttachment($file1Path, $file1Name);
$mail->addAttachment($file2Path, $file2Name);
$mail->addAttachment($file3Path, $file3Name);
$mail->Send();
This didn't work. After looking online, I found that you can generally only add multiple attachments to an email when the attachments are received from a form input that allows for multiple submissions at a time https://phppot.com/php/send-email-with-multiple-attachments-using-php/. The files are stored together as an array and are attached by looping through the array. It is important to me to not retrieve the multiple files from one form input, so I do not like this option. I thought it might help if I stored the file information in arrays myself, but this also didn't work.
$fileNameArray = array($file1Name, $file2Name, $file3Name);
$filePathArray = array($file1Path, $file2Path, file3Path);
// . . .
for($i = 0; $i < 3; $i++) {
$mail->addAttachment($fileDataArray[$i], $fileNameArray[$i]);
}
$mail->Send();
Next, I tried a solution where I attempted to send multiple emails, each in reply to the previous one, that contained a single attachment. This also didn't work. After the first email, no other attachments were included.
$fileNameArray = array($file1Name, $file2Name, $file3Name);
$filePathArray = array($file1Path, $file2Path, file3Path);
for($i = 0; $i < 3; $i++) {
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = '587';
$mail->SMTPAuth = true;
$mail->Username = 'email@gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// . . .
$mail->Body = $mail_body;
$mail->addAttachment($filePathArray[$i], $fileNameArray[$i]);
I've also looked through some fixes that involve editing the PHPMailer code. Both fixes have to do with the content-ID. I either couldn't find the code that was being referenced for the fix or it seemed like, in the time since the fix was posted, PHPMailer was updated and the fix was implemented.
https://sourceforge.net/p/phpmailer/discussion/130418/thread/42bf5695/ https://developer-paradize.blogspot.com/2015/02/how-to-fix-multiple-attachments-problem.html
I'm kind of lost at what to do at this point. If anyone knows how to add multiple attachments to an email using PHPMailer, could you please help me? Your help is much appreciated. Thank you.