2016-12-14 45 views
0

我是一名php初學者,而我的英語可能是錯誤的...我試圖找到一種方式通過phpmailer發送一封郵件,附帶一個表單。 目前該文件上傳到服務器上的目錄,但有2個文件,一個是好的,另一個是0字節,並且代碼附加了該文件...錯誤的!並且我找不到有什麼問題... 同樣當郵件發送時沒有留言顯示... 如果有人可以幫助我,我非常感謝!

HTMLPhpmailer Addattachment出錯

<form action="formulaire.php" method="post" enctype="multipart/form-data"> 
    <table align="center"><tr><td><label for="nom">Votre nom :</label></td> 
     <td><input type="text" name="nom" required/><br></td></tr> 
     <tr><td><label for="prenom">Votre prénom: </label></td> 
     <td><input type="text" name="prenom" required/><br></td></tr> 
     <tr><td><label for="societe">Société: </label></td> 
     <td><input type="text" name="societe" required/><br></td></tr> 
     <tr><td><label for="phone">Téléphone: </label></td> 
     <td><input type="text" name="phone" required/><br></td></tr> 
     <tr><td><label for="email">Votre E-mail: </label></td> 
    <td><input type="email" name="email" required/><br></td></tr> 
    <tr><td><label for="message">Texte explicatif :</label></td><br> 
     <td><textarea name="message" rows="2" cols="50" required></textarea></td></tr> 
    <tr><td><input type="hidden" name="MAX_FILE_SIZE" value="10000000"> Send this file: <input name="userfile" type="file"></td></tr> 
     <tr><td></td></tr> 
     <tr><td align="center"><input type="submit" value="Envoyer"></td></tr></table> 
</form> 

PHP

<?php 
      if (array_key_exists('userfile', $_FILES)) { 
      $uploadfile = tempnam('upload', $_POST['nom']); 
      if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile.".jpg")) { 
      require_once("PHPMailer/class.phpmailer.php"); 
      require_once('PHPMailer/PHPMailerAutoload.php'); 
      $mail = new PHPMailer(); 
      $mail->From = $_POST['email']; 
      $mail->IsMail(); 
      $mail->ClearAddresses(); 
      $mail->AddAddress ("[email protected]"); 
      $mail->isHTML(true); // Set email format to HTML 
      $mail->Subject = 'xxxxx'; 
      $mail->Body = '<ul> 
      <li>Nom : '. $_POST['nom'] .'</li> 
      <li>Prenom : '. $_POST['prenom'] .'</li> 
      <li>Societe : '. $_POST['societe'] .'</li> 
      <li>Telephone : '. $_POST['phone'] .'</li> 
      <li>E-mail : '. $_POST['email'] .'</li> 
      '.$filename;' 
     </ul>'; 
      $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; 
      $mail->addAttachment($uploadfile, ''); 
      if(!$mail->send()) { 
      $msg = "Mailer Error: " . $mail->ErrorInfo; 
     } else { 
      $msg = "Message sent!"; 
     } 
    } else { 
     $msg = 'Failed to move file to ' . $uploadfile; 
    } 
} 
?> 
+0

您正在定義'$ msg',但從不顯示它。檢查'$ uploadfile'的值和調用addAttachment()'的返回值。請勿將提交者地址用作發件人地址;它是僞造的,並且會導致郵件無法通過SPF檢查。您不需要加載PHPMailer類*和*自動加載器;自動裝載機就是您所需要的。 – Synchro

+0

嗨,感謝您的幫助,我糾正了味精現在顯示的狀態! – mat75011

+0

我刪除了phpmailerclass並保留自動加載器。我不明白From地址的問題!我需要它在我的郵件...我可以把什麼,而不是那個?我檢查了$ uploadfile的值,它是0b文件。所以我改變行$ mail-> addAttachment($ uploadfile。「。jpg」,'');然後現在該文件附加在郵件中!應該是這樣的... – mat75011

回答

0

tempnam()實際創建,當你調用一個空文件。這是它如何「保留」一個獨特的臨時文件供以後使用。但是,當您撥打move_uploaded_file()時,您會在末尾附加「.jpg」。這就是爲什麼你有兩份文件,一份是空的。

在稍後的消息中,您附加了$uploadfile而不是$uploadfile.'.jpg';這就是爲什麼0長度文件是附加到您的電子郵件的原因。請注意,對於第二個參數,您可以根據需要命名該文件並將其用於磁盤上的文件名稱而不是該文件的名稱。這對你的情況會更好,因爲用戶不會得到具有隨機亂碼名稱的文件;相反,您可以使用$_FILES['userfile']陣列中的原始名稱。