通过PHPMailer从表单上的输入发送多个附件

时间:2019-05-15 19:25:44

标签: php forms email phpmailer attachment

我已经搜索了所有内容,但是找不到特定的答案。我在那里有一个表格,用户可以放入多个附件。我正在使用PHPMailer发送提交的表单。我到处都看过了,似乎有人在解释如何上传多个文件,而不是从用户输入中解释。谢谢!。

我的附件上传按钮名称是“附件”

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>

1 个答案:

答案 0 :(得分:1)

您缺少的是输入元素的命名;您需要使用数组命名来使其正确处理多个文件,因此按以下方式构建它:

<input type="file" name="attachments[]" multiple="multiple">

重要的一点是元素名称末尾的[],这意味着多个值将作为数组提交。当您收到来自此的提交时,请尝试var_dump($_FILES),以便查看所获得的内容。您还应该检查move_uploaded_file()的返回值,而不是假设它起作用。另外,如果您使用的是HTML5,则不需要结束</input>标记。

The multiple file upload example provided with PHPMailer可以准确地说明您的要求,并包括我提到的错误检查。

the PHP docs中也有介绍。下次再努力!