通过 PHPMailer 从表单输入发送多个附件
Send MULTIPLE Attachments from Input on Form via PHPMailer
我找遍了,但没有找到具体的答案。我在那里有一个表格,用户可以放入多个附件。我正在使用 PHPMailer 发送提交的表单。我到处都看过,似乎有人解释了如何上传多个文件,但不是来自用户输入。谢谢你!。
我的附件上传按钮名称是 "attachments"
if (isset($_POST["submit"])) {
$optionOne = $_POST['optionOne'] ;
$tick = $_REQUEST['tick'] ;
$results = $_REQUEST['results'] ;
$option = $_POST['option'] ;
$option = $_POST['requirements'] ;
$mail = new PHPMailer;
//Server settings
$path = 'upload/' . $_FILES["attachments"]["name"];
move_uploaded_file($_FILES["attachments"]["tmp_name"], $path);
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isSendmail(); // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bonnie'; // SMTP username
$mail->Password = 'bonnie'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('bonniethompson12345@gmail.com', 'Mailer');
$mail->addAddress('bonniethompson12345@gmail.com'); // Add a recipient
$mail->isHTML(true);
$mail->AddAttachment($path); // Set email format to HTML
$mail->Subject = 'Form submission';
$mail->Body = 'This course is identified in my Work Plan and Learning Agreement: $optionOne \n \n I am attending this session because: $tick \n \n What would you like to achieve as a result of your attendance: $results \n \n Do you require adjustments or additions to the session delivery to support your participation: $option \n \n Please provide details of your requirments: $requirements</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->AddAttachment('Logo.png');
if(!$mail->send()) {
echo "Failure to send";
} else {
echo "Message has been sent successfully";
}
}
<p>Please upload any supporting documentation to support your registration request </p>
<div class="browse-button">
<input type="file" name="attachments" multiple="multiple"></input>
</div>
您缺少的是输入元素的命名;你需要使用数组命名来使其正确处理多个文件,所以这样构建它:
<input type="file" name="attachments[]" multiple="multiple">
重要的一点是元素名称末尾的 []
,这意味着多个值将作为数组提交。当您收到来自它的提交时,请尝试 var_dump($_FILES)
以便您可以看到您得到了什么。您还应该检查 move_uploaded_file()
的 return 值,而不是假设它有效。此外,如果您使用 HTML5.
,则不需要关闭 </input>
标签
The multiple file upload example provided with PHPMailer 准确地演示了您的要求,并包括我提到的错误检查。
the PHP docs 中也有介绍。下次仔细看!
我找遍了,但没有找到具体的答案。我在那里有一个表格,用户可以放入多个附件。我正在使用 PHPMailer 发送提交的表单。我到处都看过,似乎有人解释了如何上传多个文件,但不是来自用户输入。谢谢你!。
我的附件上传按钮名称是 "attachments"
if (isset($_POST["submit"])) {
$optionOne = $_POST['optionOne'] ;
$tick = $_REQUEST['tick'] ;
$results = $_REQUEST['results'] ;
$option = $_POST['option'] ;
$option = $_POST['requirements'] ;
$mail = new PHPMailer;
//Server settings
$path = 'upload/' . $_FILES["attachments"]["name"];
move_uploaded_file($_FILES["attachments"]["tmp_name"], $path);
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->isSendmail(); // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'bonnie'; // SMTP username
$mail->Password = 'bonnie'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('bonniethompson12345@gmail.com', 'Mailer');
$mail->addAddress('bonniethompson12345@gmail.com'); // Add a recipient
$mail->isHTML(true);
$mail->AddAttachment($path); // Set email format to HTML
$mail->Subject = 'Form submission';
$mail->Body = 'This course is identified in my Work Plan and Learning Agreement: $optionOne \n \n I am attending this session because: $tick \n \n What would you like to achieve as a result of your attendance: $results \n \n Do you require adjustments or additions to the session delivery to support your participation: $option \n \n Please provide details of your requirments: $requirements</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//$mail->AddAttachment('Logo.png');
if(!$mail->send()) {
echo "Failure to send";
} else {
echo "Message has been sent successfully";
}
}
<p>Please upload any supporting documentation to support your registration request </p>
<div class="browse-button">
<input type="file" name="attachments" multiple="multiple"></input>
</div>
您缺少的是输入元素的命名;你需要使用数组命名来使其正确处理多个文件,所以这样构建它:
<input type="file" name="attachments[]" multiple="multiple">
重要的一点是元素名称末尾的 []
,这意味着多个值将作为数组提交。当您收到来自它的提交时,请尝试 var_dump($_FILES)
以便您可以看到您得到了什么。您还应该检查 move_uploaded_file()
的 return 值,而不是假设它有效。此外,如果您使用 HTML5.
</input>
标签
The multiple file upload example provided with PHPMailer 准确地演示了您的要求,并包括我提到的错误检查。
the PHP docs 中也有介绍。下次仔细看!