2017-07-17 167 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文件区域,您应该添加。如果文件上传成功,则邮件应在此后解除。 –

相关问题