表单中的PHPMailer Laravel附件无效

时间:2017-07-17 20:17:57

标签: php laravel phpmailer

我试图在我的PHPmailer中添加PDF附件,但它不会发送附件。

控制器:

        $recipient = Request::input('emailaddress');
        $recipientName = Request::input('name');
        $file = Request::input('file');

     $mail = new \PHPMailer(true); 
        try {
            $mail->isSMTP(); 
            $mail->CharSet = "utf-8"; 
            $mail->SMTPAuth = true;  
            $mail->SMTPSecure = "tls"; 
            $mail->Host = "smtp.sendgrid.net";
            $mail->Port = 587; 
            $mail->Username = "username";
            $mail->Password = "password";
            $mail->setFrom("info@email.nl", "Business");
            $mail->Subject = "Offerte";
            $mail->MsgHTML("Test.");
            $mail->addAddress($recipient, $recipientName);

            if (isset($_FILES[$file]) &&
                $_FILES[$file]['error'] == UPLOAD_ERR_OK) {
                $mail->AddAttachment($_FILES[$file]['tmp_name'],
                $_FILES[$file]['name']);
            }

            $mail->send();
        } catch (phpmailerException $e) {
            dd($e);
        } catch (Exception $e) {
            dd($e);
       }
       return back()->with('send', 'send');    
}

表格:

<form method="POST" action="/offer/sendmail">

        <div class="form-group">
          <label for="name">Aan:</label>
          <input type="text" class="form-control" id="name" name="name" value="{{$offers->name}}">
        </div>

        <div class="form-group">
          <label for="emailaddress">Email-adres:</label>
          <input type="text" class="form-control" id="emailaddress" name="emailaddress" value="{{$offers->email}}">
        </div>

        <div class="form-group">
          <label for="subject">Onderwerp:</label>
          <input type="text" class="form-control" id="subject">
        </div>

        <div class="form-group">
          <label for="subject">Bericht:</label>
          <textarea class="form-control" rows="5" id="comment"></textarea>
        </div>

        <div class="form-group">
          <label for="subject">Voeg een bestand toe:</label>
          <input type ="file" name='file' id='uploaded_file'>
        </div>



      </div>
      <div class="modal-footer">
        <button type="submit" class="btn btn-success">Verzenden</button>

        </form>

它确实发送了电子邮件,但其中没有附件。你知道为什么吗?我还更改了$ mail-&gt;用户名和密码,这样我就可以将我的凭据保密。

1 个答案:

答案 0 :(得分:0)

此代码不安全;你信任来自$_FILES的价值观。您需要致电is_uploaded_filemove_uploaded_fileRead the PHP docs on handling uploads

在更基础的级别上,您尚未在表单上设置enctype属性,因此文件元素无效 - 您需要添加enctype="multipart/form-data",因此您的form标记将会成为

<form method="POST" action="/offer/sendmail" enctype="multipart/form-data">

设置MAX_FILE_SIZE元素也是一个好主意。

将您的代码基于PHPMailer提供的send_file_upload示例,该示例正确且安全地处理上传。