简体   繁体   English

如何使用 PHPMailer 向 Email 添加多个附件

[英]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.我一次只能将一个 pdf 连接到 email。 Could someone please help me to figure out how to add multiple attachments to email using PHPMailer?有人可以帮我弄清楚如何使用 PHPMailer 向 email 添加多个附件吗? 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/ .在网上查看后,我发现您通常只能将多个附件添加到 email 当从允许一次多次提交的表单输入接收附件时https://phppot.com/php/send-email-with-多个附件-使用-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.我认为如果我自己将文件信息存储在 arrays 中可能会有所帮助,但这也不起作用。

$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.在第一个 email 之后,没有包含其他附件。

$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.我还查看了一些涉及编辑 PHPMailer 代码的修复程序。 Both fixes have to do with the content-ID.这两个修复都与内容 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.我要么找不到修复引用的代码,要么似乎在发布修复后,PHPMailer 已更新并实施了修复。

https://sourceforge.net/p/phpmailer/discussion/130418/thread/42bf5695/ https://developer-paradize.blogspot.com/2015/02/how-to-fix-multiple-attachments-problem.html 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?如果有人知道如何使用 PHPMailer 向 email 添加多个附件,您能帮帮我吗? Your help is much appreciated.非常感谢您的帮助。 Thank you.谢谢你。

This question was answered by synchro on another thread.这个问题由另一个线程上的同步回答。 https://github.com/PHPMailer/PHPMailer/issues/2098 https://github.com/PHPMailer/PHPMailer/issues/2098

"The threads you pointed at are years old! “你指的那根线是有年头的!

The article about unique IDs is long obsolete;关于唯一 ID 的文章早已过时; 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().具有重复 cid 值的内联附件仍将被忽略,但这是预期的行为,并且仅适用于使用 addEmbeddedImage() 和 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. PHP 文档中介绍了您应该如何处理上传,以及在 PHPMailer 参与之前发生的所有事情。

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.这些决定了 PHP 为您填充的 $_FILES 超全局中显示的内容。 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.您可以有多个文件类型输入,即 select 每个文件,或者您可以有一个允许您输入 select 多个文件的单个文件。 PHPMailer doesn't care either way, but you have to. PHPMailer 不关心任何一种方式,但你必须这样做。

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.接下来,您需要确保使用 move_uploaded_file 或至少 is_uploaded_file 以验证 $_FILES 超全局中的内容,否则不安全。

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.第三,您需要检查对 addAttachment() 的调用是否成功 - 目前,您只是假设它们可以正常工作并且根本没有错误检查。

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."所以,我建议你看一下单文件和多文件上传的例子,它们都做到了以上所有,并且添加多个附件没有已知的问题。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM