如何使用PHP邮寄我的附件?

时间:2010-07-10 16:43:13

标签: php html forms webforms

我有一个由输入字段和文件字段组成的基本表单。我有一些我想要表格的事情。收集信息(显然)。还有一个上传文件的选项(可能是.doc,.pdf,.docx),因此我想将附件只限制在那些扩展名下,且不超过2MB。我所知道的是,我必须将我的表格改为“enctype = multipart”,但这就是我所知道的。

我有以下PHP代码:

<?

$to = 'my@email.com';
$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表单代码。

非常感谢任何帮助!

非常感谢, 阿米特

4 个答案:

答案 0 :(得分:1)

要邮寄附件,您可以使用框架方法,也可以自己编写方法。有些sample code可以是good point to start

要获取从浏览器发送的文件,您必须使用FILE变量。 PHP documentation很好地解释了如何做到这一点。

要将文件类型限制为.doc和.pdf,您必须阅读文件的开头并与“真实”.doc或.pdf进行比较。更简单的方法是比较文件扩展名,但您网站的用户始终可以将virus.exe重命名为document.pdf。

答案 1 :(得分:1)

尚未经过测试,但理论上应该有效。

使用此代码使用缓冲区。 (代码来源:http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//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: webmaster@example.com\r\nReply-To: webmaster@example.com";
//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";
?> 

答案 2 :(得分:1)

使用PHPMailer。它是免费的,效果很好,并且可以快速使用附件。我已经看到Swiftmailer在这里提到了很多,但是我没有自己使用它,所以不能说太多。

答案 3 :(得分:0)

如果您可以访问PEAR库,这会使附件变得简单。看看here