【问题标题】:Send MULTIPLE Attachments from Input on Form via PHPMailer通过 PHPMailer 从表单输入发送多个附件
【发布时间】:2019-10-03 00:17:24
【问题描述】:

我已经四处搜索,但找不到具体的答案。我有一个表格,用户可以放入多个附件。我正在使用 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>

【问题讨论】:

    标签: php forms email phpmailer attachment


    【解决方案1】:

    您缺少的是输入元素的命名;你需要使用数组命名来使其正确处理多个文件,所以像这样构建它:

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

    重要的一点是元素名称末尾的[],这意味着多个值将作为数组提交。当您收到来自那里的提交时,请尝试var_dump($_FILES),这样您就可以看到您有什么。您还应该检查move_uploaded_file() 的返回值,而不是假设它有效。此外,如果您使用 HTML5,则不需要关闭 &lt;/input&gt; 标记。

    The multiple file upload example provided with PHPMailer 准确演示了您的要求,包括我提到的错误检查。

    the PHP docs 也对此进行了介绍。下次再努力!

    【讨论】:

    • 谢谢你的救星!下次我会努力寻找哈哈。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 2015-11-29
    • 2017-05-06
    相关资源
    最近更新 更多