简体   繁体   中英

How to Add Multiple Attachments to an Email Using PHPMailer

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.

This question was answered by synchro on another thread. https://github.com/PHPMailer/PHPMailer/issues/2098

"The threads you pointed at are years old!

The article about unique IDs is long obsolete; inline attachments with duplicate cid values will still be ignored, but that's expected behavior, and only applies to inline attachments created using addEmbeddedImage() and addStringEmbeddedImage().

The key problem here is that you're just not handling uploads properly. How you should handle uploads is covered in the PHP docs, and all that occurs before PHPMailer has any involvement.

First of all, you need to understand how file inputs work. These determine what shows up in the $_FILES superglobal that PHP populates for you. You can either have multiple file-type inputs that select a single file each, or you can have a single one that allows you to select multiple files. PHPMailer doesn't care either way, but you have to.

Next, you need to make sure you use move_uploaded_file or at least is_uploaded_file in order to validate what's in the $_FILES superglobal, otherwise, it's not safe.

Thirdly, you need to check whether the calls to addAttachment() are successful – at present, you're just assuming they work and have no error checking at all.

So, I recommend you take a look at the single and multiple file upload examples, both of which do all of the above, and there are no known problems with adding multiple attachments."

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