2012-07-27 38 views
1

我不完全是一個親,所以請原諒我問這樣一個(可能)愚蠢的問題。瀏覽到圖像文件,然後附加到PHP郵件

我需要幫助設置(在html頁面上)一種方法,允許訪問者瀏覽到計算機上的文件(實際上是2個文件),當他們選擇提交時,該文件將附加到PHP郵件表單。

我知道PHP郵件圖像,我需要使用像PearMime或PHPMailer。

* 更新 *

好了,現在我知道如何把HTML表單來獲取文件。當我使用下面的PHP時,我做錯了什麼?我有兩個通過ID從HTML頁面請求的文件(HTML同時創建了這兩個文件),也就是PHPMailer_5.2.1的位置,$ mail-> MsgHTML(file_get_contents('testfiles.html'));實際上是查看我在該位置創建的html頁面,該頁面僅包含正文中包含單詞Sample的html文檔的模板啓動器。

<?php 

require("PHPMailer_5.2.1/class.phpmailer.php"); 

$mail = new PHPMailer(); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 


$file1 = $_REQUEST['file1']; 
$file2 = $_REQUEST['file2']; 

try { 
    $mail->AddReplyTo('[email protected]', 'FirstName LastName'); 
    $mail->AddAddress('[email protected]', 'FirstName LastName'); 
    $mail->SetFrom('[email protected]', 'FirstName LastName'); 
    $mail->AddReplyTo('[email protected]', 'FirstName LastName'); 
    $mail->Subject = 'PHPMailer Test Subject via mail(), advanced'; 
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically 
    $mail->MsgHTML(file_get_contents('testfiles.html')); 
    $mail->AddAttachment($file1);  // attachment 
    $mail->AddAttachment($file2); // attachment 
    $mail->Send(); 
    echo "Message Sent OK</p>\n"; 
} catch (phpmailerException $e) { 
    echo $e->errorMessage(); //Pretty error messages from PHPMailer 
} catch (Exception $e) { 
    echo $e->getMessage(); //Boring error messages from anything else! 
} 


?> 
+0

我很困惑,這條線有什麼問題:'$ mail-> MsgHTML(file_get_contents('testfiles.html'));'你想用它做什麼不能正常工作? – MasterGberry 2012-07-27 23:07:18

回答

1

您正在尋找HTML屬性?

<form action="upload_file.php" method="post" enctype="multipart/form-data"> 
<label for="file">Filename:</label> 
<input type="file" name="file" id="file" /> 
<br /> 
<input type="submit" name="submit" value="Submit" /> 

enctype="multipart/form-data位於您的標籤或它不會允許附加圖像是非常重要的。然後你的PHP腳本應該根據需要處理這些字段。祝你好運:)

+0

哇,真的那麼簡單嗎?我是這樣想的! – Justgrant2009 2012-07-27 21:01:14

+0

另一個問題,並添加一些代碼到我原來的問題...請檢查。 – Justgrant2009 2012-07-27 21:34:03