2010-07-10 71 views
0

我有一個由輸入字段和文件字段組成的基本表單。我有一些我想要的表單要做的事情。收集信息(顯然)。還有一個選項可以上傳一個文件(可能是.doc,.pdf,.docx),所以我想將附件限制在那些擴展名下,並且小於2MB。我所知道的是,我必須有我的表單「enctype = multipart」,但這就是我所知道的。如何使用PHP發送附件?

我有下面的PHP代碼:

<? 

$to = '[email protected]'; 
$subject = 'Contact from your website'; 
$message = 'From: ' . "\n\n" . 'Name: ' . $_REQUEST['name'] . "\n\n" . 'E-mail: ' . $_REQUEST['email'] . "\n\n" . 'Comments: ' . $_REQUEST['comments']; 
$email = $_REQUEST['email']; 
$headers = 'From: ' . $email . "\r\n" . 
      'Reply-To: ' . $email . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 
$headers .= "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type: text/plain; charset=ISO-8859-1";  



$str = $_REQUEST['email']; 

if($str == "E-mail address" || $str == "") 
{ 
echo "Please use your browser's back button enter a valid E-mail address"; 

} else { 
mail ($to, $subject, $message, $headers); 
header("Location: thankyou.html"); 
} 

?> 

我如何調整它,使其能夠發送附件?

如果可能,我很樂意看到例子。請理解我對PHP非常陌生,直到現在也不需要發送帶有附件的PHP Web表單。因此,我所要求的是,如果您發佈了一個示例,請嘗試澄清可用於該示例的各個HTML表單代碼。

任何幫助將不勝感激!

非常感謝, 阿米特

回答

1

要發送附件,您可以使用框架方法,也可以自己編寫方法。一些sample code可以是good point to start

要從瀏覽器發送文件,您必須使用FILE變量。 PHP documentation很好地解釋瞭如何去做。

要將文件類型限制爲.doc和.pdf,您必須閱讀文件的開頭,並將其與「真實」.doc或.pdf進行比較。更簡單的方法是比較文件擴展名,但是您的網站用戶可以隨時將virus.exe重命名爲document.pdf。

0

如果你的PEAR庫我 - 我的訪問,this'd使添加附件不在話下。看看here

+0

我這裏有幾個問題:1 )如何獲得訪問PEAR庫/服務器上安裝它(或者是它甚至對我?!) 2)在你展示的例子鏈接它說明如何附加預定的文件,如圖像。我希望附加的文件是用戶從他們的計算機上傳的文件(爲了使事情更清楚,我製作了一個簡歷數據庫,所以我希望我的用戶通過表單發送他們的簡歷)。那麼我會用$ file-attached或者類似的東西替換它在「img here」中的位置嗎?! – Amit 2010-07-10 17:00:01

+0

獲取訪問權限 - 如果您擁有服務器並自己管理服務器,則安裝時不應出現任何問題 - 如果您沒有再問您的託管公司PEAR是否可用(以及是否可以提供PEAR)。你不限於你可以附加的東西 - 任何文件都會很好,圖像與否。 – 2010-07-10 17:17:32

+0

謝謝你。但我怎麼做像$ _REQUEST ['文件']代替 ...你知道嗎?發送給我的文件是他們計算機上的文件,不在我的服務器上 – Amit 2010-07-10 17:29:47

1

還沒有測試,但它應該在理論上工作。

使用此代碼使用緩衝區。 (代碼來源:http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

<?php 
//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 
+0

代碼存在一些缺陷。無論哪種方式,我嘗試過了,但沒有奏效。謝謝你嘗試。 – Amit 2010-07-10 18:20:00

1

使用PHPMailer。它是免費的,運行良好,並使附件快速。我見過Swiftmailer在這裏也提到過很多,但是沒有用過,所以不能多說。

+0

我不太明白如何使用PHPMailer。有沒有任何示例文件顯示要做什麼? – Amit 2010-07-10 19:51:18

+0

在PHPMailer站點的菜單欄上,選擇產品 - > PHP郵件程序 - > PHPMailer示例。這裏有很多,從基本的東西到特定的SMTP服務器配置 – 2010-07-12 00:44:31