2014-03-02 91 views
-1

我試圖用phpMailer腳本發送附件。附件不會與phpmailer一起發送

由於我的電子郵件發送正確,一切工作正常,但是我確實遇到了未發送附件的問題。

HTML部分:

<p> 
    <label>Attachment :</label> 
    <input name="doc" type="file"> 
</p> 

PHP:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$mail->isSMTP();    
$mail->Host = '*****'; 
$mail->SMTPAuth = true; 
$mail->Port = 465; 
$mail->Username = '*****'; 
$mail->Password = '*****'; 
$mail->SMTPSecure = 'ssl'; 

$mail->From = $_POST["name"]; 
$mail->FromName = 'Your Name'; 
$mail->Subject = 'Message Subject'; 
$mail->addAddress('*****');  

$mail->addAttachment($_FILES["doc"]); 
$mail->isHTML(true); 

$mail->Subject = 'Here is the subject'; 
$mail->Body = "You got a new message from your website : 

    Name: $_POST[name] 
    Company: $_POST[company] 
    Phone: $_POST[phone] 
    Email: $_POST[email] 
    Message: $_POST[message]"; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
    exit; 
} 

?> 

<!DOCTYPE HTML> 
<html> 
<head> 
<title>Thanks!</title> 
</head> 
<body> 
<p> <img src="img/correct.png" alt="icon" style=" margin-right: 10px;">Thank you! We will get back to you soon.</p> 
</body> 
</html> 
+0

我將不勝感激,如果你能解釋一下你的「反對票」 –

回答

1
Try: 

    if (isset($_FILES['doc']) && 
     $_FILES['doc']['error'] == UPLOAD_ERR_OK) { 
     $mail->AddAttachment($_FILES['doc']['tmp_name'], 
          $_FILES['doc']['name']); 
    } 

Basic example can also be found [here](https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/AdvancedMail). 

The function definition for `AddAttachment` is: 

    public function AddAttachment($path, 
            $name = '', 
            $encoding = 'base64', 
            $type = 'application/octet-stream') 
0

請看看如何PHP file uploads工作。此陣列關鍵字:

$_FILES["doc"] 

...將永遠不會包含文件。最多隻能包含一個數組,其中包含有關在何處查找文件的信息。

+0

你可以幫我用一個例子或解決方案?問題是將此與phpmailer –

+0

[示例#2驗證文件上載](http://es1.php.net/manual/en/features.file-upload.post-method.php#example-357)集成。我無法理解,如果將完整的數據陣列視爲文件內容,您的上傳如何在PhpMailer旁邊工作。 –

+0

你有文件上傳在你的應用程序的另一部分工作嗎? –

0

未經檢驗的解決方案,但應該工作:

變化

$mail->addAttachment($_FILES["doc"]); 

$mail->addAttachment($_FILES["doc"]["tmp_name"], $_FILES["doc"]["name"], "base64", $_FILES["doc"]["tmp_type"]); 

但是,請考慮learning php upload file handling如已被別人評論。 請勿在未經驗證的情況下使用用戶輸入。決不!