2017-07-17 51 views
0

我試圖在我的PHPmailer中添加PDF附件,但它不會發送附件。PHPMailer Laravel從表單不工作

控制器:

 $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("[email protected]", "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->用戶名和密碼,所以我可以保持我的憑據私人。

+0

你可以調試'isset($ _ FILES [$ file])'& '$ _FILES [$ file] ['error'] ==UPLOAD_ERR_OKÉ'的值,顯然其中一個是錯誤的,我們需要檢查哪一個是。 – milo526

+0

也許是一個愚蠢的問題,因爲我有點新到PHP。我如何調試?只是一個dd? – itvba

+0

這可能是一種方法,你也可以使用'Log :: info($ value)'寫入你的日誌(在導入正確的外觀之後)或者使用'dump()'或者簡單地回顯我使用過的值 – milo526

回答

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示例,該示例正確安全地處理上傳。