2017-07-17 2 views
0

J'essaye de créer un formulaire de contact en utilisant PHPMailer. J'ai déjà fait la partie où il envoie l'email, mais j'ai un problème avec la pièce jointe. Mon fichier est téléchargé, mais l'e-mail ne peut pas être envoyé. S'il vous plaît aidez-moi à le réparer, merci beaucoup!Envoyer un e-mail (avec pièce jointe téléchargée) via PHPMailer

Voici mon code 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> 

et le code 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>'; 
       } 
      } 
    ?> 

Merci beaucoup!

+1

essayez de changer $ mail-> addAttachment (pièce jointe $); à $ mail-> addAttachment ($ file); –

+1

Pour résoudre: Quelle est la valeur de $ _FILES ['attachment'] ['name'] '? Que renvoie 'move_uploaded_file'? – showdev

+0

Basez votre code sur l'exemple ['send_file_upload' fourni avec PHPMailer] (https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps). – Synchro

Répondre

0

$mail->addAttachment($attachment);

devrait être $mail->addAttachment($file);

+0

J'ai essayé mais pas travaillé. –

+0

vous devez ajouter si la condition à la zone de fichier move_uploaded. Si le fichier est téléchargé avec succès, le courrier devrait être renvoyé par la suite. –