2017-07-17 3 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-> usernameとpasswordを変更して、自分の証明書を非公開にすることもできます。

+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_fileまたはmove_uploaded_fileに電話する必要があります。 Read 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サンプルにコードを記述してください。

関連する問題