2017-07-17 3 views
0

PHPMailerを使用して連絡先フォームを作成しようとしています。私はすでに電子メールを送信する部分を行っていますが、添付ファイルに問題があります。私のファイルはアップロードされていますが、電子メールは送信できません。私はそれを修正するのを助けてください、ありがとう!PHPMailer経由で電子メールをアップロードしました。

ここに私のHTML:

<div id="main"> 
     <h1>h1 tag </h1> 
     <div id="login"> 
      <h3>content text. </h3> 
      <hr/> 
      <form action="index.php" method="post" enctype="multipart/form-data"> 
       <input type="text" placeholder="   name" name="subject"/> </br> 
       <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option">Option 1</label> 
       <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option 1Option">Option 2</label> 
       <label class="checkbox-inline"><input type="checkbox" name="checkbox" value="Option 1Option 1Option 1">Option 3</label> 
       <textarea rows="4" cols="50" placeholder="text arena" name="message"></textarea></br>     
       <input type="hidden" name="MAX_FILE_SIZE" value="100000"> Send this file: <input name="attachment" type="file">          
       <input type="submit" value="Send" name="send"/> 
      </form> 
      <div><h5>* new info</h5></div> 
     </div> 
    </div> 

とPHPコード:

<?php 

      require 'phpmailer/PHPMailerAutoload.php'; 
      if(isset($_POST['send'])) 
       { 
       $email = '[email protected]';      
       $password = 'jcfm1211'; 
       $to_id = '[email protected]'; 
       $message = $_POST['message']; 
       $subject = $_POST['subject']; 
       $option = $_POST['checkbox']; 
       $sub = '=?UTF-8?B?'.base64_encode($subject).'?=';    
       date_default_timezone_set('Asia/Ho_Chi_Minh'); 
       $date = date("H:i - d/m/Y", time()); 
       // build attachment- i think here is my problem! 
       $file = "attachment/" . basename($_FILES['attachment']['name']); 
       move_uploaded_file($_FILES['attachment']['tmp_name'], $file)); 

       // build message body 
       $body = ' 
       <html> 
       <body>     
       Info<br> 
       data: '.$date.' <br><br> 
       ___________________________________________________________________<br> 

       Class hours: '.$message.'<br> 
       lựa chọn: '.$option.'<br> 
       <br> 
       Date: '.$message.'<br> 
       <br> 
       You will receive an invitation from client info text removed. You may also receive an update with documents and a reminder with client info text removed. Please watch your e-mail.<br> 
       Thanks,<br> 
       Name<br> 
       ____________________________________________________________________<br> 
       client info text removed<br> 
       client info text removed<br> 
       client info text removed<br> 
       client info text removed<br> 
       client info text removed<br> 
       </body> 
       </html> 
       '; 

       $mail = new PHPMailer; 
       $mail->isSMTP(); 
       $mail->Host = 'smtp.gmail.com'; 
       $mail->Port = 587; 
       $mail->SMTPSecure = 'tls'; 
       $mail->SMTPAuth = true; 
       $mail->Username = $email; 
       $mail->Password = $password; 
       $mail->setFrom('[email protected]', '[email protected]');     
       $mail->addAddress($to_id); 
       $mail->Subject = $sub; 
       // attachment 
       $mail->addAttachment($attachment); 

       $mail->msgHTML($body); 
       if (!$mail->send()) { 
        $error = "Mailer Error: " . $mail->ErrorInfo; 
        ?><script>alert('<?php echo $error ?>');</script><?php 
       } 
       else { 
        echo '<script>alert("Thanks!");</script>'; 
       } 
      } 
    ?> 

感謝そんなに!

+1

は$ mail-> addAttachment($添付ファイル)を変更しようとする必要があります。 〜 $ mail-> addAttachment($ file); –

+1

トラブルシューティング: '$ _FILES ['attachment'] ['name']'の値は何ですか? 'move_uploaded_file'は何を返しますか? – showdev

+0

PHPMailerで提供されている['send_file_upload'の例を参考にしてください。](https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps)。 – Synchro

答えて

0

$mail->addAttachment($attachment);

$mail->addAttachment($file);

+0

私は試しましたが動作しません。 –

+0

move_uploadedファイル領域に条件がある場合は追加する必要があります。ファイルが正常にアップロードされた場合、その後メールが送信されます。 –

関連する問題